创建 bean 时出错,无法将新的 LinkedBlockingQueue 作为构造函数注入到 ThreadPoolExecutor

发布于 11-16 01:54 字数 1289 浏览 2 评论 0原文

我正在尝试创建一个 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

I'm trying to create a ThreadPoolExecutor bean which needs to be passed as a property to a number of other beans.

I have defined the following, but I'm encountering an odd error creating the LinkedBlockingQueue (seen below).

<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>

Error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ2024-11-23 01:54:59

我不太确定为什么会出现此消息。最后,它只是一条 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>

I am not quite sure why this message shows up. In the end it's only a DEBUG message and it should not prevent you from bringing up your application. Alternatively, you could use a FactoryBean instead that instantiates 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;
    }
}

In your application context you'd then create the bean like this:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文