我试图创建一个ThreadPoolExecutor bean,它需要作为属性传递给许多其他bean。
我定义了以下内容,但是我遇到了一个奇怪的错误,创建了LinkedBlockingQueue(见下文)。
<bean name="moduleExecutorService" class="java.util.concurrent.ThreadPoolExecutor">
<constructor-arg value="1" /> <!-- Minimun # of threads in pool -->
<constructor-arg value="20" /> <!-- Maximum # of threads in pool (pool is a caching pool that will only keep open those necessary) -->
<constructor-arg value="60" /> <!-- Shutdown unused threads after this TimeUnit -->
<constructor-arg>
<bean class="java.util.concurrent.TimeUnit" factory-method="valueOf">
<constructor-arg value="SECONDS" />
</bean>
</constructor-arg>
<constructor-arg type="java.util.concurrent.LinkedBlockingQueue"><bean class="java.util.concurrent.LinkedBlockingQueue" /></constructor-arg>
</bean>
的错误:
446 [main] DEBUG org.springframework.beans.TypeConverterDelegate - Cannot create copy of Collection type [java.util.concurrent.LinkedBlockingQueue] - injecting original Collection as-is
java.lang.InstantiationException: java.util.concurrent.BlockingQueue
答案 0 :(得分:0)
我不太清楚为什么会出现此消息。最后它只是一个DEBUG
消息,它不应该阻止你提出你的应用程序。或者,您可以使用FactoryBean
来实例化ThreadPoolExecutor
。
public class ThreadPoolExecutorFactory implements FactoryBean
{
private final ReentrantLock lock = new ReentrantLock();
private ThreadPoolExecutor executor;
private int corePoolSize;
private int maximumPoolSize;
private long keepAliveTime;
private TimeUnit unit;
public Object getObject()
{
lock.lock();
if (executor == null)
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, unit, new LinkedBlockingQueue<Runnable>());
lock.unlock();
return executor;
}
public Class getObjectType()
{
return ThreadPoolExecutor.class;
}
public boolean isSingleton()
{
return true;
}
}
在您的应用程序上下文中,您将创建如下的bean:
<bean name="moduleExecutorService" class="ThreadPoolExecutorFactory">
<constructor-arg value="1" />
<constructor-arg value="20" />
<constructor-arg value="60" />
<constructor-arg>
<bean class="java.util.concurrent.TimeUnit" factory-method="valueOf">
<constructor-arg value="SECONDS" />
</bean>
</constructor-arg>
</bean>