私は10秒ごとにジョブをトリガしなければならないQuartzフレームワークに取り組んでおり、すべてのジョブが10秒後にサービスにヒットしました。中断されたスレッドはスレッドプールによって削除されます
メインクラス
public class CronTriggerExample
{
public static void main(String[] args) throws Exception
{
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
ジョブ・クラス
私のテスト条件についてループしながら、私は無限に実行しています。
public class HelloJob implements InterruptableJob
{
AtomicReference<Thread> runningThread = new AtomicReference<Thread>();
AtomicBoolean stopFlag = new AtomicBoolean(false);
static Date outcallExecuteJobRunTime = new Date();
static boolean prvJobRunning = false;
private static AtomicBoolean prvJobExecuted = new AtomicBoolean(true);
public void execute(JobExecutionContext context)
throws JobExecutionException {
long start = System.currentTimeMillis();
try{
System.out.println("["+Thread.currentThread().getName()+"] Running OutCallExecutor job");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -20);
Date compareDate = cal.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
boolean isThreadExceededRunTime = compareDate.after(outcallExecuteJobRunTime);
System.out.println("compare date is : "+df.format(compareDate));
System.out.println("Running Thread is "+runningThread);
if(! isThreadExceededRunTime)
System.out.println("Previous job is running withing time limit");
if(!prvJobExecuted.get()){
//Check if prev job is still running? If yes, then the thread is hanged/blocked -
//so interrupt it and set previous job to true
if(isThreadExceededRunTime){
System.out.println("Previouse job is running more the limit");
interrupt();
return;
}else{
System.out.println("Normal exit. Previous job is not executed yet");
return;
}
}
prvJobExecuted.set(false);
this.runningThread.set(Thread.currentThread());
System.out.println("Running Thread is "+runningThread.get().getName());
outcallExecuteJobRunTime = new Date();
System.out.println("OutCallExcecutor :: outcallExecuteJobRunTime : "+outcallExecuteJobRunTime);
System.out.println("Running OutCallExecutor executed.");
while(1 == 1){}
}catch(Exception e){
e.printStackTrace();
System.out.println("OutCallExecutor :: Exception occured while OutCallExecutor. Exception "+e.getMessage());
prvJobExecuted.set(true);
}
prvJobExecuted.set(true);
}
@Override
public void interrupt() throws UnableToInterruptJobException {
//System.out.println("HelloJob.interrupted BZZZZZZZZZZ(). Stopping running Thread "+runningThread.get().getName());
System.out.println("===1=====");
prvJobExecuted.set(true);
System.out.println("===2=====");
Thread thread = runningThread.getAndSet(null);
System.out.println("===1===== Thread is "+thread);
if (thread != null){
if(thread.getName() != null){
System.out.println("===1===== Thread Name is "+thread.getName());
}
thread.interrupt();
}
}
}
だけで理解するために、私はプログラムの出力を印刷しています
アウトプット
[DefaultQuartzScheduler_Worker-1] Running OutCallExecutor job
compare date is : 2016-07-21 23:01:00
Running Thread is null
Previous job is running withing time limit
Running Thread is DefaultQuartzScheduler_Worker-1
OutCallExcecutor :: outcallExecuteJobRunTime : Thu Jul 21 23:01:20 IST 2016
Running OutCallExecutor executed.
[DefaultQuartzScheduler_Worker-2] Running OutCallExecutor job
OutCallExecutor compare date is : 2016-07-21 23:01:10
Running Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
Previous job is running withing time limit
Normal exit. Previous job is not executed yet
======================================================
====================================================
[DefaultQuartzScheduler_Worker-3] Running OutCallExecutor job
compare date is : 2016-07-21 23:01:20
Running Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
Previous job is running withing time limit
Normal exit. Previous job is not executed yet
======================================================
====================================================
[DefaultQuartzScheduler_Worker-4] Running OutCallExecutor job
compare date is : 2016-07-21 23:01:30
Running Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
Previouse job is running more the limit
===1=====
===2=====
===1===== Thread is Thread[DefaultQuartzScheduler_Worker-1,5,main]
===1===== Thread Name is DefaultQuartzScheduler_Worker-1
は、私がいる間、その後、WorkerThread-1を中断し、再びプールに糸を通すために割り当てないのと、それは損失を行ったとき私はプールのデフォルトのサイズを同じに残す必要がありますスレッドしたい。 スレッドプールのサイズが上記のように書かれたプログラムのように影響を与えないスレッドを強制終了または停止するのを手伝ってもらえますか?スレッドプール全体が緩やかになり、最後のスレッドは新しいジョブがサービスにヒットしないので中断しません。
ありがとうございました!
うわーを試してみてください。私はあなたが何をしようとしているのか理解していません。あなたの仕事(仕事)は仕事をうまくやってはいけませんし、それが動いているスレッドで何もしてはいけませんか?ジョブが(通常または例外的に)実行された後、スレッドはプールに戻る必要があります。なぜあなたはそれを中断していますか? –
私の仕事が滞っていて、何の例外もエラーもなく長時間実行される場合、その特定のジョブを中断してスレッドプールに再度送信する必要があるため、他のジョブを実行することはできません。 – vicky9988
私は他のスレッドがそれにアクセスすることを許可しないために無限ループwhileを置いて、その場合は中断し、今起こっていないThreadPoolに送り返すべきです。私は望みます...私は明らかにしません、もっと詳しく説明しようと思うよ – vicky9988