JMS配置类中可能存在的奇怪行为

时间:2016-05-09 14:39:51

标签: java spring-boot spring-jms

这是我之前在帖子中提出的另一个相关问题:Safely Terminating a Spring JMS application

我使用Spring启动的JMS应用程序正确处理所有内容并关闭而没有错误。为了实现这一点,我改变了一个bean:

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(mqConnectionFactory());
    factory.setDestinationResolver(destinationResolver());
    factory.setConcurrency("1");
    factory.setErrorHandler(errorHandler());
    factory.setSessionTransacted(true);
    factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    return factory;
}

要:

@Bean
    public DefaultMessageListenerContainer defaultMessageListenerContainer() {
        DefaultMessageListenerContainer jmsListenerContainer = new DefaultMessageListenerContainer();
        jmsListenerContainer.setConnectionFactory(mqConnectionFactory());
        jmsListenerContainer.setDestinationResolver(destinationResolver());
        jmsListenerContainer.setDestinationName(queueName);
        jmsListenerContainer.setConcurrency("1");
        jmsListenerContainer.setErrorHandler(errorHandler());
        jmsListenerContainer.setSessionTransacted(true);
        jmsListenerContainer.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        jmsListenerContainer.setAutoStartup(false);
        return jmsListenerContainer;
    }

问题在于,我本来可以创建一个"修补程序",因为我对spring的了解很少。当我偶然发现这篇文章时添加了已更改的bean jmsListenerContainer.setAutoStartup(false);中的行:http://forum.spring.io/forum/spring-projects/integration/79176-illegalstateexception-no-message-listener-specified,因为没有autoStartup设置为false我在日志中的每个已处理消息之后得到此消息:

java.lang.IllegalStateException: No message listener specified - see property 'messageListener'
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:691) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:651) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:315) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:253) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1150) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1142) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1039) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_74]

仍然可以正确处理和关闭所有内容,我只是在日志中看到这一点。不确定我的配置文件中是否存在冲突,这可能是其根源。我只是想确保这些更改不会导致问题,即使所有内容目前都按预期运行且没有错误。

最后这是我的整个配置类:

@Configuration
@EnableJms
public class MQConfig {

    private static final Logger logger = LogManager.getLogger(MQConfig.class.getName());

    @Value("${mq.hostName}")
    String host;
    @Value("${mq.port}")
    Integer port;
    @Value("${mq.queueManager}")
    String queueManager;
    @Value("${mq.queueName}")
    String queueName;
    @Value("${mq.channel}")
    String channel;

    @Autowired
    Environment environment;

    @Bean
    public DefaultMessageListenerContainer defaultMessageListenerContainer() {
        DefaultMessageListenerContainer jmsListenerContainer = new DefaultMessageListenerContainer();
        jmsListenerContainer.setConnectionFactory(mqConnectionFactory());
        jmsListenerContainer.setDestinationResolver(destinationResolver());
        jmsListenerContainer.setDestinationName(queueName);
        jmsListenerContainer.setConcurrency("1");
        jmsListenerContainer.setErrorHandler(errorHandler());
        jmsListenerContainer.setSessionTransacted(true);
        jmsListenerContainer.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        jmsListenerContainer.setAutoStartup(false);
        return jmsListenerContainer;
    }

    @Bean
    public MQConnectionFactory mqConnectionFactory() {
        MQConnectionFactory mqConnectionFactory = new MQConnectionFactory();

        try {
            mqConnectionFactory.setHostName(host);
            mqConnectionFactory.setPort(port);
            mqConnectionFactory.setQueueManager(queueManager);
            mqConnectionFactory.setTransportType(1);
        } catch (JMSException ex) {
            logger.error(ex.getStackTrace());
        }

        return mqConnectionFactory;
    }

    @Bean
    public DynamicDestinationResolver destinationResolver() {
        DynamicDestinationResolver destinationResolver = new DynamicDestinationResolver();

        try {
            Connection connection = mqConnectionFactory().createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destinationResolver.resolveDestinationName(session, queueName, false);
        } catch (JMSException ex) {
            logger.error(ex.getStackTrace());
        }

        return destinationResolver;
    }

    @Bean
    public MQQueue mqQueue() {
        MQQueue mqQueue = new MQQueue();

        try {
            mqQueue.setBaseQueueName(queueName);
            mqQueue.setBaseQueueManagerName(queueManager);
        } catch (JMSException ex) {
            logger.error(ex.getStackTrace());
        }

        return mqQueue;
    }

    @Bean
    public JmsErrorHandler errorHandler(){
        return new JmsErrorHandler();
    }

    @Bean
    public MQManager mqHoldManager(){
        return new MQManager(host, port, queueName,
                queueManager, channel);
    }

    @Bean
    public MQInitializer mqInitializer(){
        return new MQInitializer(environment);
    }


}

思考?这太乱了吗?

编辑:JMS侦听器

@Component
public class MQConsumer {

    @Resource(name = "mqHoldManager")
    private MQManager mqHoldManager;
    @Resource(name = "defaultMessageListenerContainer")
    private DefaultMessageListenerContainer listenerContainer;

    @Autowired
    MQInitializer mqInitializer;    /* To ensure this bean executes before Listener Setup not necessarily for any particular usage*/

    final Logger logger = LogManager.getLogger(MQConsumer.class.getName());
    private static ReportManager reportManager = new ReportManager();
    private static boolean isFirstQueue = true;

    @JmsListener(destination = "${mq.queueName}")
    public void processOrder(String message) throws Exception {...}

1 个答案:

答案 0 :(得分:1)

我很困惑。

DefaultJmsListenerContainerFactory用于带注释的POJO方法:

@JmsListener(...)
public void foo(String bar) {...}

工厂用于为方法创建侦听器容器。

使用您的替换配置,我认为您没有在DefaultMessageListenerContainer中配置消息监听器。

通常你会container.setMessageListner(myListener) myListenerMessageListenerSessionAwareMessageListenerMessageListenerAdapter包裹POJO听众。

autoStartup设置为false并且从不启动容器除了将容器bean添加到上下文之外什么都不做。

我看不出你怎么能得到任何有关该配置的消息。

修改

您使用的是Spring Boot吗?如果是这样,它会为您创建一个默认的jmsListenerContainerFactory - 这是我最好的猜测。

在这种情况下,您的停止代码并没有真正停止实际的容器 - 只是从未启动的“虚拟”容器。

我建议您@JmsListener id@Autowire JmsListenerEndpointRegistry并致电registry.getListenerContainer("myListener").stop()

@JmsListener(id = "myListener", destination = "${mq.queueName}")