准备工作 下载quartz和spring相应的包
1.编写测试类
public
class SimpleService implements Serializable {
private
static final
long serialVersionUID = 122323233244334343L;
private
static final Log logger = LogFactory.getLog(SimpleService.
class);
public
void testMethod1(){
//这里执行定时调度业务 便于显示明细添加些特殊符号
logger.info(
"testMethod1...@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@....1");
}
public
void testMethod2(){
logger.info(
"testMethod2....###################################################!!!!!!!!!!!!!!!!!!!!...2");
}
}
(1)配置spring的数据源
<bean id=
"dataSource"
class=
"org.apache.commons.dbcp.BasicDataSource">
<property name=
"driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name=
"url">
<value>jdbc:mysql:
//127.0.0.1:3306/going</value>
</property>
<property name=
"username">
<value>root</value>
</property>
<property name=
"password">
<value>root</value>
</property>
<property name=
"maxActive">
<value>100</value>
</property>
<property name=
"maxIdle">
<value>2</value>
</property>
<property name=
"maxWait">
<value>1200</value>
</property>
</bean>
<!--
<bean id=
"dataSource"
class=
"org.springframework.jndi.JndiObjectFactoryBean"
abstract=
"false">
<property name=
"jndiName">
<value>java:comp/env/jdbc/mysqlds</value>
</property>
</bean>
(2)配置spring和job的结合
<bean id=
"simpleService"
class=
"com.going.oa.quartz.example5.SimpleService">
</bean>
<bean name=
"quartzScheduler"
class=
"org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name=
"dataSource">
<
ref bean=
"dataSource"/>
</property>
<property name=
"applicationContextSchedulerContextKey" value=
"applicationContextKey"/>
<property name=
"configLocation" value=
"classpath:quartz_priority.properties"/>
<property name=
"triggers">
<list>
<
ref bean=
"trigger1"/>
<
ref bean=
"trigger2"/>
</list>
</property>
</bean>
<bean id=
"jobDetail1"
class=
"frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name=
"targetObject"
ref=
"simpleService"/>
<property name=
"targetMethod" value=
"testMethod1"/>
<property name=
"concurrent" value=
"false" />
</bean>
<bean id=
"trigger1"
class=
"org.springframework.scheduling.quartz.CronTriggerBean">
<property name=
"jobDetail"
ref=
"jobDetail1"/>
<property name=
"cronExpression" value=
"0/5 * * ? * * *"/>
</bean>
<bean id=
"jobDetail2"
class=
"frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name=
"targetObject"
ref=
"simpleService"/>
<property name=
"targetMethod" value=
"testMethod2"/>
<property name=
"concurrent" value=
"false" />
</bean>
<bean id=
"trigger2"
class=
"org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name=
"jobDetail"
ref=
"jobDetail2"/>
<property name=
"startDelay" value=
"1"/>
<property name=
"repeatCount" value=
"100"/>
<property name=
"repeatInterval" value=
"1000"/>
</bean>
</beans>
注意上面frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类在spring的jar包中不提供,spring提供的MethodInvokingJobDetailFactoryBean有个bug所以需要到网上去下载这俩个文件
http://jira.springframework.org/browse/SPR-3797
3编写测试类
package com.going.oa.quartz.example5;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* User: weiyong
* Date: 2010-3-23
* Time: 13:57:51
* To change this template use File | Settings | File Templates.
*/
public
class MainTest {
/**
* @param args
*/
public
static
void main(String[] args) {
ApplicationContext springContext =
new ClassPathXmlApplicationContext(
new String[]{
"classpath:applicationContext-resources.xml",
"classpath:applicationContext-quartz.xml"});
}
}
本文介绍如何使用Spring框架整合Quartz实现定时任务调度。通过配置数据源、定义任务和服务,创建了两个示例任务,分别设置为CronTrigger和SimpleTrigger触发方式。

被折叠的 条评论
为什么被折叠?



