elastic job 动态设置定期职务,elasticjob动态

elastic job 动态设置定时任务,elasticjob动态

  1. 版本

     <!-- import elastic-job lite core -->
     <dependency>
       <groupId>com.dangdang</groupId>
       <artifactId>elastic-job-lite-core</artifactId>
       <version>2.1.3</version>
     </dependency>
    
     <!-- import other module if need -->
     <dependency>
       <groupId>com.dangdang</groupId>
       <artifactId>elastic-job-lite-spring</artifactId>
       <version>2.1.3</version>
     </dependency>
    
  2. 配置


        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:reg="http://www.dangdang.com/schema/ddframe/reg"
        xmlns:job="http://www.dangdang.com/schema/ddframe/job"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://www.dangdang.com/schema/ddframe/reg
                         http://www.dangdang.com/schema/ddframe/reg/reg.xsd
                         http://www.dangdang.com/schema/ddframe/job
                         http://www.dangdang.com/schema/ddframe/job/job.xsd
                         ">
     <!--configure registry center -->
     <reg:zookeeper id="regCenter" server-lists="${job.registry.address}" namespace="${job.namespace}" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
    
     <!--configure job -->
     <!--<job:simple id="myElasticJob" class="com.zhuanche.util.MyElasticJob" registry-center-ref="regCenter" cron="0/10 * * * * ?"   sharding-total-count="1" sharding-item-parameters="0=A" />-->
    

3.1 代码

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;        

  String cron = DateUtil.getCron(activityEndTime);
            logger.info("【定时任务执行的时间】cron={}",cron);
            int shardingTotalCount = 1;
            String jobName = UUID.randomUUID().toString() + "-" + numprizeBaseId;
            JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(jobName, cron, shardingTotalCount).build();
            SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, DynamicElasticJob.class.getCanonicalName());
            JobScheduler jobScheduler = new JobScheduler(zookeeperRegistryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).build());
            try {
                jobScheduler.init();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("定时任务创建失败");
            }

3.2 工具类

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by admin on 2017/10/23.
 */
public class DateUtil {
    private static final String CRON_DATE_FORMAT = "ss mm HH dd MM ? yyyy";

    /***
     * @param date 时间
     * @return cron类型的日期
     */
    public static String getCron(final Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(CRON_DATE_FORMAT);
        String formatTimeStr = "";
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }
}

 

job 动态设置定时任务,elasticjob动态 1.
版本 !– import elastic-job lite core — dependency groupId com.dangdang
/ groupId artifactId elastic-job-lite-core / a…

scheduler定时调度系统是大多行业项目都需要的,传统的spring-job模式,个人感觉已经out了,因为存在很多的问题,特别是定时调度的追加、修改、删除等,需要修改xml,xml的配置生效无非是热部署灰度发布方案或者直接停止、重启服务器,完全不能做到自动启动、修复方式。

什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定)。

提醒:可以对应用进行集群部署,在对定时调度配置时可以使用集群方式或者单边配置应用方式,今天讲解的是使用spring4+scheduler实现定时调度,闲话少说,直接把步骤记录下来:

这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文件,但总需要重新启动web服务啊,研究了下Quartz在Spring中的动态定时,发现<bean
id=”cronTrigger”
class=”org.springframework.scheduling.quartz.CronTriggerBean”
>

 

         <property
name=”jobDetail” ref=”schedulerJobDetail”/>

  1. 在项目的pom.xml文件中引入quartz的jar包,如下:


    lt;dependency>
    org.quartz-scheduler
    quartz
    1.8.5
    lt;/dependency>

  2. 定义quartz的配置文件spring-context-quartz.xml:


     xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="  
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"  
     default-lazy-init="false">  
     <!-- 调度器 -->  
     <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   
        <!-- 通过applicationContextSchedulerContextKey属性配置spring上下文 -->      
         <property name="applicationContextSchedulerContextKey" value="applicationContext" />  
     </bean>    
     <!--加载数据库任务-->  
     <bean id="jobService" class="com.ml.honghu.job.service.JobService" init-method="loadJob" />  
    

  3.  在项目的web.xml文件中引入spring-context-quartz.xml配置文件

    classpath*:spring-context-quartz.xml

  4. 定义job实体对象

    public class Job{

     private static final long serialVersionUID = 1L;  
    
     /** 
      * 任务执行周期cron表达式 
      */  
     public static int EXECYCLE_CRON = 2;  
     /** 
      * 任务执行周期自定义 
      */  
     public static int EXECYCLE_DEFINE = 1;  
     /** 
      * 执行周期-分钟 
      */  
     public static int EXECYCLE_MINUTE = 1;  
     /** 
      * 执行周期-小时 
      */  
     public static int EXECYCLE_HOUR = 2;  
     /** 
      * 执行周期-日 
      */  
     public static int EXECYCLE_DAY = 3;  
     /** 
      * 执行周期-月 
      */  
     public static int EXECYCLE_WEEK = 4;  
     /** 
      * 执行周期-月 
      */  
     public static int EXECYCLE_MONTH = 5;  
    

         <property
name=”cronExpression”>

    private String jobType;     // 任务类型(1首页静态化、2栏目页静态化、3内容页静态化、4采集、5分发)  
    private String jobName;     // 任务名称  
    private String jobClass;        // 任务类  
    private String execycle;        // 执行周期分类(1非表达式 2 cron表达式)  
    private String dayOfMonth;      // 每月的哪天  
    private String dayOfWeek;       // 周几  
    private String hour;        // 小时  
    private String minute;      // 分钟  
    private String intervalHour;        // 间隔小时  
    private String intervalMinute;      // 间隔分钟  
    private String jobIntervalUnit;     // 1分钟、2小时、3日、4周、5月  
    private String cronExpression;      // 规则表达式  
    private String isEnable;        // 是否启用  

    public Job() {  
        super();  
    }  

    public Job(String id){  
        super(id);  
    }  

    @Length(min=1, max=1, message="任务类型(1首页静态化、2栏目页静态化、3内容页静态化、4采集、5分发)长度必须介于 1 和 1 之间")  
    public String getJobType() {  
        return jobType;  
    }  

    public void setJobType(String jobType) {  
        this.jobType = jobType;  
    }  

    @Length(min=1, max=255, message="任务名称长度必须介于 1 和 255 之间")  
    public String getJobName() {  
        return jobName;  
    }  

    public void setJobName(String jobName) {  
        this.jobName = jobName;  
    }  

    @Length(min=1, max=255, message="任务类长度必须介于 1 和 255 之间")  
    public String getJobClass() {  
        return jobClass;  
    }  

    public void setJobClass(String jobClass) {  
        this.jobClass = jobClass;  
    }  

    @Length(min=1, max=1, message="执行周期分类(1非表达式 2 cron表达式)长度必须介于 1 和 1 之间")  
    public String getExecycle() {  
        return execycle;  
    }  

    public void setExecycle(String execycle) {  
        this.execycle = execycle;  
    }  

    @Length(min=0, max=11, message="每月的哪天长度必须介于 0 和 11 之间")  
    public String getDayOfMonth() {  
        return dayOfMonth;  
    }  

    public void setDayOfMonth(String dayOfMonth) {  
        this.dayOfMonth = dayOfMonth;  
    }  

    @Length(min=0, max=1, message="周几长度必须介于 0 和 1 之间")  
    public String getDayOfWeek() {  
        return dayOfWeek;  
    }  

    public void setDayOfWeek(String dayOfWeek) {  
        this.dayOfWeek = dayOfWeek;  
    }  

    @Length(min=0, max=11, message="小时长度必须介于 0 和 11 之间")  
    public String getHour() {  
        return hour;  
    }  

    public void setHour(String hour) {  
        this.hour = hour;  
    }  

    @Length(min=0, max=11, message="分钟长度必须介于 0 和 11 之间")  
    public String getMinute() {  
        return minute;  
    }  

    public void setMinute(String minute) {  
        this.minute = minute;  
    }  

    @Length(min=0, max=11, message="间隔小时长度必须介于 0 和 11 之间")  
    public String getIntervalHour() {  
        return intervalHour;  
    }  

    public void setIntervalHour(String intervalHour) {  
        this.intervalHour = intervalHour;  
    }  

    @Length(min=0, max=11, message="间隔分钟长度必须介于 0 和 11 之间")  
    public String getIntervalMinute() {  
        return intervalMinute;  
    }  

    public void setIntervalMinute(String intervalMinute) {  
        this.intervalMinute = intervalMinute;  
    }  

    @Length(min=0, max=1, message="1分钟、2小时、3日、4周、5月长度必须介于 0 和 1 之间")  
    public String getJobIntervalUnit() {  
        return jobIntervalUnit;  
    }  

    public void setJobIntervalUnit(String jobIntervalUnit) {  
        this.jobIntervalUnit = jobIntervalUnit;  
    }  

    @Length(min=0, max=255, message="规则表达式长度必须介于 0 和 255 之间")  
    public String getCronExpression() {  
        return cronExpression;  
    }  

    public void setCronExpression(String cronExpression) {  
        this.cronExpression = cronExpression;  
    }  

    @Length(min=1, max=1, message="是否启用长度必须介于 1 和 1 之间")  
    public String getIsEnable() {  
        return isEnable;  
    }  

    public void setIsEnable(String isEnable) {  
        this.isEnable = isEnable;  
    }  

}  

            
<value>0/10 * * * * ?</value>

  1. 编写quartz的jobServvice类:

    package com.ml.honghu.job.service;

    import java.text.ParseException;
    import java.util.List;
    import java.util.UUID;

    import org.quartz.CronTrigger;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import com.ml.honghu.StringUtils;
    import com.ml.honghu.common.persistence.Page;
    import com.ml.honghu.common.service.CrudService;
    import com.ml.honghu.job.dao.JobDao;
    import com.ml.honghu.job.entity.Job;

    /**

    • 定时调度任务Service
    • @author honghu
      */
      @Service
      @Transactional(readOnly = true)
      public class JobService extends CrudService {

      @Autowired
      private JobDao jobDao;

      private Logger logger = LoggerFactory.getLogger(getClass());

      public Job get(String id) {

       return super.get(id);  
      

      }

      public List findList(Job job) {

       return super.findList(job);  
      

      }

      public Page findPage(Page page, Job job) {

       return super.findPage(page, job);  
      

      }

      @Transactional(readOnly = false)
      public void save(Job job) {

       super.save(job);  
       // 启用则启动任务  
       if (StringUtils.equals("1", job.getIsEnable())) {  
           startTask(job, job.getId());  
       }  
      

      }

      @Transactional(readOnly = false)
      public void update(Job job) {

       //结束定时调度  
       endTask(job.getId());  
      
       job.preUpdate();  
       jobDao.update(job);  
      
       // 启用则启动任务  
       if (StringUtils.equals("1", job.getIsEnable())) {  
           startTask(job, job.getId());  
       }  
      

      }

      @Transactional(readOnly = false)
      public void delete(Job job) {

       //结束任务  
       endTask(job.getId());  
      
       super.delete(job);  
      

      }

      /**

      • 系统初始加载任务
        */
        public void loadJob() throws Exception {
        List jobList = this.findList(new Job());
        if (null != jobList && jobList.size() > 0) {

         for (int i = 0; i < jobList.size(); i++) {  
             Job job = jobList.get(i);  
             // 任务开启状态 执行任务调度  
             if (StringUtils.equals("1", job.getIsEnable())) {  
                 try {  
                     JobDetail jobDetail = new JobDetail();  
                     // 设置任务名称  
                     if (StringUtils.isNotBlank(job.getId())) {  
                         jobDetail.setName(job.getId());  
                     } else {  
                         UUID uuid = UUID.randomUUID();  
                         jobDetail.setName(uuid.toString());  
                         job.setId(uuid.toString());  
                     }  
                     jobDetail.setGroup(Scheduler.DEFAULT_GROUP);  
                     // 设置任务执行类  
                     jobDetail.setJobClass(getClassByTask(job.getJobClass()));  
                     // 添加任务参数  
                     CronTrigger cronTrigger = new CronTrigger("cron_" + i, Scheduler.DEFAULT_GROUP,  
                             jobDetail.getName(), Scheduler.DEFAULT_GROUP);  
        
                     cronTrigger.setCronExpression(getCronExpressionFromDB(job.getId()));  
                     // 调度任务  
                     scheduler.scheduleJob(jobDetail, cronTrigger);  
                 } catch (SchedulerException e) {  
                     logger.error("JobService SchedulerException", e);  
                 } catch (ClassNotFoundException e) {  
                     logger.error("JobService ClassNotFoundException", e);  
                 } catch (Exception e) {  
                     logger.error("JobService Exception", e);  
                 }  
             }  
         }  
        

        }
        }

        /**

      • @param taskClassName
      • 任务执行类名
      • @return
      • @throws ClassNotFoundException
        */
        @SuppressWarnings(“rawtypes”)
        private Class getClassByTask(String taskClassName) throws ClassNotFoundException {
        return Class.forName(taskClassName);
        }

        public String getCronExpressionFromDB(String id) throws Exception {
        // 设置任务规则
        Job job = this.get(id);
        if (null != job) {

         if (Job.EXECYCLE_CRON == Integer.parseInt(job.getExecycle())) {  
             return job.getCronExpression();  
         } else {  
             Integer execycle = Integer.parseInt(job.getJobIntervalUnit());  
             String excep = "";  
             if (execycle.equals(Job.EXECYCLE_MONTH)) {  
                 excep = "0  " + job.getMinute() + " " + job.getHour() + " " + job.getDayOfMonth() + " * ?";  
             } else if (execycle.equals(Job.EXECYCLE_WEEK)) {  
                 excep = "0  " + job.getMinute() + " " + job.getHour() + " " + " ? " + " * " + job.getDayOfWeek();  
             } else if (execycle.equals(Job.EXECYCLE_DAY)) {  
                 excep = "0  " + job.getMinute() + " " + job.getHour() + " " + " * * ?";  
             } else if (execycle.equals(Job.EXECYCLE_HOUR)) {  
                 excep = "0 0 */" + job.getIntervalHour() + " * * ?";  
             } else if (execycle.equals(Job.EXECYCLE_MINUTE)) {  
                 excep = "0  */" + job.getIntervalMinute() + " * * * ?";  
             }  
             return excep;  
         }  
        

        }
        return “”;
        }

        private void startTask(Job job, String id) {
        try {

         String cronExpress = getCronExpressionFromDB(id);  
         if (StringUtils.isNotEmpty(cronExpress) && cronExpress.indexOf("null") == -1) {  
             JobDetail jobDetail = new JobDetail();  
             jobDetail.setName(id);  
             jobDetail.setGroup(Scheduler.DEFAULT_GROUP);  
             jobDetail.setJobClass(getClassByTask(job.getJobClass()));  
             CronTrigger cronTrigger = new CronTrigger("cron_" + id, Scheduler.DEFAULT_GROUP, jobDetail.getName(),  
                     Scheduler.DEFAULT_GROUP);  
             cronTrigger.setCronExpression(cronExpress);  
             scheduler.scheduleJob(jobDetail, cronTrigger);  
         }  
        

        } catch (ParseException e) {

         logger.error("JobService ParseException", e);  
        

        } catch (Exception e) {

         logger.error("JobService Exception", e);  
        

        }
        }

        private void endTask(String id) {
        try {

         scheduler.deleteJob(id, Scheduler.DEFAULT_GROUP);  
        

        } catch (SchedulerException e) {

         logger.error("JobService endTask", e);  
        

        }
        }

        @Autowired
        private Scheduler scheduler;

      }

        
</property>

6.
编写相关job的Controller、dao、dao.xml我这边就不写了,其实就是对数据的增删改查操作

   
 中cronExpression是关键,如果可以动态设置cronExpression的值,也就说如果我们可以直接调用CronTriggerBean中设置cronExpression的方法,就可以顺利解决问题了。

  1. 启动项目验证quartz是否成功:

熟悉1的朋友可以跳过不看,下面2、3是动态定时任务的具体实现。

图片 1

 

图片 2

1. Quartz在Spring中的简单配置

资料和源码来源图片 3

Spring配置文件:

    <bean
id=”schedulerJobDetail”
class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>

        <property
name=”targetObject” ref=”scheduleInfoAction”/>

        <property
name=”targetMethod” value=”simpleJobTest”/>

        <property
name=”concurrent” value=”false”/>

   
</bean>

    <bean
id=”cronTrigger”
class=”org.springframework.scheduling.quartz.CronTriggerBean”
>

         <property
name=”jobDetail” ref=”schedulerJobDetail”/>

         <property
name=”cronExpression“>

            
<value>0/10 * * * * ?</value>

        
</property>

    
</bean>

    <bean
id=”schedulerFactory”
class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>

        <property
name=”triggers”>

           
<list>

                <ref
local=”cronTrigger”/>

           
</list>

       
</property>

</bean>

 

在上面的配置中设定了

① targetMethod: 指定需要定时执行scheduleInfoAction中的simpleJobTest()方法


concurrent:对于相同的JobDetail,当指定多个Trigger时,
很可能第一个job完成之前,第二个job就开始了。指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。

③ cronExpression:0/10 *
* * * ?表示每10秒执行一次,具体可参考附表。


triggers:通过再添加其他的ref元素可在list中放置多个触发器。

 

scheduleInfoAction中的simpleJobTest()方法

注意:此方法没有参数,如果scheduleInfoAction有两个方法simpleJobTest()和simpleJobTest(String
argument),则spring只会去执行无参的simpleJobTest().

public
void simpleJobTest() { 

        log.warn(“uh
oh, Job is scheduled !'” + “‘ Success…”);

    }

 

2.Quartz在Spring中动态设置cronTrigger方法一

Spring配置文件:

<bean
id=”scheduleInfoAction”
class=”com.lively.happyoa.jobs.webapp.action.ScheduleInfoAction”>

        <property
name=”scheduler” ref=”schedulerFactory”/>

        <property
name=”scheduleInfoManager” ref=”scheduleInfoManager”/>

   
</bean>

    <bean
id=”schedulerJobDetail”
class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>

        <property
name=”targetObject” ref=”scheduleInfoAction”/>

        <property
name=”targetMethod” value=”reScheduleJob“/>

        <property
name=”concurrent” value=”false”/>

   
</bean>

    <bean
id=”cronTrigger
class=”org.springframework.scheduling.quartz.CronTriggerBean”
>

         <property
name=”jobDetail” ref=”schedulerJobDetail”/>

         <property
name=”cronExpression“>

            
<value>0/10 * * * * ?</value>

发表评论

电子邮件地址不会被公开。 必填项已用*标注