Spring定时器实现(二)
Spring结合quarzt可以实现更复杂的定时器,现做简单介绍相关配置:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd "> <description>spring-configuration</description> <bean id="timerTask" class="com.charles.spring.service.impl.TimerTaskImpl"></bean> <bean id="timerDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="timerTask"></property>
<property name="targetMethod" value="doTimerTask"></property>
</bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="timerDetail" />
<property name="startDelay" value="10000" />
<property name="repeatInterval" value="3000" />
</bean> <bean id="startTimer" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean> </beans>
由于结合quartz的原因,需要相关quartz的Jar包,可到官网下载:http://www.quartz-scheduler.org/
接口与其实现类与Spring定时器实现(一)相同
package com.charles.spring.service.impl;
import com.charles.spring.service.TimerTask;
public class TimerTaskImpl implements TimerTask {
@Override
public void doTimerTask() throws Exception {
System.out.println("Hello Timer");
}
}
测试类:
package com.charles.spring.handler; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Timer { public static void main(String[] args) { @SuppressWarnings({ "unused", "resource" })
ApplicationContext context = new ClassPathXmlApplicationContext("config/spring-config.xml");
try {
Thread.sleep(10*60*1000);
} catch (Exception e) { } } }
以上算是Spring定时器第二种实现方式了,不过、目前还有另一种配置,如下:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd "> <description>spring-configuration</description> <bean id="timerTask" class="com.charles.spring.service.impl.TimerTaskImpl"></bean> <bean id="timerDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="timerTask"></property>
<property name="targetMethod" value="doTimerTask"></property>
</bean> <bean id="cronTigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="timerDetail"></property>
<property name="cronExpression">
<value>0/2 * * * * ?</value>
</property>
</bean> <bean id="startTimer" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTigger" />
</list>
</property>
</bean> </beans>
此时、将原先配置文件中的
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="timerDetail" />
<property name="startDelay" value="10000" />
<property name="repeatInterval" value="3000" />
</bean>
修改为:
<bean id="cronTigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="timerDetail"></property>
<property name="cronExpression">
<value>0/2 * * * * ?</value>
</property>
</bean>
即可。
Spring定时器实现(二)的更多相关文章
- spring定时器(二)
此定时器可重置定时时间. 1. spring的定时器配置文件application.xml: <?xml version="1.0" encoding="UTF-8 ...
- spring定时器,定时器一次执行两次的问题
Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...
- spring定时器设置(转自:http://my.oschina.net/LvSantorini/blog/520049)
转自:http://my.oschina.net/LvSantorini/blog/520049<!-- MessageRequestTask类中包含了msgRequest方法,用于执行定时任务 ...
- java定时器,Spring定时器和Quartz定时器
一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...
- Spring定时器的使用方法
Spring定时器主要通过Quartz Cron表达式来实现定时任务,注解用法如下: # 每月的最后1天 @Scheduled(cron = "0 0 18 28–31 * ?") ...
- Spring 定时器Quartz的用法
Spring定时器Quartz的用法也很简单,需要引入quartz-all-1.5.2.jar java代码如下: package com.coalmine.desktop; import java. ...
- spring定时器,当遇见半小时的情况时
spring定时器遇见半小时的解决方法(这里只提供注解方式) @Scheduled(fixedRate=6000000)//每隔100分钟执行方法 fixedRate的值是毫秒
- Spring 定时器的使用
spring定时器应用 相关类: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean 配置定时远行方法 o ...
- spring定时器
本人小菜鸟一枚,今天在公司看到一段spring定时器配置,自己总结一下! <!-- 定义调用对象和调用对象的方法 --><bean id="jobtask9" c ...
随机推荐
- 关于angular-route后获取路由标签的一些问题
要实现angular路由,我们需要用到angular.js和angular-route.js 在接入网络的情况下,很多网站都可以下载到这个文件. 然后呢,将文件引入到你的HTML中,然后是基础格式 h ...
- Java中SimpleDateFormat用法详解
所有已实现的接口: Serializable, Cloneable SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本) ...
- [BZOJ2707]走迷宫
Description Morenan被困在了一个迷宫里.迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T.可惜的是,Morenan非常的脑小,他只会从一个点出发随机沿 ...
- 去除MyEclipse频繁弹出的Update Progress窗口
方法1: 1.关闭updating index Window => Preferences => Myeclipse Enterprise Workbench => Maven4My ...
- 关于MATLAB处理大数据坐标文件2017622
今天新提交了一次数据,总量达到10337个,本以为成绩会突飞猛进,没想到还是不如从前 但是已经找到人工鼠标轨迹的程序,有待完善,接下来兵分四路:找特征.决策树.完善人工轨迹程序,使其可以将生成的数据自 ...
- json转javascript对象
json转javascript对象var tpIdObj = eval("(" + tpid + ")");//json字符串转 对象var tpid = JS ...
- 马踏棋盘算法递归+回溯法实现 C语言
r为矩阵的行,c为矩阵的列 将结果输出到当前目录下的results.txt. 结果将给出:1.是否存在路径使马可以按要求走遍所有的方格: 2.解的总数: 3.程序执行的时间: #include< ...
- 使用dns批量管理普通主机名相关问题
1.dns配置 日常管理主机过程中,会有很多地方需要使用到主机名的,当主机非常多的时候,就不适合使用hosts来管理和同步的所有主机hosts了,这个时候就可以使用dns来管理主机名映射和变动 dns ...
- 如何在自己的网页上插入一个超链接,发起临时qq会话
1.先开通临时会话功能 打开网页http://shang.qq.com/v3/index.html
- JavaScript对象属性访问的两种方式
JavaScript对象属性访问的两种方式 object.attribute object["attribute"] 例如: var employees = [ { "f ...