JAVA 定时器 信息推送

 

package com.flux.wms.menu.system;

import java.util.Timer;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import com.flux.wms.actions.common.BaseAction;
import com.flux.wms.models.common.CommonBusinessModel;
import com.flux.wms.util.Util;
import com.ibm.rcp.platform.mmi.PopupWindowA1;
/**
 * Project Name:wms.Project
 * 
 * @author sisi
 * 
 * Purpose: 预警消息推送
 * 
 * Create Time: 20120530
 * 
 * Create Specification:
 * 
 * Modified Time:
 * 
 * Modified by:
 * 
 * Modified Specification:
 * 
 * Version: 1.0
 */
public class PopupWindowTimer extends BaseAction  {
	private int beginTime = 2000;//启动时间
	private int delayTime = 20000;//间隔时间

	Timer timer = null;
	
	
	public void run(){
		beginPopupWindowTimer();
	}
	
	//启动定时器
	public void beginPopupWindowTimer(){
		timer = new Timer();
		PopupWindowTimerTask myTask = new PopupWindowTimerTask();
		timer.schedule(myTask, beginTime, delayTime);//时间单位:毫秒
	}
	
	//停止定时器
	public void stopPopupWindowTimer(){
		if(timer != null){
			timer.cancel();
		}
	}
	
	//调用消息推送功能
    static class PopupWindowTimerTask extends java.util.TimerTask {

         public void run() {
     		Display display = PlatformUI.getWorkbench().getDisplay();
    		if (!display.isDisposed()) {
    			display.asyncExec(new Runnable() {
    				public void run() {
    		        	 try{
    		        		 String Message = getMessage();
    		        		 if(Util.isNotNull(Message)){
    		        			 PopupWindowA1 popWin = new PopupWindowA1(Message);
        		        		 popWin.start();
    		        		 }
    		        		
    		        	 }catch(Exception err){
    		        		 err.printStackTrace();
    		        	 }
    				}
    			});
    		}

        	
        }
         
     	//获取需要推送的信息
     	private String getMessage(){
     		String mess = CommonBusinessModel.getInstance().getAlertMessage();
     		return mess;
     	}


    }

    //设置启动时间
	public void setBeginTime(int beginTime) {
		this.beginTime = beginTime;
	}

	//设置间隔时间
	public void setDelayTime(int delayTime) {
		this.delayTime = delayTime;
	}
}

你可能感兴趣的:(java,Runnable,timer,PopupWindow)