Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。
使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少
监听器类:
package com.tech.jin.listener; import java.util.Timer; import java.util.TimerTask; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; import org.springframework.web.context.support.WebApplicationContextUtils; import com.tech.jin.task.OneTask; import com.tech.jin.util.Const; /** * 任务监听器 需要配置在web.xml的listener中 * @author Administrator * */ public class TaskListener implements ServletContextListener{ private Logger logger = Logger.getLogger(TaskListener.class); private ServletContext context = null; private Timer timer = null; @Override public void contextDestroyed(ServletContextEvent event) { logger.info("TaskListener contextDestroyed..."); timer.cancel(); context = null; } @Override public void contextInitialized(ServletContextEvent event) { logger.info("TaskListener contextInitialized..."); context = event.getServletContext(); //如果使用spring框架,可将context放入到一个工具类中的常量,用来取到WebApplicationContext // Const.WEB_APP_CONTEXT = WebApplicationContextUtils.getWebApplicationContext(context); logger.info("定时器开始初始化..."); timer = new Timer(); logger.info("定时任务OneTask创建..."); TimerTask task = new OneTask(context); long period = 60*60*1000;//每隔1小时 timer.schedule(task, 0, period); logger.info("定时任务OneTask已添加到任务调度..."); //TODO } }
任务类:
package com.tech.jin.task; import java.util.Calendar; import java.util.TimerTask; import javax.servlet.ServletContext; import org.apache.log4j.Logger; public class OneTask extends TimerTask{ private Logger logger = Logger.getLogger(OneTask.class); private ServletContext context = null; private static final int taskExcuteTime = 15;//15点钟 public OneTask(ServletContext context){ this.context = context; } @Override public void run() { logger.info("OneTask run..."); Calendar c = Calendar.getInstance(); int currentHour = c.get(Calendar.HOUR_OF_DAY);//24小时制,取值0-24 if(currentHour==taskExcuteTime){ logger.info("任务开始..."); try { doTask();//执行任务逻辑 } catch (Exception e) { logger.error("任务执行异常:"+e); } logger.info("任务结束..."); } } public void doTask() throws InterruptedException{ logger.info("doTask"); logger.info("context:"+context); } }
另一种是相比于Timer的单线程,它是通过线程池的方式来执行任务的。
package com.tech.jin.listener; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; import com.tech.jin.task.OneTask; /** * 任务监听器 需要配置在web.xml的listener中 * @author Administrator * */ public class TaskListener implements ServletContextListener{ private Logger logger = Logger.getLogger(TaskListener.class); private ServletContext context = null; private Timer timer = null; @Override public void contextDestroyed(ServletContextEvent event) { logger.info("TaskListener contextDestroyed..."); timer.cancel(); context = null; } @Override public void contextInitialized(ServletContextEvent event) { logger.info("TaskListener contextInitialized..."); context = event.getServletContext(); //如果使用spring框架,可将context放入到一个工具类中的常量,用来取到WebApplicationContext // Const.WEB_APP_CONTEXT = WebApplicationContextUtils.getWebApplicationContext(context); //1.定时任务实现方式一 /* logger.info("定时器开始初始化..."); timer = new Timer(); logger.info("定时任务OneTask创建..."); TimerTask task = new OneTask(context); long period = 60*60*1000;//每隔1小时 timer.schedule(task, 0, period); logger.info("定时任务OneTask已添加到任务调度...");*/ //2.定时任务实现方式二 相比于Timer的单线程,它是通过线程池的方式来执行任务的 Runnable r = new Runnable() { @Override public void run() { // TODO Auto-generated method stub } }; long initialDelay = 0;//创建对象马上执行 long period = 1;//间隔一秒 ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(r, initialDelay, period, TimeUnit.SECONDS); } }