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);
    }

}

 

posted @ 2015-05-06 23:45  yshy  阅读(288)  评论(0编辑  收藏  举报