首页 > 代码库 > spring整合quartz
spring整合quartz
Quartz 介绍
实战
第一步 :spring、quartz 相应的jar包,添加到项目中(需要的call me)
/WEB-INF/lib/quartz-2.2.1.jar
以及spring的一些必要包
<!-- 包含quartz定时任运行方式 --> <import resource="classpath:applicationContext-quartz.xml"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 6. 配置QUARTZ -->
<!-- 集成方式:JobDetailFactoryBean,并且任务类需要继承QuartzJobBean-->
<!-- 定义jobDetail -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- durability 表示任务完成之后是否依然保留到数据库,默认false -->
<property name="durability" value="http://www.mamicode.com/true" />
<!-- 目标类 /wmuitp/src/test/SpringQuartz.java-->
<property name="jobClass" value="http://www.mamicode.com/blog.quartz.SpringQuartzTest"></property>
<!-- 在这个例子中,jobDataAsMap没有用,此目标类中接受的参数 ,若参数为service,则可以在此进行参数配置,类似struts2 -->
<!--
<property name="jobDataAsMap">
<map>
<entry key="service"><value>simple is the beat</value></entry>
</map>
</property>
-->
</bean>
<bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- durability 表示任务完成之后是否依然保留到数据库,默认false -->
<property name="durability" value="http://www.mamicode.com/true" />
<!-- 目标类 /wmuitp/src/test/SpringQuartz.java-->
<property name="jobClass" value="http://www.mamicode.com/blog.quartz.SpringQuartzTest2"></property>
</bean>
<!-- 定义simpleTrigger触发器 -->
<!--
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="jobDetail"></property>
<property name="repeatCount">
<value>8</value>
</property>
<property name="repeatInterval">
<value>1000</value>
</property>
<property name="startDelay">
<value>4</value>
</property>
</bean>
-->
<!-- 另一种触发器是CornTrigger -->
<bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<!-- 每个4秒触发 -->
<property name="cronExpression" value="http://www.mamicode.com/0/4 * * * * ?"/>
</bean>
<!-- 另一种触发器是CornTrigger2 -->
<bean id="cornTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail2"/>
<!-- 每个10秒触发 -->
<property name="cronExpression" value="http://www.mamicode.com/0/10 * * * * ?"/>
</bean>
<!-- 定义核心调度器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cornTrigger"/>
</property>
</bean>
<!-- 定义核心调度器2 -->
<bean id="scheduler2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="cornTrigger2"/>
</property>
</bean>
</beans>
#目标类
<property name="jobClass" value="http://www.mamicode.com/test.SpringQuartzTest"></property>
下面第三步:编写目标类
package test; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class SpringQuartzTest extends QuartzJobBean { /*业务实现*/ public void work() { System.out.println("执行调度任务:"+new Date()); } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { this.work(); } }
spring整合quartz