zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    每日一题力扣520 判断大小写
    每日一题力扣434
    每日一题力扣557
    每日一题力扣344 反转字符串
    每日一题力扣125 回文字符串
    每日一题力扣541
    每日一题力扣58
    【20211015】项目成本粗略分析
    【20211013】连岳摘抄
    【20211014】自己就是解决问题的起点
  • 原文地址:https://www.cnblogs.com/yshyee/p/4483641.html
Copyright © 2011-2022 走看看