spring定时任务的简单应用(转载)
这几天因为工作的需要,用到了spring的定时任务的功能,网上查资料,并且实际运用了一下,觉得spring还是很智能化的,只需要配置一下配置文件就可以了,下面的这篇文章我觉得还是很好的,摘录下来,供以后参考用
考用 package util; import org.apache.log4j.*; public class TaskJob { public static Logger log = Logger .getLogger(TaskJob. class ); public void SayHello() { // TODO Auto-generated method stub try { log.info( "处理任务开始>........" ); // 业务逻辑代码调用 System.out.println( "时间[" + new java.util.Date().toLocaleString() + "]----->大家好啊!" ); log.info( "处理任务结束!" ); } catch (Exception e) { log.error( "处理任务出现异常" , e); } } } |
2 .接下来的在spring 里配置: <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" > <beans> <bean name= "taskJob" class = "util.TaskJob" /> <bean id= "methodInvokingJobDetail" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <property name= "targetObject" > <ref bean= "taskJob" /> </property> <property name= "targetMethod" > <value>SayHello</value> </property> </bean> <!-- 配置触发器 --> <bean id= "cronTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" > <!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 --> <property name= "jobDetail" > <ref bean= "methodInvokingJobDetail" /> </property> <!-- 每天的 8 点到 21 点每隔 1 分钟触发,具体说明见附录 --> <property name= "cronExpression" > <value> 0 * 08 - 21 * * ?</value> </property> </bean> <bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > <!-- 添加触发器 --> <property name= "triggers" > <list> <ref local= "cronTrigger" /> </list> </property> </bean> </beans> |
3 .测试执行的类,只要把spring的配置文件加载了就可以看到定时任务运行了。 package util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "加载spring配置文件...." ); ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); System.out.println( "加载配置文件完毕!" ); // ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml"); } } 如果要在web项目中运行的话还要在web.xml 中加入如下代码: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.properties</param-value> </context-param> <listener> <listener- class >org.springframework.web.context.ContextLoaderListener</listener- class > </listener> |
以下是网上摘抄的一些说明:
字段允许值允许的特殊字符
秒
0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W
C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选)留空,
1970-2099 , - * /
表达式意义
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *"
每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *"
每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?"
在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5
14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?"
在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15
10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L
* ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L
2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3"
每月的第三个星期五上午10:15触发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步