Possible Strange Behaviour in JMS Config Class
<p>This is a separate yet related question to my earlier post here: <a href="https://stackoverflow.com/questions/37077787/safely-terminating-a-spring-jms-application/37078307?noredirect=1#comment61777499_37078307">Safely Terminating a Spring JMS application</a></p>

<p>My JMS application that I have using spring boot processes everything correctly and shuts down with no errors. To get this to work I changed a bean from:</p>

<pre><code>@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;
}
</code></pre>

<p>To:</p>

<pre><code>@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;
    }
</code></pre>

<p>The problem with this is, I could have created just a "hotfix", as my knowledge about spring is little. the line in the changed bean <code>jmsListenerContainer.setAutoStartup(false);</code> was added when I stumbled upon this post: <a href="http://forum.spring.io/forum/spring-projects/integration/79176-illegalstateexception-no-message-listener-specified" rel="nofollow noreferrer">http://forum.spring.io/forum/spring-projects/integration/79176-illegalstateexception-no-message-listener-specified</a> as without the <code>autoStartup</code> set to false I get this after every processed message in my logs:</p>

<pre><code>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]
</code></pre>

<p>Everything still is processed and shut down correctly, I just see this in my logs. Not sure if there is a conflict in my config file or not that may be the root of this. I just want to make sure the changes won't cause problems, even though everything currently works as intended with no errors.</p>

<p>Lastly here is my entire configuration class:</p>

<pre><code>@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);
    }


}
</code></pre>

<p>Thoughts? Is this messy?</p>

<p>EDIT: JMS Listener</p>

<pre><code>@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 {...}
</code></pre>