使用Executor和WebSocket进行多线程

时间:2015-06-04 19:59:04

标签: java multithreading spring websocket spring-annotations

就我而言,我想创建多个线程执行器来处理收到的陷阱。在同一个应用程序中,我想实现websocket以使我的应用程序实时运行。

我有配置类来创建ThreadPoolExecutor,就像这个

@Configuration
@EnableAsync
@EnableScheduling
@Profile("!" + Constants.SPRING_PROFILE_FAST)
public class AsyncConfiguration implements AsyncConfigurer, EnvironmentAware {
 .......
 @Override
    @Bean
    public Executor getAsyncExecutor() {
        log.debug("Creating Async Task Executor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(propertyResolver.getProperty("corePoolSize", Integer.class, 30));
        executor.setMaxPoolSize(propertyResolver.getProperty("maxPoolSize", Integer.class, 150));
        executor.setQueueCapacity(propertyResolver.getProperty("queueCapacity", Integer.class, 10000));
        executor.setThreadNamePrefix("ems-Executor-");
        return new ExceptionHandlingAsyncTaskExecutor(executor);
    }

然后我在TrapReceiver类中使用执行器,

@Component
public class TrapReceiver extends Thread implements CommandResponder {

    @Inject
    private ApplicationContext applicationContext;

    @Inject
    private Executor executor;

    public TrapReceiver(){
    }

    List<PDUv1> listPdu = new ArrayList<PDUv1>();
    String message = "";
    long totReceivedTrap = 0;

    @PostConstruct
    public void init() {
        //create thread pool untuk memanage thread puller (thread yang pull dan save device oid value)
        System.out.println("Running trap listener");
        this.start();
    }

    public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
        PDUv1 pdu = (PDUv1) cmdRespEvent.getPDU();
        listPdu.add(pdu);
        if (pdu != null) {
            if(listPdu.size() == 3){ //3trap per thread
                List<PDUv1> temp = new ArrayList<PDUv1>();
                temp.addAll(listPdu);
                TrapInsertor trapInsertor = (TrapInsertor) applicationContext.getBean("trapInsertor");
                trapInsertor.setProperty(temp);
                executor.execute(trapInsertor);
                listPdu.clear();
            }
        }
        totReceivedTrap++;
        if(totReceivedTrap % 10000 == 0)
            System.out.println("total received trap "+totReceivedTrap);
    }

    public void run() {
        while (true) {
            try {
                this.listen(new UdpAddress(getIp()+"/162")); //where to listen
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }
    }

这段代码运行良好,但是当我尝试在我的应用程序中添加websocket功能时,应用程序出错了。当我使用@EnableWebSocketMessageBroker注释时出现错误。这是我的websocket配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketAppConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

这就是错误

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.concurrent.Executor com.satunol.ems.snmp.TrapReceiver.executor; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [java.util.concurrent.Executor] is defined: expected single matching bean but found 5: getAsyncExecutor,messageBrokerSockJsTaskScheduler,clientInboundChannelExecutor,clientOutboundChannelExecutor,brokerChannelExecutor
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 16 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [java.util.concurrent.Executor] is defined: expected single matching bean but found 5: getAsyncExecutor,messageBrokerSockJsTaskScheduler,clientInboundChannelExecutor

我的代码出了什么问题,如果我在websocket配置或者线程池中出错了,应该怎么做。

2 个答案:

答案 0 :(得分:1)

这里的问题是因为[java.util.concurrent.Executor]有多个可能的bean,Spring无法选择正确的bean。 Spring autowire机制默认基于类型。 由于WebSocket有自己的ThreadPoolTask​​Executor实现,因此您可以使用5种可能的bean。

autowire机制基于这样的假设:您将给出一个匹配的bean,而不需要任何其他注释。由于存在多个匹配bean,因此您需要告诉Spring您想要自动装配的bean。 您可以使用注释@Qualifier(“beanName”)

来实现它
@Autowired
@Qualifier("getAsyncExecutor")
private Executor executor;

希望这有帮助!

一些例子: Example of @Qualifier annotation

答案 1 :(得分:0)

这既不是多线程问题,也不是网络套接字,这与Spring及其自动布线功能有关。请参阅错误 -

  

引起:org.springframework.beans.factory。 BeanCreationException :   无法自动装配字段:private java.util.concurrent.Executor   com.satunol.ems.snmp.TrapReceiver.executor;嵌套异常是   org.springframework.beans.factory.NoUniqueBeanDefinitionException:不   定义类型为 [java.util.concurrent.Executor]的限定bean:   预期单匹配bean但发现5:   getAsyncExecutor,messageBrokerSockJsTaskScheduler,clientInboundChannelExecutor,clientOutboundChannelExecutor,brokerChannelExecutor

我的建议:删除 threadpool threadpoolexecutor 标记并添加 spring spring-annotations < / em>标签,一些Spring民众应该能够快速帮助你。