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定时器实现(二)的更多相关文章

  1. spring定时器(二)

    此定时器可重置定时时间. 1. spring的定时器配置文件application.xml: <?xml version="1.0" encoding="UTF-8 ...

  2. spring定时器,定时器一次执行两次的问题

    Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...

  3. spring定时器设置(转自:http://my.oschina.net/LvSantorini/blog/520049)

    转自:http://my.oschina.net/LvSantorini/blog/520049<!-- MessageRequestTask类中包含了msgRequest方法,用于执行定时任务 ...

  4. java定时器,Spring定时器和Quartz定时器

    一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...

  5. Spring定时器的使用方法

    Spring定时器主要通过Quartz Cron表达式来实现定时任务,注解用法如下: # 每月的最后1天 @Scheduled(cron = "0 0 18 28–31 * ?") ...

  6. Spring 定时器Quartz的用法

    Spring定时器Quartz的用法也很简单,需要引入quartz-all-1.5.2.jar java代码如下: package com.coalmine.desktop; import java. ...

  7. spring定时器,当遇见半小时的情况时

    spring定时器遇见半小时的解决方法(这里只提供注解方式) @Scheduled(fixedRate=6000000)//每隔100分钟执行方法 fixedRate的值是毫秒

  8. Spring 定时器的使用

    spring定时器应用 相关类: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean 配置定时远行方法 o ...

  9. spring定时器

    本人小菜鸟一枚,今天在公司看到一段spring定时器配置,自己总结一下! <!-- 定义调用对象和调用对象的方法 --><bean id="jobtask9" c ...

随机推荐

  1. C++经典绘图工具EasyX

    EasyX简介 EasyX 在学习C语言时,很多同学抱怨说C只能写最简单的Demo程序,通过printf在屏幕上打印字符来验证代码.这样的编程很枯燥,一点没觉得自己在设计软件. EasyX是针对C++ ...

  2. sqlmap详细使用 [精简]

    1. 基础用法: 一下./sqlmap.py 在kali和backtrack中使用sqlmap的时候,直接用:sqlmap ./sqlmap.py -u “注入地址” -v 1 –dbs   // 列 ...

  3. 做电子商务的七个SEO技巧

    随着电子商务网站的迅速增加,为了适应不断增长的网络客户多样化需求.越来越多的人注意到,从网站上购买既节省了时间和金钱,因此电子商务社区越来越激烈.网站需要具有独特的设计风格和功能,而不是网站内容的不断 ...

  4. .NET和JAVA 反射对比

    反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等程序集内部的信息.使用反射可以看到一个程序集内部的接口.类.方法.字段.属性.特性等等信息.在System.Reflectio ...

  5. Step by Step 用Azure Automation 来开虚机(ARM)

    使用Azure Automation来自动化处理各种重复的耗时的云管理任务从而帮助云运维人员提升效率,帮助降低运营成本. 具体相关的介绍以及怎样利用Azure Automation来完成定期开关虚拟机 ...

  6. Vulkan Tutorial 18 重构交换链

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 现在我们已经成功的在屏幕上绘制出三角形,但是在某些情况下, ...

  7. R语言重要数据集分析研究——  数据集本身的分析技巧

    数据集本身的分析技巧           作者:王立敏           文章来源:网络 1.数据集 数据集,又称为资料集.数据集合或资料集合,是一种由数据所组成的集合. Data set(或dat ...

  8. 百度地图 js api 实现 line 居中显示

    项目中有个需求需要在百度地图的中心显示画的线,以前用过mapPanto这个方法,传入坐标就可以将地图平移到这个坐标,不过不知道如何获取线的中心点,看了别人的代码,有以下两个函数可以实现这个功能 get ...

  9. celery的使用

    1.celery的任务调度 # -*- coding: utf-8 -*- import threading from bs4 import BeautifulSoup from tornado im ...

  10. [leetcode-565-Array Nesting]

    A zero-indexed array A consisting of N different integers is given. The array contains all integers ...