1 16 17 package org.springframework.scheduling.concurrent; 18 19 import java.util.concurrent.Executor ; 20 import java.util.concurrent.Executors ; 21 import java.util.concurrent.RejectedExecutionException ; 22 23 import org.springframework.core.task.TaskRejectedException; 24 import org.springframework.scheduling.SchedulingTaskExecutor; 25 26 50 public class ConcurrentTaskExecutor implements SchedulingTaskExecutor, Executor { 51 52 private Executor concurrentExecutor; 53 54 55 60 public ConcurrentTaskExecutor() { 61 setConcurrentExecutor(null); 62 } 63 64 69 public ConcurrentTaskExecutor(Executor concurrentExecutor) { 70 setConcurrentExecutor(concurrentExecutor); 71 } 72 73 74 77 public void setConcurrentExecutor(Executor concurrentExecutor) { 78 this.concurrentExecutor = 79 (concurrentExecutor != null ? concurrentExecutor : Executors.newSingleThreadExecutor()); 80 } 81 82 86 public Executor getConcurrentExecutor() { 87 return this.concurrentExecutor; 88 } 89 90 91 95 public void execute(Runnable task) { 96 try { 97 this.concurrentExecutor.execute(task); 98 } 99 catch (RejectedExecutionException ex) { 100 throw new TaskRejectedException( 101 "Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex); 102 } 103 } 104 105 108 public boolean prefersShortLivedTasks() { 109 return true; 110 } 111 112 } 113 | Popular Tags |