从4.2.0.RC3升级到4.2.0.RELEASE时出现Spring异步问题

时间:2015-08-04 18:13:32

标签: java spring spring-mvc executorservice spring-websocket

我有一个使用spring(4.2.x)工件的web应用程序spring-webmvc,spring-messaging,spring-websocket

我在spring config java class

中有以下@ Enable * annotations
@EnableWebMvc
@EnableWebSocketMessageBroker
@EnableAsync
@EnableMBeanExport

WebSocket用于向浏览器客户端广播消息。 并且很少有使用@Async

注释的异步方法

该应用程序在春季版本4.2.0.RC3上正常运行。但当我将其更改为GA版本4.2.0.RELEASE时,我在启动时得到以下异常。如果我删除@EnableAsync它工作正常,但我需要异步功能。

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.core.task.TaskExecutor] is defined: expected single matching bean but found 4: clientOutboundChannelExecutor,messageBrokerTaskScheduler,clientInboundChannelExecutor,brokerChannelExecutor
    org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:366)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
    org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor.setBeanFactory(AsyncAnnotationBeanPostProcessor.java:128)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1597)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1565)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201)
    org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)
    org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:682)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:539)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

3 个答案:

答案 0 :(得分:21)

在Spring应用程序上下文配置中添加bean

@Configuration
@EnableAsync
public class AppContext extends WebMvcConfigurationSupport {
    @Bean
    public Executor taskExecutor() {
        return new SimpleAsyncTaskExecutor();
    }
}

我建议您参考linuxism.tistory.com/2076

如果以XML格式声明执行程序,则可以创建一个类并将其命名为TaskExecutor。然后,当Spring试图找到TaskExecutor bean时,它会找到这个。

@Component
public class TaskExecutor extends SimpleAsyncTaskExecutor {
}

答案 1 :(得分:10)

您的@Configuration之一必须实施AsyncConfigurer来为TaskExecutor方法指定特定的@Async

否则,可以从applicationContext中选择哪一个。

即使它与RC3一起使用也不重要,因此错误已经修复GA

<强>更新

AsyncAnnotationBeanPostProcessor中的源代码如下:

Executor executorToUse = this.executor;
if (executorToUse == null) {
    try {
        // Search for TaskExecutor bean... not plain Executor since that would
        // match with ScheduledExecutorService as well, which is unusable for
        // our purposes here. TaskExecutor is more clearly designed for it.
        executorToUse = beanFactory.getBean(TaskExecutor.class);
    }
    catch (NoUniqueBeanDefinitionException ex) {
        try {
            executorToUse = beanFactory.getBean(DEFAULT_TASK_EXECUTOR_BEAN_NAME, TaskExecutor.class);
        }
        catch (NoSuchBeanDefinitionException ex2) {
            throw new IllegalStateException("More than one TaskExecutor bean exists within the context, " +
                    "and none is named 'taskExecutor'. Mark one of them as primary or name it " +
                    "'taskExecutor' (possibly as an alias); or specify the AsyncConfigurer interface " +
                    "and implement getAsyncExecutor() accordingly.", ex);
        }
    }
    catch (NoSuchBeanDefinitionException ex) {
        logger.debug("Could not find default TaskExecutor bean", ex);
        // Giving up -> falling back to default executor within the advisor...
    }
}

所以,我想在从RC3转到GA之间,你已经有了taskExecutor bean了。

正如我们所看到的,StackTrace已经有了这样一个bean ......

答案 2 :(得分:1)

对于像我这样仍在使用老式XML配置的人......

这在春季4.2之前对我有用:

<task:annotation-driven />
<task:executor id="executorA" pool-size="50" />
<task:executor id="executorB" pool-size="100" />

@Async("executorA")
public void executeA() {}

@Async("executorB")
public void executeB() {}

正如Artem所指出的,即使你的应用程序中没有这样的方法,Spring 4.2也会对哪个池用于无资格的异步方法感到困惑。

为了解决这个问题,我使用了这个:

<task:annotation-driven executor="defaultExecutor"/>

<task:executor id="defaultExecutor" pool-size="1" queue-capacity="0"/>
<task:executor id="executorA" pool-size="50" />
<task:executor id="executorB" pool-size="100" />

@Async("executorA")
public void executeA() {}

@Async("executorB")
public void executeB() {}

请注意,如果添加无限定符的@Async方法,那么这些方法将使用defaultExectuor线程池:

@Async
public void myDefaultExecute() {}

当然,executeA()调用将使用executorA线程池,而executeB()调用将使用executorB线程池。

希望有所帮助。