Java计时器Timer 使用 完整案例(转)

                                                 Java计时器Timer 使用 完整案例

    现在大家看一下我写的关于实现LED滚屏显示动态信息!

    web 监听:

    Java代码

    package com.ving.xzfw.led;

    import java.util.Timer;//定时器类

    import javax.servlet.ServletContextEvent;

    import javax.servlet.ServletContextListener;

    import javax.servlet.ServletContext;

    import org.apache.log4j.Logger;

    import com.ving.xzfw.util.GetInformation;

    //import com.ving.xzfw.lwsp.GetConfigInfor;

    public class LEDListener implements ServletContextListener {

    private Timer timer = null;

    private Logger log = Logger.getLogger(getClass());

    //  GetconfigInfor config = new GetconfigInfor();

    GetInformation getInfo = new GetInformation("bpelConfig.properties");

    Integer runTime = Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));

    String fileRealPath = "";

    String templePath = "";

    public void contextInitialized(ServletContextEvent event) {

    // 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能

    ServletContext context = event.getServletContext();

    fileRealPath = context.getRealPath("")

    + System.getProperty("file.separator") + "LEDFile"

    + System.getProperty("file.separator");

    templePath = context.getRealPath("")

    + System.getProperty("file.separator") + "led"

    + System.getProperty("file.separator");

    timer = new Timer();

    log.info("定时器已启动");// 添加日志,可在tomcat日志中查看到

    timer.schedule(new LEDTimerTack(fileRealPath, templePath), 20000, runTime);

    log.info("已经添加任务");

    }

    public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。

    timer.cancel();

    log.info("定时器销毁");

    }

    }

    package com.ving.xzfw.led;

    import java.util.Timer;//定时器类

    import javax.servlet.ServletContextEvent;

    import javax.servlet.ServletContextListener;

    import javax.servlet.ServletContext;

    import org.apache.log4j.Logger;

    import com.ving.xzfw.util.GetInformation;

    //import com.ving.xzfw.lwsp.GetConfigInfor;

    public class LEDListener implements ServletContextListener {

    private Timer timer = null;

    private Logger log = Logger.getLogger(getClass());

    //GetconfigInfor config = new GetconfigInfor();

    GetInformation getInfo = new GetInformation("bpelConfig.properties");

    Integer runTime = Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));

    String fileRealPath = "";

    String templePath = "";

public void contextInitialized(ServletContextEvent event) {

    // 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能

    ServletContext context = event.getServletContext();

    fileRealPath = context.getRealPath("")

    + System.getProperty("file.separator") + "LEDFile"

    + System.getProperty("file.separator");

    templePath = context.getRealPath("")

    + System.getProperty("file.separator") + "led"

    + System.getProperty("file.separator");

    timer = new Timer();

    log.info("定时器已启动");// 添加日志,可在tomcat日志中查看到

    timer.schedule(new LEDTimerTack(fileRealPath, templePath), 20000, runTime);

    log.info("已经添加任务");

    }

    public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。

    timer.cancel();

    log.info("定时器销毁");

    }

    }

    LEDTimeTrack

    Java代码

    package com.ving.xzfw.led;

    import java.util.TimerTask;

    public class LEDTimerTack extends TimerTask {

    String fileRealPath = "";

    String templePath = "";

    String type="";

    int floor;

    public LEDTimerTack(String fileRealPath,String templePath,String type,Integer floor) {

    this.fileRealPath=fileRealPath;

    this.templePath=templePath;

    this.type =type;

    this.floor=floor;

    }

    public LEDTimerTack(String fileRealPath,String templePath) {

    this.fileRealPath=fileRealPath;

    this.templePath=templePath;

    }

    public void run( ) {

    for(int i=1;i<=3;i++)

    //呵呵,这里就是led动态生成信息的代码!

    XlsUtil.createHtmlFile(templePath, fileRealPath,i);

    }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值