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 ...
随机推荐
- springboot问题:解决异常Unable to start embedded container;
使用eclipse创建springboot练习时,当主函数与控制器同时写在同一个类时,启动项目正常运行,而当把主函数单独放在一个类中时,无论是与控制器同包还是控制器所在的包是其子包,都报: org.s ...
- React-Native集成到已有项目中的总结
安装Python 从官网下载并安装python 2.7.x(3.x版本不行) 安装node.js 从官网下载node.js的官方V6.X.X版本或更高版本.安装完成后检测是否安装成功:node -v ...
- Java web中常见编码乱码问题(二)
根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析: 2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码, ...
- python selenium-webdriver 通过cookie登陆(十一)
上节介绍了浏览器的常用方法,涉及到了cookie的使用,本节介绍一下如何利用cookie进行登陆系统,这里使用到了request模块,我们首先利用request模块,请求登陆地址进行登陆,登陆成功以后 ...
- centos6.7下安装mysql5.6.22同时解决中文乱码问题
1.下载 http://dev.mysql.com/downloads/mysql/ 或者使用wget下载: wget http://dev.mysql.com/get/Downloads/MySQL ...
- 技术分析 | 新型勒索病毒Petya如何对你的文件进行加密
6月27日晚间,一波大规模勒索蠕虫病毒攻击重新席卷全球. 媒体报道,欧洲.俄罗斯等多国政府.银行.电力系统.通讯系统.企业以及机场都不同程度的受到了影响. 阿里云安全团队第一时间拿到病毒样本,并进行了 ...
- Swift数组的存取与修改
对数组的存取与修改可以通过数组的方法和属性来进行,或者使用数组的下标语法. 要知道数组中元素的数量,可以查看它的只读属性count: println("The shopping list c ...
- Example005控制弹出窗口居中显示
<!-- 实例005控制弹出窗口居中显示 --> <head> <meta charset="UTF-8"> </head> < ...
- pdf.js在国际化的时候,显示不了中文的解决办法
在项目中使用了pdf实现在线预览功能,开始工具栏中一直都是英文的,在view.js中设置了也不起作用,偶然发现了问题所在 当我把网站发布到iis上的时候,用google浏览器的审查元素功能的审核发现j ...
- Java IO在实际项目开发中应用
IO是java绕不过去的槛,在开发中io无处不在, 正如同 世界上本没有路,java io写多了,也就知道了大体是什么意思,在读完thinking in java 感觉就更清晰了,结合具体的业务场景, ...