skaic commented on issue #1464:
URL: 
https://github.com/apache/shardingsphere-elasticjob/issues/1464#issuecomment-870211599


   Core:
   核心思想:
   
   We start a process in new thread and waiting it。 When time out, call the 
interrupt() of the thread  And cloes() of the thread socket 。
   我们通过线程启动任务后,使用一个计时器进行等待,如果超时了,就调用该线程的 interrupt 方法和该线程的套接字 close 方法。
   
   So,  Ours Job can  try to find a timeout  By  Check  isInterrupted ( 
`Thread.currentThread().isInterrupted()` ) 、 catch InterruptedException | 
IOException , and to close self .
   因此,我们的任务就可以通过检查 线程的 isInterrupted 
标记(`Thread.currentThread().isInterrupted()`) 、 捕获 InterruptedException | 
IOException 来发现超时,让任务自己可以顺利的终止执行。
   
   
   
   
   代码如下:
   
   ```
    for (int each : items) {
               ... 
               Future<Boolean> future = executorService.submit(() -> {
                   try {
                       process(jobConfig, shardingContexts, each, 
jobExecutionEvent);
                   } finally {
                       latch.countDown();
                   }
                   return true;
               });
               futures.add(future);
           }
   
           for (Future<Boolean> future : futures) {
               try {
                   future.get(10, TimeUnit.SECONDS);
               } catch (final InterruptedException ex) {
                   Thread.currentThread().interrupt();
               } catch (final ExecutionException ex) {
                   throw new JobSystemException(ex);
               } catch (final TimeoutException ex) {
                   // call the interrupt()
                   future.cancel(true);
               }
           }
   ```
   
   Job:
   
   
   ```
   public class JavaSimpleJob implements SimpleJob {
   
       private final FooRepository fooRepository = 
FooRepositoryFactory.getFooRepository();
   
       @Override
       public void execute(final ShardingContext shardingContext) {
           List<Foo> data = 
fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
           for (Foo each : data) {
               if (!Thread.currentThread().isInterrupted()) {
                   fooRepository.doSomeThing(each.getId());
               }
           }
       }
   
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to