• quart动态执行定时任务


    今天有个需求,前端可以将定时任务自定义保存到数据库,每天根据查询数据库来执行任务。

    其实不用动态也是可以实现,但是。也是想试试动态执行定时任务看看怎么样的。

    (1)建立一个QuartzManage类

    package com.bsoft.homecare.schedule;
    
    import org.quartz.*;
    
    /**
     * @auther: wujj
     * @date: 2018/8/20 14:25
     * @description:
     */
    public class QuartzManager {
    
        private Scheduler scheduler;
    
        /**
         * @Description: 添加一个定时任务
         *
         * @param jobName 任务名
         * @param jobGroupName  任务组名
         * @param triggerName 触发器名
         * @param triggerGroupName 触发器组名
         * @param jobClass  任务
         * @param cron   时间设置,参考quartz说明文档
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public void addJob(String jobName, String jobGroupName,
                           String triggerName, String triggerGroupName, Class jobClass, String cron) {
            try {
                // 任务名,任务组,任务执行类
                JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
    
                // 触发器
                TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
                // 触发器名,触发器组
                triggerBuilder.withIdentity(triggerName, triggerGroupName);
                triggerBuilder.startNow();
                // 触发器时间设定
                triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
                // 创建Trigger对象
                CronTrigger trigger = (CronTrigger) triggerBuilder.build();
    
                // 调度容器设置JobDetail和Trigger
                scheduler.scheduleJob(jobDetail, trigger);
    
                // 启动
                if (!scheduler.isShutdown()) {
                    scheduler.start();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 修改一个任务的触发时间
         *
         * @param jobName
         * @param jobGroupName
         * @param triggerName 触发器名
         * @param triggerGroupName 触发器组名
         * @param cron   时间设置,参考quartz说明文档
         */
        public void modifyJobTime(String jobName,
                                  String jobGroupName, String triggerName, String triggerGroupName, String cron) {
            try {
                TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
                CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
                if (trigger == null) {
                    return;
                }
    
                String oldTime = trigger.getCronExpression();
                if (!oldTime.equalsIgnoreCase(cron)) {
                    /** 方式一 :调用 rescheduleJob 开始 */
                    // 触发器
                    TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
                    // 触发器名,触发器组
                    triggerBuilder.withIdentity(triggerName, triggerGroupName);
                    triggerBuilder.startNow();
                    // 触发器时间设定
                    triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
                    // 创建Trigger对象
                    trigger = (CronTrigger) triggerBuilder.build();
                    // 方式一 :修改一个任务的触发时间
                    scheduler.rescheduleJob(triggerKey, trigger);
                    /** 方式一 :调用 rescheduleJob 结束 */
    
                    /** 方式二:先删除,然后在创建一个新的Job  */
                    //JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
                    //Class<? extends Job> jobClass = jobDetail.getJobClass();
                    //removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
                    //addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
                    /** 方式二 :先删除,然后在创建一个新的Job */
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 移除一个任务
         *
         * @param jobName
         * @param jobGroupName
         * @param triggerName
         * @param triggerGroupName
         */
        public void removeJob(String jobName, String jobGroupName,
                              String triggerName, String triggerGroupName) {
            try {
                TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
    
                scheduler.pauseTrigger(triggerKey);// 停止触发器
                scheduler.unscheduleJob(triggerKey);// 移除触发器
                scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description:启动所有定时任务
         */
        public void startJobs() {
            try {
                scheduler.start();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description:关闭所有定时任务
         */
        public void shutdownJobs() {
            try {
                if (!scheduler.isShutdown()) {
                    scheduler.shutdown();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        public Scheduler getScheduler() {
            return scheduler;
        }
    
        public void setScheduler(Scheduler scheduler) {
            this.scheduler = scheduler;
        }
    
    }
    

     (2)配置一个MySchedule1 的定时任务

    /**
     * @auther: wujj
     * @date: 2018/8/20 15:02
     * @description:
     */
    public class MySchedule1 implements Job {
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println(new Date() + ": ========job 111111 doing something...");
        }
    }
    

     (3)定时任务起始类

    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    import java.util.Date;
    
    /**
     * @auther: wujj
     * @date: 2018/8/20 15:02
     * @description:
     */
    public class MySchedule implements Job {
    
        public void doSomething(){
            System.out.println(new Date() + ": job master Online...");
        }
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println("job doing something...");
        }
    
    }
    

      

    (4)测试类

    /**
     * @auther: wujj
     * @date: 2018/8/20 15:06
     * @description:
     */
    public class testJob {
    
        public static void main(String[] args) throws BeansException {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-quartz.xml");
            QuartzManager quartzManager = (QuartzManager) ctx.getBean("quartzManager");
            try {
                System.out.println("===============【系统启动】===============");
    
                Thread.sleep(5000);
                System.out.println("【增加job1启动】开始(每1秒输出一次)...");
                quartzManager.addJob("test", "test", "test", "test", MySchedule1.class, "0/1 * * * * ?");
    
                Thread.sleep(10000);
                System.out.println("【修改job1时间】开始(每3秒输出一次)...");
                quartzManager.modifyJobTime("test", "test", "test", "test", "0/3 * * * * ?");
    
                Thread.sleep(20000);
                System.out.println("【移除job1定时】开始...");
                quartzManager.removeJob("test", "test", "test", "test");
    
                Thread.sleep(5000);
                System.out.println("【增加job2启动】开始(每1秒输出一次)...");
                quartzManager.addJob("test", "test", "test", "test", MySchedule1.class, "0/1 * * * * ?");
    
                Thread.sleep(15000);
    
                // 关掉任务调度容器
                quartzManager.shutdownJobs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

      (5)配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
    	   default-lazy-init="true">
    
    	<!-- 这个类用来做需要完成的业务-->
    	<bean id="mySchedule" class="com.bsoft.homecare.schedule.MySchedule"/>
    
    	<!-- 定时任务 -->
    	<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<!-- 调用的类  -->
    		<property name="targetObject" ref="mySchedule" />
    		<!-- 调用类中的方法  -->
    		<property name="targetMethod" value="doSomething" />
    		<!-- 是否并发  -->
    		<property name ="concurrent" value ="false"  />
    	</bean>
    
    	<!-- 定义触发时间 ,这边就不同了,这里必须将时间设置成无限长,因为我们要去读取数据库的时间来做为定时器的触发时间-->
    	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
    		<property name="jobDetail" ref="jobtask" />
    		<!-- cron表达式  -->
    		<!--每小时执行一次 -->
    		<property name="cronExpression" value="0 0 * * * ? *" />
    	</bean>
    
    	<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
    	<bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="cronTrigger" />
    			</list>
    		</property>
    	</bean>
    
    	<!--这个类是用来设置触发时间的, startJobs方法启动调度容器,然后按照上面触发器每隔1s执行所配置的myJob2.doSomething()方法 -->
    	<bean id="quartzManager" class="com.bsoft.homecare.schedule.QuartzManager" lazy-init="false" init-method="startJobs" >
    		<!--这个对象一定要注入,这样类才能进行管理,还有在类型要用get set方法,不然会报错。-->
    		<property name="scheduler" ref="startQuertz" />
    	</bean>
    
    </beans>
    

      

  • 相关阅读:
    Java笔记(十六)……内部类
    pmm系列~基础函数
    redis基础篇~性能问题
    redis基础篇~big-key
    mysql基础~经典题目二
    (转)STORM启动与部署TOPOLOGY
    (转)项目经理怎么当
    专注力的一点体会
    163源报错Hash Sum mismatch 解决方法
    mapreduce.framework.name
  • 原文地址:https://www.cnblogs.com/tuanz/p/9512371.html
Copyright © 2020-2023  润新知