在石英调度程序中停止当前运行作业的更好方法是什么?

时间:2016-06-30 05:37:42

标签: quartz-scheduler

我正在使用quartz Scheduler。我想停止当前正在运行的作业。     我在Web应用程序中使用这个。当我单击开始按钮然后启动作业执行但是当我想使用停止按钮停止此执行作业时。     但我无法阻止。     请告诉我如何停止当前正在运行的工作?

I am giving my code.

//when i click on start button then this class will call.



@WebServlet("/ScheduleController")
public class AscentScheduleController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String RANDOM = "random";

    Scheduler schedulerRecon;
    Scheduler schedulerPos;
    Scheduler schedulerFile;
    Scheduler schedulerEtl;
    Trigger triggerIris;
    JobKey jobKey;
    JobDetail job;
    String key;
    int time;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AscentScheduleController() {
        super();

    }

    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        HttpSession session = request.getSession();

        try {

            String id = request.getParameter("id");

            System.out.println("hello in controller");
            System.out.println(id);

             System.out.println(Thread.currentThread().getName());



             schedulerRecon = new StdSchedulerFactory()
                .getScheduler();
        schedulerRecon.start();
        session.setAttribute(id,schedulerRecon );   
        System.out.println();// SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
        System.out.println();
        System.out.println();
        System.out.println("controller schedil     "+schedulerRecon);

             System.out.println(Thread.currentThread().getName());
                jobKey = new JobKey("jobKeyIris", "group1");

                job = JobBuilder.newJob(DemoTrgger.class).withIdentity("jobKey").build();

                triggerIris = TriggerBuilder.newTrigger().withIdentity("triggerIris", "group1").startNow()  
                        .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60).// repeatForever()).build();
                        withRepeatCount(0)).build();
                session.setAttribute("job", jobKey);
                System.out.println();// SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
                System.out.println();
                System.out.println();
                System.out.println("controller job key    "+jobKey);
                // temp=temp+temp;
                schedulerRecon.scheduleJob(job, triggerIris);
                // scheduler.scheduleJob(jobQCB, triggerQCB);
                System.out.println("fire job");

        }
}       
//and my job class which implement Job is below


public class DemoTrgger implements Job {
public static Thread th;
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println();
        System.out.println("demo trigger  "+Thread.currentThread().getName()+"thread obj  "+Thread.currentThread());
        th = Thread.currentThread();
        System.out.println();
        for(long i=0l;i<2000000;i++){
            System.out.println("i---------  "+i);
        }
        for(long i=0l;i<2000000l;i++){
            System.out.println("j---  "+i);
        }
    }


}


when I click on stop button the below class will call.Here I get the Scheduler reference which I use in start using session and I want stop respectively running job.

@WebServlet("/SchedulerStopController")
public class SchedulerStopController extends HttpServlet implements InterruptableJob {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SchedulerStopController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String id=request.getParameter("id");
        System.out.println(id);
        HttpSession session=request.getSession();
        Scheduler schedular=(Scheduler) session.getAttribute(id);
        System.out.println();// SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
        System.out.println();
        System.out.println();
        System.out.println("stop schedular   "+schedular);


        try {
            SchedulerMetaData metaData = schedular.getMetaData();
        } catch (SchedulerException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
       // log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

        /*Runtime.getRuntime().addShutdownHook(new Thread()
        {
            public void run()
            {
                System.out.println(Thread.currentThread().getName());
                // this will get called on shutdown

// TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("------- My own shutdown hook -----------------");
            }
        });
    */




        try {
            JobKey jobKey=(JobKey) session.getAttribute("job");
            System.out.println();// SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
            System.out.println();
            System.out.println("stop controller "+Thread.currentThread().getName());
            System.out.println();
            System.out.println("stop job   "+jobKey);
            schedular.deleteJob(jobKey);


        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    thanks.      

0 个答案:

没有答案