第一种:为普通java类中的某个方法配置跑批任务
1.定义要跑批的类和方法:
package com.xy.utils.quartz;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 测试定时器类
* @author javaw
*
*/
public class TestQuartz {
public static Logger logger = LoggerFactory.getLogger(TestQuartz.class);
public void TestMethod(){
logger.info("Auto Execute TestMethod start! Date={}" ,new DateTime().toString("YYYY-MM-DD HH:mm:ss" ));
logger.info("**********测试跑批类************");
logger.info("Auto Execute TestMethod end! Date={}" ,new DateTime().toString("YYYY-MM-DD HH:mm:ss" ));
}
}
2.配置Spring定时器让quartz自动执行testMethod方法:
<!-- 实例化bean -->
<bean id= "testMethodQuartz" class ="com.xy.utils.quartz.TestQuartz"/>
<!-- 配置MethodInvokingJobDetailFactoryBean -->
<bean id= "testTaskMethod"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testMethodQuartz"/>
<property name="targetMethod" value="TestMethod"/>
<property name="concurrent" value="false"/>
</bean>
<!-- 配置定时表达式 -->
<bean id= "testTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail" ref="testTaskMethod" />
<!-- 每一分钟执行一次 -->
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>
<!-- 配置调度工厂 -->
<bean id= "testSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers" >
<list>
<ref bean="testTaskTrigger" />
</list>
</property>
</bean>
3.写测试方法进行调用(不用启动tomcat):
public static void main(String[] args) {
SchedulerFactoryBean schedulerFactoryBean = (SchedulerFactoryBean) SpringUtilsFromClassPathXml.getBean( "testSchedulerFactoryBean");
//启动调度器
schedulerFactoryBean.start();
}
第二种:为继承QuartzJobBean的java类配置跑批任务
1.写java类继承QuartzJobBean,重写executeInternal 方法:
package com.xy.utils.quartz;
import org.joda.time.DateTime;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
* 测试继承QuartzJobBean的java类配置定时器
* @author javaw
*
*/
public class TestQuartzExtendsJobBean extends QuartzJobBean{
private static Logger logger = LoggerFactory.getLogger(TestQuartzExtendsJobBean. class);
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
logger.info("Auto Execute TestQuartzExtendsJobBean start! Date={}",new DateTime().toString( "YYYY-MM-DD HH:mm:ss"));
logger.info("**********"+content.getMergedJobDataMap().get("descString")+"************");
logger.info("Auto Execute TestQuartzExtendsJobBean end! Date={}",new DateTime().toString( "YYYY-MM-DD HH:mm:ss"));
}
}
2.配置文件:
<!--
第二种:为继承Quartz的java类实现跑批
**********1.JobDetailBean
**********2.CronTriggerBean
**********3.SchedulerFactoryBean
-->
<bean id= "testJobDetailBean"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="name" value="exampleJob" />
<property name="jobClass"
value="com.xy.utils.quartz.TestQuartzExtendsJobBean"/>
<!-- 可以封装各种数据到JobExecutionContext里 -->
<property name="jobDataAsMap">
<map>
<entry key="descString" value="测试跑批"/>
</map>
</property>
</bean >
<bean id= "testTaskTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testJobDetailBean" />
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>
<bean id= "testSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers" >
<list>
<ref bean="testTaskTrigger" />
</list>
</property>
</bean>