java 自动定时_Java设置定时任务

项目需求每日凌晨4点进行对账业务,下面是定时任务:

首先创建该类的监听类CheckAccountListener

1 public class CheckAccountListener implementsServletContextListener {2

3 private staticCheckAccountTimer timer;4

5 @Override6 public voidcontextDestroyed(ServletContextEvent arg0) {7 try{8 if(timer!= null){9 timer.stop();10 timer = null;11 }12

13 }catch(Exception e){14 e.printStackTrace();15 }16

17 }18

19 @Override20 public voidcontextInitialized(ServletContextEvent arg0) {21 try{22 timer = newCheckAccountTimer();23 timer.start();24 } catch(Exception e) {25 e.printStackTrace();26 }27

28 }29

30 }

再创建该类的time方法

1 public class CheckAccountTimer extendsTimer {2 private boolean isRun = false;3

4 public synchronized booleanstart() {5

6 try{7 //规定的每天时间凌晨4点运行

8 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd '04:00:00'");9 //首次运行时间

10 Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(newDate()));11 //如果今天的已经过了 首次运行时间就改为明天

12 if (System.currentTimeMillis() >startTime.getTime()){13 startTime = new Date(startTime.getTime() + 1000 * 60 * 60 * 24);14 }15

16

17 if(isRun) {18 System.out.println("**********对账程序已经启动");19 return true;20 }21 this.schedule(new CheckAccount(), startTime, 1000 * 60 * 60 * 24);22 isRun = true;23 System.out.println("**********对账程序启动成功");24 return true;25 } catch(Exception e) {26 System.out.println("**********对账程序出错");27 e.printStackTrace();28 isRun = false;29 return false;30 }31 }32

33 public booleanstop() {34 try{35 if (!isRun) {36 System.out.println("**********对账程序并未启动");37 return true;38 }39 this.cancel();40 isRun = false;41 System.out.println("**********停止订对账程序成功");42 return true;43 } catch(Exception e) {44 System.out.println("**********停止对账程序出错");45 e.printStackTrace();46 return false;47 }48 }49 }

Timer schedule执行定时任务

只执行一次的方法

Timer.schedule(TimerTask task, long delay)方法在程序中只执行一次(long delay 为距离启动任务的时间差)

注释:Schedules the specified task for execution after the specified delay。大意是在延时delay毫秒后执行task。

Timer.schedule(TimerTask task, Date time) 方法在程序中只执行一次(Date time 为设定的启动时间)

重复执行方法

Timer.schedule(TimerTask task, long delay, long period)是重复的执行

注释:Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay。大意是在延时delay毫秒后重复的执行task,周期是period毫秒

Timer.schedule(TimerTask task, Date time, long period) 是重复的执行 (Date time 为启动时间)

配置web.xml文件

在servlet-mapping下添加监听

1

2 com.sales.task.CheckAccountListener

3

web.xml下监听的作用

web.xml下的配置一:

1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: 和

2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.

3.容器将转化为键值对,并交给ServletContext.

4.容器创建中的类实例,即创建监听.

5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得

ServletContext = ServletContextEvent.getServletContext();

context-param的值 = ServletContext.getInitParameter("context-param的键");

6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.

换句话说,这个时候,你对中的键值做的操作,将在你的WEB项目完全启动之前被执行.

7.你可能想在项目启动之前就打开数据库.

那么这里就可以在中设置数据库的连接方式,在监听类中初始化数据库的连接.

8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.


版权声明:本文为weixin_29097457原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。