本文章记录如何实现web项目启动之后,自动运行我们设置的定时任务的案例
- 首先定义自己的Task(即自启动所要执行的方法)
我这里所要执行的是一段自动发送短信的代码,大家可以根据自己的需求自由订制,只需要在run()中实现自己需要执行的任务即可
package com.jr.p2p.manager.sendmessage;
import java.util.TimerTask;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;
import com.jr.p2p.framework.system.service.SystemParamService;
import com.jr.p2p.manager.noteinfo.service.NoteInfoService;
@RestController
public class NoteInfoTimer extends TimerTask
{
/**
* 短信发送服务接口
*/
@Resource
private NoteInfoService noteInfoService;
@Resource
private SystemParamService systemParamService;
@Override
public void run()
{
//提醒日短信模版
String repayContent = systemParamService.queryValueByParamKey("repayment_msg_content");
//逾期提醒短信模版
String overRepayContent = systemParamService.queryValueByParamKey("repayment_over_msg_content");
//是否关闭新标上线短信提醒
String closeNewSubjectInfo = systemParamService.queryValueByParamKey("close_send_newsubject_msg");
//新标上线模版自动生成,此处可传空字符串
if("no".equalsIgnoreCase(closeNewSubjectInfo)){
noteInfoService.sendNewSubjectsInfo("新标上线了");
}
noteInfoService.sendRepayDateInfo(repayContent);
noteInfoService.sendOverDateInfo(overRepayContent);
}
}
- 指定第一次执行的时间,以及每次执行的时间间隔
new Timer().schedule(task, firstTime, delayTime) :
task :即为我们在上面自定义的task
firstTime:为第一次执行的时间,可以为Date(第一次执行的日期), long(web启动后延迟多少毫秒运行)
delayTime:为每隔多久自动执行(毫秒数)、
这里我实现了第一次执行的时间可配置,通过getSmsSendTime()方法从数据库中读取我们规定第一次执行的时间并返回一个Date类型
package com.jr.p2p.manager.sendmessage;
import java.util.Date;
import java.util.Timer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.jr.p2p.common.util.YunTuDateUtils;
import com.jr.p2p.framework.system.service.SystemParamService;
@RestController
public class MessageSend
{
private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
@Autowired
private NoteInfoTimer noteInfoTimer;
@Autowired
private SystemParamService systemParamService;
public void timeStart(){
Date smsSendTime = getSmsSendTime();
new Timer().schedule(noteInfoTimer, smsSendTime,1000*3600*24);
}
private Date getSmsSendTime()
{
Date result = new Date();
String todayTimeStr = YunTuDateUtils.date2String(result, DEFAULT_DATE_FORMAT);
todayTimeStr = todayTimeStr.substring(0, todayTimeStr.indexOf(" "));
String smsSendTime = systemParamService.queryValueByParamKey("sms_send_time");
if(StringUtils.isEmpty(smsSendTime))
{
smsSendTime = "10:00:00";
}
else
{
smsSendTime = smsSendTime.trim();
}
if (YunTuDateUtils.string2Date(todayTimeStr + " " + smsSendTime, DEFAULT_DATE_FORMAT) == null)
{
smsSendTime = "10:00:00";
}
result = YunTuDateUtils.string2Date(todayTimeStr + " " + smsSendTime, DEFAULT_DATE_FORMAT);
if (result.getTime() < new Date().getTime())
{
result = YunTuDateUtils.addDay(result, 1);
}
return result;
}
}
- 实现web的监听类(即为我们在web配置需要监听的那个类)
定义自己的监听类需要实现ServletContextListener接口并实现contextInitialized()方法,容器销毁时
contextDestroyed()的方法,这里未做具体实现,有需要的可以自行定义
package com.jr.p2p.manager.listerner;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.jr.p2p.manager.sendmessage.MessageSend;
public class TimeListener implements ServletContextListener{
//application容器初始化监听函数
@Override
public void contextInitialized(ServletContextEvent sce) {
// 获取容器与相关的Service对象
ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
MessageSend messageSend = (MessageSend)ac.getBean("messageSend");
messageSend.timeStart();
System.out.println("------------>已启动定时器服务类<------------");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
- 配置web.xml文件
这里就是配置我们的监听类,非常重要的一点一定要牢记:
用于做初始化工作的监听器,一定要配置到Spring的ContextLoaderListener之后,因为要用到Spring的容器对象
<listener> <listener-class>com.jr.p2p.manager.listerner.TimeListener</listener-class> </listener>
以上即为整个的实现流程,所有内容用于个人总结,如有不恰当的请包含!