java - ScheduledExecutorService and ThreadPoolTaskExecutor that interrupts tasks after a timeout -
i used executorservice interrupts tasks after timeout.i use scheduledexecutorservice this. first submitted thread , once begin , retain future created. after use scheduledexecutorservice new task cancel retained future after period of time.
//start spring executor submit tasks threadpooltaskexecutor taskexecutor = (threadpooltaskexecutor) applicationcontextprovider.getapplicationcontext().getbean("taskexecutor"); completionservice completionservice = new executorcompletionservice(taskexecutor); //end spring executor submit tasks // start scheduledexecutorservice submit returned future object timeout scheduledexecutorservice executor = executors.newscheduledthreadpool(integer.parseint(config.getproperty("dbpoller_corepoolsize"))); final future<string> future = completionservice.submit(batchjob); // submit actual task , future // submit future executor.schedule(new runnable() { public void run() { future.cancel(true); } }, dbpollertimeout, timeunit.minutes); int count = taskexecutor.getactivecount(); if (count == 0) { taskexecutor.shutdown(); executor.shutdown(); finalexitstatus = 0; break; }
i have implemented solution in below url: executorservice interrupts tasks after timeout, working fine, until timeout, once timeout happens, cancels theenter code here
tasks threadpool not acceptable. need cancel tasks long running , reach timeout.
any idea how achieve this?
it not clear completionservice
is, , submitting batchjob
on it, hard tell exact root cause of problem. ideal scenario of submitting few tasks , cancelling them after time, use scheduledexecutorservice
both purposes.
so, can try submitting batchjob
on instance of scheduledexecutorservice
i.e. executor
.
final future<string> future = executor.submit(batchjob); // submit actual task , future
edit update: important change should in code
see never stopping scheduledexecutorservice
wrong because resources occupies never released until stop it. so, updated code should below:
scheduledexecutorservice executor = executors.newscheduledthreadpool(integer.parseint(config.getproperty("dbpoller_corepoolsize"))); final future<string> future = executor.submit(batchjob); // submit actual task , future executor.schedule(new runnable() { public void run() { future.cancel(true); executor.shutdownnow(); } }, dbpollertimeout, timeunit.minutes);
Comments
Post a Comment