1

30秒ごとに実行されるメインクラスで単純なクォーツジョブを実行しています。

public class Start {
 public static void main(String[] args) throws Exception {    
      SchedulerFactory sf = new StdSchedulerFactory();
      Scheduler sched = sf.getScheduler();
       JobDetail job = newJob(MyJob.class).withIdentity("myJob","XXX").build();
   Trigger trigger = TriggerBuilder.newTrigger()
         .withSchedule(
             SimpleScheduleBuilder.simpleSchedule()
                 .withIntervalInSeconds(30)
             .repeatForever())
                               .build();
    sched.scheduleJob(job, trigger);
     sched.start();
  } 
}

ここでは、次のように InterruptableJob を実装しています

 public class MyJob implements InterruptableJob {

private volatile boolean isJobInterrupted = false;

private JobKey jobKey = null;

private volatile Thread thisThread;

public MyJob() {
}

@Override
public void interrupt() throws UnableToInterruptJobException {
    // TODO Auto-generated method stub
    System.err.println("calling interrupt:"+thisThread+"==>"+jobKey);
    isJobInterrupted = true;
    if (thisThread != null) {
        // this call causes the ClosedByInterruptException to happen
        thisThread.interrupt();
    }

}

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    // TODO Auto-generated method stub
    thisThread = Thread.currentThread();

    jobKey = context.getJobDetail().getKey();

    System.err.println("calling execute:"+thisThread+"==>"+jobKey);
}
}

今、私は運がないあらゆる方法で別のメインクラスを使用してジョブを停止しようとしました

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

     SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        // get a "nice round" time a few seconds in the future...
        Date startTime = nextGivenSecondDate(null, 1);

        JobDetail job = newJob(MyJob.class).withIdentity("myJob", "XXX").build();

        Trigger trigger = TriggerBuilder.newTrigger()
        .withSchedule(
                 SimpleScheduleBuilder.simpleSchedule()
                     .withIntervalInSeconds(30)
                 .repeatForever())
                                   .build();

        sched.scheduleJob(job, trigger);

        sched.start();


            try {
                  // if you want to see the job to finish successfully, sleep for about 40 seconds
                Thread.sleep(60000) ;
                  // tell the scheduler to interrupt our job
                  sched.interrupt(job.getKey());
                  Thread.sleep(3 * 1000L);
                } catch (Exception e) {
                  e.printStackTrace();
                }
                System.err.println("------- Shutting Down --------");

                TriggerKey tk=TriggerKey.triggerKey("myJob","group1");
                System.err.println("tk"+tk+":"+job.getKey());
                sched.unscheduleJob(tk);
                sched.interrupt(job.getKey());
                sched.interrupt("myJob");
                sched.deleteJob(job.getKey());
                sched.shutdown();
                System.err.println("------- Shutting Down ");

                sched.shutdown(false);

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

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

    }
}

仕事を止める正しい方法を誰か教えてください。どうもありがとう。

4

1 に答える 1

7

この質問は、あなたが説明している正確な問題に答えているようです:

InterruptableJob の実装としてジョブを作成する必要があります。このジョブを中断するにはScheduler、へのハンドルが必要です。interrupt(jobKey<<job name & job group>>)

InterruptableJob ドキュメントに従って:

実行を中断するためのメカニズムを提供する、ジョブによって実装されるインターフェース。ジョブがこのインターフェイスを実装する必要はありません。実際、ほとんどの人にとって、どのジョブも実装しません。

ジョブの中断は、Java のスレッドの通常の中断と概念と課題が非常に類似しています。

Job を実際に中断する手段は、Job 自体に実装する必要があります (このインターフェースの interrupt() メソッドは、ジョブを中断する要求が行われたことをスケジューラが Job に通知するための単なる手段です)。ジョブが自分自身を中断するために使用するメカニズムは、実装によって異なる場合があります。ただし、実装における原則的な考え方は、ジョブの execute(..) の本体で定期的に何らかのフラグをチェックして、中断が要求されているかどうかを確認し、フラグが設定されている場合は、何らかの形で残りの実行を中止することです。仕事の仕事。

鉱山を強調します。類似していますが、同じではありません。スレッドを使用することは期待されていません (ただし、実際にスレッドを使用する場合は使用できますJob...)。

ジョブを中断する例は、クラス org.quartz.examples.DumbInterruptableJob の Java ソースにあります。設定に気付いたことを execute(..) が通知するまで、interrupt() メソッドをブロックするために、interrupt() と execute(..) 内で wait() と notify() 同期の組み合わせを使用することは合法です。国旗。

そのため、ドキュメントを読み、完全なダウンロードで例を調べることをお勧めします。

于 2013-08-27T14:06:39.423 に答える