quartz通过数据库配置trigger

通过数据库和配置中心superdimond动态的修改trigger,也可以逻辑删除job。

Spring配置文件如下:

<bean id="" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
		  p:targetObject-ref="" p:targetMethod="" p:concurrent="true" />
或者

<bean id="priceTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject" ref="masterMsgsProceedManage" />
	<property name="targetMethod" value="processMqSkuPriceMsg" />
</bean>

只配置jobDetail,trigger信息通过数据库获得,或通过配置中心。

import com.github.diamond.client.PropertiesConfiguration;
import com.github.diamond.client.PropertiesConfigurationFactoryBean;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class SchedulerConfigFactory extends SchedulerFactoryBean{

    protected static PropertiesConfiguration config = PropertiesConfigurationFactoryBean.getPropertiesConfiguration();

    @Override
    public void afterPropertiesSet() throws Exception {
        initTriggers();
        initQuartzProperties();
        setWaitForJobsToCompleteOnShutdown(true);
        super.afterPropertiesSet();
    }

    void initTriggers(){
        Set<String> keys=config.getProperties().stringPropertyNames();
        List<Trigger>  list= new ArrayList<Trigger>();
        for(String keyName:keys){
            if(keyName.startsWith("job_")){
                SimpleTriggerImpl simpleTrigger=createSimpleTrigger(keyName,config.getLong(keyName));
                if(simpleTrigger!=null){
                    list.add(simpleTrigger);
                }
            }
        }
        Trigger[] triggers=new Trigger[list.size()];
        setTriggers(list.toArray(triggers));
    }

    void initQuartzProperties(){
        Properties quartzProperties = new Properties();
        quartzProperties.setProperty("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
        quartzProperties.setProperty("org.quartz.threadPool.threadCount",config.getString("org.quartz.threadPool.threadCount"));
        quartzProperties.setProperty("org.quartz.jobStore.misfireThreshold","1000");
        setQuartzProperties(quartzProperties);
    }

    SimpleTriggerImpl createSimpleTrigger(String jobName,long repeatInterval){
        try {
            JobDetail jobDetail=(JobDetailImpl) SpringBeanUtil.getBean(jobName);
            SimpleTriggerImpl simpleTrigger=new SimpleTriggerImpl();
            simpleTrigger.setName("trigger_"+jobName);
            simpleTrigger.setGroup(Scheduler.DEFAULT_GROUP);
            simpleTrigger.setJobKey(jobDetail.getKey());
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("jobDetail", jobDetail);
            simpleTrigger.setJobDataMap(jobDataMap);
            simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 2000));
            simpleTrigger.setRepeatInterval(repeatInterval);
            simpleTrigger.setRepeatCount(-1);
            return simpleTrigger;
        }catch (NoSuchBeanDefinitionException e){
            logger.error("NoSuchBeanDefinitionException,jobBeanName:" +jobName);
            return null;
        }
    }
}

监听

import com.github.diamond.client.event.ConfigurationEvent;
import com.github.diamond.client.event.ConfigurationListener;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.TriggerKey;
import org.quartz.impl.StdScheduler;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SchedulerConfigListener implements ConfigurationListener {

    @Override
    public void rescheduleJob(ConfigurationEvent event) {
        try {
            String configKey = event.getPropertyName();            
            String configValue = event.getPropertyValue().toString();
            StdScheduler scheduler=SpringUtil.getBean(StdScheduler.class);
            TriggerKey triggerKey = new TriggerKey(configKey,Scheduler.DEFAULT_GROUP);
            SimpleTriggerImpl trigger = (SimpleTriggerImpl) scheduler.getTrigger(triggerKey);
            trigger.setRepeatInterval(Long.valueOf(configValue));
            scheduler.rescheduleJob(triggerKey, trigger);
        } catch (Exception e) {
            log.info("监听配置修改异常:{}", e);
        }
    }
}

数据库实现

create table `project_config` (
  `config_id` int(11) not null auto_increment,
  `config_key` varchar(64) not null,
  `config_value` longtext not null,
  `config_type` int(1) not null,
  `config_desc` varchar(256) default null,
  `project_id` varchar(256) default null,
  `delete_flag` int(1) default '0',
  `opt_user` varchar(32) default null,
  `opt_time` datetime default null,
  primary key (`config_id`)
) engine=innodb auto_increment=11780 default charset=utf8

只支持SimpleTriggerImpl,可以添加功能支持CronTriggerImpl

官方Quartz API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值