/** * Specify the {@link java.util.concurrent.Executor} to delegate to. * <p>Autodetects a JSR-236 {@link javax.enterprise.concurrent.ManagedExecutorService} * in order to expose {@link javax.enterprise.concurrent.ManagedTask} adapters for it. */ public final void setConcurrentExecutor(Executor concurrentExecutor) { if (concurrentExecutor != null) { this.concurrentExecutor = concurrentExecutor; if (managedExecutorServiceClass != null && managedExecutorServiceClass.isInstance(concurrentExecutor)) { this.adaptedExecutor = new ManagedTaskExecutorAdapter(concurrentExecutor); } else { this.adaptedExecutor = new TaskExecutorAdapter(concurrentExecutor); } } else { this.concurrentExecutor = Executors.newSingleThreadExecutor(); this.adaptedExecutor = new TaskExecutorAdapter(this.concurrentExecutor); } }
/** * Determine the specific executor to use when executing the given method. * @return the executor to use (or {@code null}, but just if no default executor has been set) */ protected AsyncTaskExecutor determineAsyncExecutor(Method method) { AsyncTaskExecutor executor = this.executors.get(method); if (executor == null) { Executor executorToUse = this.defaultExecutor; String qualifier = getExecutorQualifier(method); if (StringUtils.hasLength(qualifier)) { Assert.notNull(this.beanFactory, "BeanFactory must be set on " + getClass().getSimpleName() + " to access qualified executor '" + qualifier + "'"); executorToUse = BeanFactoryAnnotationUtils.qualifiedBeanOfType( this.beanFactory, Executor.class, qualifier); } else if (executorToUse == null) { return null; } executor = (executorToUse instanceof AsyncTaskExecutor ? (AsyncTaskExecutor) executorToUse : new TaskExecutorAdapter(executorToUse)); this.executors.put(method, executor); } return executor; }
/** * Determine the specific executor to use when executing the given method. * Should preferably return an {@link AsyncListenableTaskExecutor} implementation. * @return the executor to use (or {@code null}, but just if no default executor has been set) */ protected AsyncTaskExecutor determineAsyncExecutor(Method method) { AsyncTaskExecutor executor = this.executors.get(method); if (executor == null) { Executor executorToUse = this.defaultExecutor; String qualifier = getExecutorQualifier(method); if (StringUtils.hasLength(qualifier)) { if (this.beanFactory == null) { throw new IllegalStateException("BeanFactory must be set on " + getClass().getSimpleName() + " to access qualified executor '" + qualifier + "'"); } executorToUse = BeanFactoryAnnotationUtils.qualifiedBeanOfType( this.beanFactory, Executor.class, qualifier); } else if (executorToUse == null) { return null; } executor = (executorToUse instanceof AsyncListenableTaskExecutor ? (AsyncListenableTaskExecutor) executorToUse : new TaskExecutorAdapter(executorToUse)); this.executors.put(method, executor); } return executor; }
/** * Determine the specific executor to use when executing the given method. * Should preferably return an {@link AsyncListenableTaskExecutor} implementation. * @return the executor to use (or {@code null}, but just if no default executor is available) */ protected AsyncTaskExecutor determineAsyncExecutor(Method method) { AsyncTaskExecutor executor = this.executors.get(method); if (executor == null) { Executor targetExecutor; String qualifier = getExecutorQualifier(method); if (StringUtils.hasLength(qualifier)) { targetExecutor = findQualifiedExecutor(this.beanFactory, qualifier); } else { targetExecutor = this.defaultExecutor; if (targetExecutor == null) { synchronized (this.executors) { if (this.defaultExecutor == null) { this.defaultExecutor = getDefaultExecutor(this.beanFactory); } targetExecutor = this.defaultExecutor; } } } if (targetExecutor == null) { return null; } executor = (targetExecutor instanceof AsyncListenableTaskExecutor ? (AsyncListenableTaskExecutor) targetExecutor : new TaskExecutorAdapter(targetExecutor)); this.executors.put(method, executor); } return executor; }
/** * 设置并委托给JDK 1.5的并发执行器。 * * <p>Specify the JDK 1.5 concurrent executor to delegate to. */ public final void setConcurrentExecutor(Executor concurrentExecutor) { // 默认使用单线程的执行器服务 this.concurrentExecutor = (concurrentExecutor != null ? concurrentExecutor : Executors.newSingleThreadExecutor()); this.adaptedExecutor = new TaskExecutorAdapter(this.concurrentExecutor); }
@Bean public Executor asyncExecutor() { Thread.setDefaultUncaughtExceptionHandler((t, e) -> LOGGER.error("Uncaught exception in thread " + t + ": " + e, e)); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(1 * Runtime.getRuntime().availableProcessors()); executor.setMaxPoolSize(2 * Runtime.getRuntime().availableProcessors()); executor.setQueueCapacity(100000); executor.setThreadNamePrefix("BoDExecutor-"); executor.initialize(); return new TaskExecutorAdapter(new TransactionAwareExecutor(executor)); }
/** * Specify the JDK 1.5 concurrent executor to delegate to. */ public final void setConcurrentExecutor(Executor concurrentExecutor) { this.concurrentExecutor = (concurrentExecutor != null ? concurrentExecutor : Executors.newSingleThreadExecutor()); this.adaptedExecutor = new TaskExecutorAdapter(this.concurrentExecutor); }