本文章记录如何实现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>
以上即为整个的实现流程,所有内容用于个人总结,如有不恰当的请包含!
相关推荐
只需要在web.xml文件中添加监听器listener,然后加入附件中的文件,并将lstener指向所添加的文件TimerTaskListener,定时任务的研发便可轻松搞定!
java web定时任务 实例 博文链接:https://af8991.iteye.com/blog/1165178
主要介绍了Java Web实现添加定时任务的方法,涉及java web定时任务控制类定义、调用及监听器定义、添加等相关操作技巧,需要的朋友可以参考下
该demo只是实现定时监听、周期性执行任务的功能,而邮件发送、文件删除等自定义功能需要自行添加上去,只需在run方法下修改邮件发送、文件删除等功能即可;
在Java Web程序中加入定时任务,这里介绍两种方式使用监听器注入,使用Spring注解@Scheduled注入,需要的朋友可以参考下
附件中实现了对quartz的动态添加、修改和删除定时任务,方便通过页面动态控制执行定时任务。
本篇文章主要介绍了四步轻松搞定java web每天定时执行任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
JAVA项目服务器启动时自启动指定的Servlet,并定时执行任务。 配置关键在web.xml和servlet文件中的init操作
下面小编就为大家分享一篇Java实现Web 应用中的定时任务的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
本篇文章主要介绍了Java Web项目中编写定时任务的实现,具有一定的参考价值,有兴趣的可以了解一下。
Spring Boot 通过web开启和关闭定时任务
java quartz计划任务中间件使用教程,每天定时备份数据库的插件jspweb java网站工程.rar
Timer schedule JAVA J2EE WEB 指定每一天某时某分执行某项任务的功能。 读取目录下配置文件的内容。 开发环境jdk1.5.0_22、MyEclipse6.5中文版。...定时任务管理器,定时执行,定时任务,run 绝对能用
我们通常写的timer都是用main方法写的定时器,但同样我们也需要根据服务器启动后定时器也启动的 定时执行任务。不过有个致命伤就是集群方面跟quartz不能比,此方法代码做参考不错,如果要用,那么这个方法在一台...
web-flash是一个基于Spring Boot和Vue.js的web系统,包含了基于...基于Spring Boot+Vue.js的后台管理系统,权限管理,字典,配置,定时任务,短信,邮件,根据excel模板导出,cms内容管理,手机端h5,IDEA 代码生成插件
基于flask实现的web,celery实现异步以及任务分布式,aspchedluer实现定时任务以及对任务的暂停,恢复,删除。 该项目完成基本功能,如需增加更多需求,请自行完成 具体细节说明在代码中会有详细注释
定时任务管理带WEB管理界面-quartz-web
quartz定时任务,在web项目中的使用方法;有例子和包直接拿到工程里陪陪就能用
比如利用控制反转的核心特性,并通过依赖注入实现控制反转来实现管理对象生命周期容器化,利用面向切面编程进行声明式的事务管理,整合多种持久化技术管理数据访问,提供大量优秀的Web框架方便开发等等。Spring框架...
为您提供PowerJob java任务调度框架下载,PowerJob是全新一代分布式调度与计算框架,支持CRON、API、固定频率、固定延迟等调度策略,提供工作流来编排任务解决依赖关系,使用简单,功能强大,文档齐全,能让您轻松...