interrupt机制学习

这个机制需要注意一点,只有当job正在执行中时才会调用对应的interrupt方法,如果job不处于执行状态比如处于执行完毕还未到第二次触发的这个间隙则不会调用对应的interrupt方法。同时,在job中需要定时监控interrupt的状态以便执行对应的interrupt逻辑,通常用一个boolean类型记录interrupt状态。示例如下:

DumbInterruptableJob.java:

 

 

package quartz.example.example7;

 

import java.util.Date;

 

import org.quartz.InterruptableJob;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.quartz.UnableToInterruptJobException;

 

 

public class DumbInterruptableJob implements InterruptableJob {

 

 

    // has the job been interrupted?

    private boolean _interrupted = false;

 

    private String _jobName = "";

 

    public DumbInterruptableJob() {

    }

 

 

    public void execute(JobExecutionContext context)

        throws JobExecutionException {

 

        _jobName = context.getJobDetail().getFullName();

        System.out.println("---- " + _jobName + " executing at " + new Date());

 

        try {

 

            for (int i = 0; i < 4; i++) {

                try {

                    Thread.sleep(1000L);

                } catch (Exception ignore) {

                    ignore.printStackTrace();

                }

                // periodically check if we've been interrupted...

                if(_interrupted) {

                    System.out.println("--- " + _jobName + "  -- Interrupted... bailing out!"+new Date());

                    return; 

                }

            }

 

        } finally {

            System.out.println("---- " + _jobName + " completed at " + new Date());

        }

    }

 

    public void interrupt() throws UnableToInterruptJobException {

        System.out.println("---" + "  -- INTERRUPTING --"+new Date());

        _interrupted = true;

    }

 

}


InterruptExample.java :

package quartz.example.example7;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InterruptExample {
public ApplicationContext app;
public Scheduler sched;

    public void run() throws Exception {

        System.out.println("------- Initializing ----------------------");

        System.out.println("------- Initialization Complete -----------");

        System.out.println("------- Scheduling Jobs -------------------");

        // get a "nice round" time a few seconds in the future...
        long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();

        JobDetail job = new JobDetail("interruptableJob1", "group1",
                DumbInterruptableJob.class);
        SimpleTrigger trigger = 
            new SimpleTrigger("trigger1", "group1", 
                    new Date(ts), 
                    null, 
                    SimpleTrigger.REPEAT_INDEFINITELY, 
                    5000L);
        Date ft = sched.scheduleJob(job, trigger);
        System.out.println(job.getFullName() + " will run at: " + ft + " and repeat: "
                + trigger.getRepeatCount() + " times, every "
                + trigger.getRepeatInterval() / 1000 + " seconds");

        sched.start();
        System.out.println("------- Started Scheduler -----------------");
        

        System.out.println("------- Starting loop to interrupt job every 7 seconds ----------"+new Date());
        for(int i=0; i < 50; i++) {
            try {
                Thread.sleep(7000L); 
                // tell the scheduler to interrupt our job
                System.out.println(new Date());
                sched.interrupt(job.getName(), job.getGroup());
            } catch (Exception e) {
            }
        }
        
        System.out.println("------- Shutting Down ---------------------");

        sched.shutdown(true);

        System.out.println("------- Shutdown Complete -----------------");

    }

    public static void main(String[] args) throws Exception {

        InterruptExample example = new InterruptExample();
        example.app=new ClassPathXmlApplicationContext(new String[] {
"classpath:applicationContext.xml",
"classpath:applicationContext-quartz.xml" });
        example.sched = (Scheduler) example.app.getBean("quartzScheduler");
        example.run();
    }

}

运行结果如下:
------- Initializing ----------------------
------- Initialization Complete -----------
------- Scheduling Jobs -------------------
group1.interruptableJob1 will run at: Wed Sep 28 14:30:00 CST 2011 and repeat: -1 times, every 5 seconds
------- Started Scheduler -----------------
------- Starting loop to interrupt job every 7 seconds ----------Wed Sep 28 14:29:46 CST 2011
Wed Sep 28 14:29:53 CST 2011
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:00 CST 2011
Wed Sep 28 14:30:00 CST 2011
---  -- INTERRUPTING --Wed Sep 28 14:30:00 CST 2011
--- group1.interruptableJob1  -- Interrupted... bailing out!Wed Sep 28 14:30:01 CST 2011
---- group1.interruptableJob1 completed at Wed Sep 28 14:30:01 CST 2011
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:05 CST 2011
Wed Sep 28 14:30:07 CST 2011  //job处于执行期,调用interrupt方法,中断job
---  -- INTERRUPTING --Wed Sep 28 14:30:07 CST 2011
--- group1.interruptableJob1  -- Interrupted... bailing out!Wed Sep 28 14:30:08 CST 2011
---- group1.interruptableJob1 completed at Wed Sep 28 14:30:08 CST 2011
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:10 CST 2011
---- group1.interruptableJob1 completed at Wed Sep 28 14:30:14 CST 2011
Wed Sep 28 14:30:14 CST 2011   //该处因为job不处于执行期所以interrupt方法不调用
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:15 CST 2011
---- group1.interruptableJob1 completed at Wed Sep 28 14:30:19 CST 2011
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:20 CST 2011
Wed Sep 28 14:30:21 CST 2011
---  -- INTERRUPTING --Wed Sep 28 14:30:21 CST 2011
--- group1.interruptableJob1  -- Interrupted... bailing out!Wed Sep 28 14:30:22 CST 2011
---- group1.interruptableJob1 completed at Wed Sep 28 14:30:22 CST 2011
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:25 CST 2011
Wed Sep 28 14:30:28 CST 2011
---  -- INTERRUPTING --Wed Sep 28 14:30:28 CST 2011
--- group1.interruptableJob1  -- Interrupted... bailing out!Wed Sep 28 14:30:29 CST 2011
---- group1.interruptableJob1 completed at Wed Sep 28 14:30:29 CST 2011
---- group1.interruptableJob1 executing at Wed Sep 28 14:30:30 CST 2011

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值