一:前沿
最近在做一个定时的功能,就是在一定时间内查询订单,然后告诉用户未付款,已付款等消息通知,而且要做集群的功能,这个集群的功能是指,我部署两套代码,其中一个定时的功能在运行,另外一个就不要运行。这中集群在刚刚开始的时候我还是理解不了的,因为这种集群是站在代码的角度老看的吧!我的这篇博客就分两篇了,一篇是定时的功能,一篇是具有集成功能(http://www.cnblogs.com/wuhao1991/p/4332446.html)的
二:内容
所以在网上百度了下有关集成的操作,我用的是最新的quartz的包2.2.1的,maven配置如下吧
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-version}</version> </dependency>
接着就是配置文件的配置了,其实很多也是在网上百度的,照着配置,就如这个http://my.oschina.net/u/853107/blog/170774,这里面的定时功能就是可以的!我的配置如下(其实有两种写法的):
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="scheduTask" class="com.gsh.graduate.quartz.ScheduTask"></bean> <!-- 调用的对象和方法的指定 --> <bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="scheduTask"></property> <property name="targetMethod" value="doTask"></property> <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 --> <property name="concurrent" value="false"/> </bean> <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="jobTask" /> </property> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myJobTrigger" /> </list> </property> </bean>
<!--- 下面是另外一种写法-> <!-- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="cronExpression"> <value>0/5 * * * * ?</value> </property> <property name="jobDetail"> <bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="scheduTask"></property> <property name="targetMethod" value="doTask"></property> 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 <property name="concurrent" value="false"/> </bean> </property> </bean> </list> </property> </bean> --> </beans>
下面这个是定时操作的方法
package com.gsh.graduate.quartz; import java.io.Serializable; import java.util.Date; public class ScheduTask implements Serializable { private static final long serialVersionUID = 1L; public static void doTask(){ System.out.println("开始执行!!!"); System.out.println("执行时间:"+new Date()); System.out.println("执行结束"); } }
三:总结
对于spring结合quartz的定时是不难的,难得就是在集成上面,我弄了一天都没搞定,下一篇(http://www.cnblogs.com/wuhao1991/p/4332446.html)就记载我弄的集成,最近接触了好多新东西,集成这种还是第一次接触吧!努力吧!最近要谈试用,谈了下,又没音了,真是无语啊!