spring动态定时器

<!-- 被执行类 -->
<bean id="autoBillQuartz" class="com.util.task.AutoBillQuartz" init-method="initTime">
    <property name="scheduler" ref="systemDescsionScheduler" />
    <property name="p2pCreditAppInfoLogic" ref="p2pCreditAppInfoLogic"></property>
</bean>

<!-- 将autoQuartzJob注入到job中 -->
<bean id="autoQuartzJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="autoBillQuartz" />
    <property name="targetMethod" value="doSomething" />
    <property name="concurrent" value="false" />
</bean>

<!-- 将job注入到定时触发器 默认5分钟一次-->
<bean id="autoBillTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
    <property name="jobDetail" ref="autoQuartzJob" />  
    <property name="cronExpression">  
        <value>0 0/5 * * * ?</value>   
  </property>  
</bean>
<!-- 系统定时任务工厂类 -->
<bean id="systemDescsionScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="autoBillTrigger"/>
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">4</prop>
</props>
</property>
</bean>
package com.util.task;


import java.text.ParseException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.CronTriggerBean;
import com.crfchina.aps.foundation.common.common.LogUtils;
import com.crfchina.p2p.app.entity.P2pCreditAssignSize;
import com.crfchina.p2p.app.service.logic.P2pCreditAppInfoLogicInterface;

/**
* 处理动态自动分单
*
* @author xlin
*
*/
public class AutoBillQuartz {
private Scheduler scheduler;
private P2pCreditAppInfoLogicInterface p2pCreditAppInfoLogic;
private static CronTriggerBean trigger = null;
private static boolean auto;//开关
private Log log = LogUtils.getLog(AutoBillQuartz.class.getName());

public void initTime() {
try {
log.info("审核分单初始化!");
P2pCreditAssignSize cas = p2pCreditAppInfoLogic
.queryP2pCreditAssignSize();
if (cas != null) {
//获取数据库分单时间
String allotTime = cas.getAllotTime();
if (!StringUtils.isEmpty(allotTime)) {
resetJob(allotTime);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("审核分单初始化异常!",e);
}
}

/**
* 获取时间
*
* @param allotTime
* @return
*/
private String getAllotTime(String allotTime) {
if (allotTime.equals("1")) {
// 每五分钟一次
allotTime = "0 0/5 * * * ?";
} else if (allotTime.equals("2")) {
// 每10分钟一次
allotTime = "0 0/10 * * * ?";
} else if (allotTime.equals("3")) {
// 每20分钟一次
allotTime = "0 0/20 * * * ?";
} else if(allotTime.equals("4")){
allotTime="4";
}else {
allotTime = "0 0/5 * * * ?";
}
return allotTime;
}

/**
* 执行定时任务
*/
public void doSomething() {
log.info("审核分单!");
try {
p2pCreditAppInfoLogic.morningAutoAssignCreditAppInfo();
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("自动分单异常!"+e.getMessage(), e);
e.printStackTrace();
}
}

/**
* 设置定时任务时间
*
* @param cronExpression
*/
public void resetJob(String cronExpression) {
try {
if (trigger == null) {
trigger = (CronTriggerBean) scheduler.getTrigger("autoBillTrigger",
Scheduler.DEFAULT_GROUP);
}
cronExpression = getAllotTime(cronExpression);
String originConExpression = trigger.getCronExpression();
// 如果相等,则表示用户并没有重新设定数据库中的任务时间,这种情况不需要重新rescheduleJob
if(cronExpression.equals("4")){
stopTrigger();
}else{
startTrigger(cronExpression);
if (!originConExpression.equalsIgnoreCase(cronExpression)) {
trigger.setCronExpression(cronExpression);
scheduler.rescheduleJob("autoBillTrigger", Scheduler.DEFAULT_GROUP,
trigger);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("审核分单设置频率失败!",e);
throw new RuntimeException("审核分单设置频率失败!");

}

}

/**
* 关闭定时
* @throws SchedulerException
*/
private void stopTrigger() throws SchedulerException {
scheduler.pauseJob("autoBillTrigger",Scheduler.DEFAULT_GROUP);
scheduler.pauseTrigger("autoBillTrigger", Scheduler.DEFAULT_GROUP);
auto=true;
}

/**
* 开启定时
* @param cronExpression
* @throws ParseException
* @throws SchedulerException
*/
private void startTrigger(String cronExpression) throws ParseException,
SchedulerException {
if(auto){
trigger.setCronExpression(cronExpression);
scheduler.rescheduleJob("autoBillTrigger", Scheduler.DEFAULT_GROUP,trigger);
}
}

public Scheduler getScheduler() {
return scheduler;
}

public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}

public P2pCreditAppInfoLogicInterface getP2pCreditAppInfoLogic() {
return p2pCreditAppInfoLogic;
}

public void setP2pCreditAppInfoLogic(
P2pCreditAppInfoLogicInterface p2pCreditAppInfoLogic) {
this.p2pCreditAppInfoLogic = p2pCreditAppInfoLogic;
}

}

猜你喜欢

转载自xianlincai.iteye.com/blog/2304856