• quartz2.2.1-测试02-通过servlet动态修改定时任务运行时间


    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package com.yan.test.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.quartz.CronScheduleBuilder;
    import org.quartz.CronTrigger;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerFactory;
    import org.quartz.TriggerBuilder;
    import org.quartz.TriggerKey;
    import org.quartz.impl.StdSchedulerFactory;
    
    /**
     *
     * @author yan
     */
    public class ModifyJobTimeServlet extends HttpServlet {
    
       
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
         
            SchedulerFactory sf = new StdSchedulerFactory();
            
            /**
             * triggerName=triggerNameTest
             * triggerGroup=triggerGroupTest
             * cron=0/10 * * * * ?
             */
            try {
                Scheduler scheduler = sf.getScheduler();
                
                String triggerName = request.getParameter("triggerName");
                String triggerGroup = request.getParameter("triggerGroup");
                String cron = request.getParameter("cron");
                
                
                TriggerKey tk = new TriggerKey(triggerName,triggerGroup);
                
                CronTrigger newCronTrigger = TriggerBuilder.newTrigger()
                        .withIdentity(tk)  
                        .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();  
                
                scheduler.rescheduleJob(tk, newCronTrigger);
            } catch (SchedulerException ex) {
                Logger.getLogger(ModifyJobTimeServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    
        
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
    }
  • 相关阅读:
    Java学习之路(三)--Thinking in Java
    Java学习之路(二)--Thinking in Java
    Java学习之路(一)--Thinking in Java
    ES6中y修饰符合u修饰符
    map数据结构
    Set数据结构
    不确定参数的处理
    函数参数的默认值
    class基础语法
    生成新数组的方法和在数组中查找
  • 原文地址:https://www.cnblogs.com/yshyee/p/4483641.html
Copyright © 2020-2023  润新知