001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.task.support; 018 019import java.util.List; 020import java.util.concurrent.AbstractExecutorService; 021import java.util.concurrent.TimeUnit; 022 023import org.springframework.core.task.TaskExecutor; 024import org.springframework.util.Assert; 025 026/** 027 * Adapter that takes a Spring {@link org.springframework.core.task.TaskExecutor} 028 * and exposes a full {@code java.util.concurrent.ExecutorService} for it. 029 * 030 * <p>This is primarily for adapting to client components that communicate via the 031 * {@code java.util.concurrent.ExecutorService} API. It can also be used as 032 * common ground between a local Spring {@code TaskExecutor} backend and a 033 * JNDI-located {@code ManagedExecutorService} in a Java EE 7 environment. 034 * 035 * <p><b>NOTE:</b> This ExecutorService adapter does <em>not</em> support the 036 * lifecycle methods in the {@code java.util.concurrent.ExecutorService} API 037 * ("shutdown()" etc), similar to a server-wide {@code ManagedExecutorService} 038 * in a Java EE 7 environment. The lifecycle is always up to the backend pool, 039 * with this adapter acting as an access-only proxy for that target pool. 040 * 041 * @author Juergen Hoeller 042 * @since 3.0 043 * @see java.util.concurrent.ExecutorService 044 */ 045public class ExecutorServiceAdapter extends AbstractExecutorService { 046 047 private final TaskExecutor taskExecutor; 048 049 050 /** 051 * Create a new ExecutorServiceAdapter, using the given target executor. 052 * @param taskExecutor the target executor to delegate to 053 */ 054 public ExecutorServiceAdapter(TaskExecutor taskExecutor) { 055 Assert.notNull(taskExecutor, "TaskExecutor must not be null"); 056 this.taskExecutor = taskExecutor; 057 } 058 059 060 @Override 061 public void execute(Runnable task) { 062 this.taskExecutor.execute(task); 063 } 064 065 @Override 066 public void shutdown() { 067 throw new IllegalStateException( 068 "Manual shutdown not supported - ExecutorServiceAdapter is dependent on an external lifecycle"); 069 } 070 071 @Override 072 public List<Runnable> shutdownNow() { 073 throw new IllegalStateException( 074 "Manual shutdown not supported - ExecutorServiceAdapter is dependent on an external lifecycle"); 075 } 076 077 @Override 078 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { 079 throw new IllegalStateException( 080 "Manual shutdown not supported - ExecutorServiceAdapter is dependent on an external lifecycle"); 081 } 082 083 @Override 084 public boolean isShutdown() { 085 return false; 086 } 087 088 @Override 089 public boolean isTerminated() { 090 return false; 091 } 092 093}