maven构建使用Spring执行定时任务
来源:互联网 发布:hbo直播软件 编辑:程序博客网 时间:2024/03/29 23:47
首先,pom.xml中加入依赖:
版本配置信息:
<properties><java-version>1.6</java-version><org.springframework-version>3.1.1.RELEASE</org.springframework-version><tiles-version>2.2.1</tiles-version><org.aspectj-version>1.6.10</org.aspectj-version><org.slf4j-version>1.6.6</org.slf4j-version></properties>
依赖:
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework-version}</version><exclusions><!-- Exclude Commons Logging in favor of SLF4j --><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion><exclusion><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId></exclusion><exclusion><groupId>commons-digester</groupId><artifactId>commons-digester</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${org.springframework-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${org.springframework-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${org.springframework-version}</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>1.8.5</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${org.springframework-version}</version></dependency><!-- AspectJ --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${org.aspectj-version}</version></dependency>
java代码:
package com.cetc.yun.service.impl;import org.apache.log4j.Logger;import com.cetc.yun.service.TaskTestService;public class TaskTestServiceImpl implements TaskTestService {private static Logger log = Logger.getLogger(TaskTestServiceImpl.class);@Overridepublic void sayHello() { // TODO Auto-generated method stub try { log.info("处理任务开始>........"); // 业务逻辑代码调用 log.info("时间[" + new java.util.Date().toGMTString() + "]----->大家好啊!"); log.info("处理任务结束!"); } catch (Exception e) { log.error("处理任务出现异常", e); }}}
关键配置信息:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"xmlns:beans="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"><!-- Root Context: defines shared resources visible to all other web components --><bean name="taskJob" class="com.cetc.yun.service.impl.TaskTestServiceImpl" /> <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.CronTriggerFactoryBean"> <!-- 这里不可以直接在属性jobDetail中引用taskJob,因为他要求的是一个jobDetail类型的对象,所以我们得通过MethodInvokingJobDetailFactoryBean来转一下 --> <property name="jobDetail"><ref bean="methodInvokingJobDetail" /></property> <!-- 每天的8点到21点每隔1分钟触发,具体说明见附录 --> <property name="cronExpression"><value>0/5 * 08-21 * * ?</value></property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 添加触发器 --> <property name="triggers"> <list> <ref local="cronTrigger" /> </list> </property> </bean></beans>
其他配置略
参考文章:http://blog.sina.com.cn/s/blog_4847a92801008nt4.html http://my.oschina.net/SmilePlus/blog/83726
在本机测试通过
运行结果:
INFO : com.cetc.yun.service.impl.TaskTestServiceImpl - 处理任务开始>........
INFO : com.cetc.yun.service.impl.TaskTestServiceImpl - 时间[26 Jul 2013 03:23:00 GMT]----->大家好啊!
INFO : com.cetc.yun.service.impl.TaskTestServiceImpl - 处理任务结束!
以下是网上摘抄的一些说明:
字段允许值允许的特殊字符
秒 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触发
- maven构建使用Spring执行定时任务
- eclipse 使用maven构建springboot+定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用Spring Quartz执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- Spring使用注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- Visual Assist X 注释报错的解决
- iphone数据存储之-- Core Data的使用(一)
- 【C/C++学习】之十三、虚函数剖析
- XMPP——Smack[2]会话、消息监听、字体表情和聊天窗口控制
- c++ 对象释放
- maven构建使用Spring执行定时任务
- 飘逸的python - super()只不过是用来避免硬编码的
- win7 64位系统 下的 odbc数据源
- Javascript & CSS gotchas in Mobile Web App Development
- Python的50个模块,满足你各种需要
- 2013-07-26(周五)
- 通过GC创建dataguard备库失败一则
- IE文档模式
- Java 环境变量配置