您的位置:首页 > 编程语言 > Java开发

Java设置定时任务

2015-11-16 10:52 387 查看
项目需求每日凌晨4点进行对账业务,下面是定时任务:

首先创建该类的监听类CheckAccountListener

public class CheckAccountListener implements ServletContextListener {

private static CheckAccountTimer timer;

@Override
public void contextDestroyed(ServletContextEvent arg0) {
try{
if(timer!= null){
timer.stop();
timer = null;
}

}catch(Exception e){
e.printStackTrace();
}

}

@Override
public void contextInitialized(ServletContextEvent arg0) {
try {
timer = new CheckAccountTimer();
timer.start();
} catch (Exception e) {
e.printStackTrace();
}

}

}


再创建该类的time方法

public class CheckAccountTimer extends Timer {
private boolean isRun = false;

public synchronized boolean start() {

try {
// 规定的每天时间凌晨4点运行
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd '04:00:00'");
// 首次运行时间
Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(new Date()));
// 如果今天的已经过了 首次运行时间就改为明天
if (System.currentTimeMillis() > startTime.getTime()){
startTime = new Date(startTime.getTime() + 1000 * 60 * 60 * 24);
}

if (isRun) {
System.out.println("**********对账程序已经启动");
return true;
}
this.schedule(new CheckAccount(), startTime, 1000 * 60 * 60 * 24);
isRun = true;
System.out.println("**********对账程序启动成功");
return true;
} catch (Exception e) {
System.out.println("**********对账程序出错");
e.printStackTrace();
isRun = false;
return false;
}
}

public boolean stop() {
try {
if (!isRun) {
System.out.println("**********对账程序并未启动");
return true;
}
this.cancel();
isRun = false;
System.out.println("**********停止订对账程序成功");
return true;
} catch (Exception e) {
System.out.println("**********停止对账程序出错");
e.printStackTrace();
return false;
}
}
}


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下添加监听

<listener>
<listener-class>com.sales.task.CheckAccountListener</listener-class>
</listener>


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.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: