使用quartz框架可以完成定时任务处理即Job,比如有时候我们设置1个Job每隔5分钟执行1次,后来会发现当前Job启动的时候上一个Job还没有运行结束,这显然不是我们期望的,此时可以设置quartz中的参数,来确保Job不并发执行
1. quartz未与Spring结合
//可以通过在实现Job接口的类上加注解的方式 @DisallowConcurrentExecution public class TestJob implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("test"); } }
2. quartz与spring集成,设置配置文件concurrent参数为false
<bean id="fetchOneJob" class="hm.com.job.FetchDataFromOrgJDBC"/> <bean id="fetchOneJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="concurrent" > <value>false</value> </property> <property name="targetObject"> <ref bean="fetchOneJob" /> </property> <property name="targetMethod"> <value>work</value> </property> </bean> <bean id="fetchOneJobTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="fetchOneJobTask" /> </property> <property name="cronExpression"> <value>0 0/3 * * * ?</value> </property> </bean> <bean id="startFetchOneJobTime" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="fetchOneJobTime" /> </list> </property> </bean>