第二种实现 方式
package com.shengyidi.app.task;
public class QuartzTestS {
public void start() {
testMethod();
}
private void testMethod() {
System.out.println("\n\n_______________" + Thread.currentThread().getName() + "_start________________________");
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
try {
Thread.currentThread().sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("\n\n_______________" + Thread.currentThread().getName() + "_end________________________");
}
}
xml配置
<bean id="quartzTestS" class="com.shengyidi.app.task.QuartzTestS"> </bean> <bean id="quartzTestSTimer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="quartzTestS" /> </property> <property name="targetMethod"> <value>start</value> </property> <property name="concurrent" value="false" /> </bean> <bean id="test2" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="quartzTestSTimer" /> </property> <property name="cronExpression"> <value>0 /1 * * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="test2" /> </list> </property> </bean>
结果数据
_______________org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1_start________________________
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1 0
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1 1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1 2
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1 3
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1 4
_______________org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1_end________________________
_______________org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2_start________________________
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2 0
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2 1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2 2
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2 3
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2 4
_______________org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2_end________________________
_______________org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3_start________________________
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3 0
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3 1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3 2
总结:
第二种是spring 提供的,第一种是我想的
第一种比第二种快很多。建议使用第一种