SampleDataJmsMessageListener { @Scheduled(fixedDelay = 60000) public void controlSampleDataJmsMessageListener() { try { Boolean jmsMessageListenerEnabled = Boolean.valueOf(configurationHelper.getProperty(ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED)); JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext() .getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class); MessageListenerContainer jmsMessageListenerContainer = registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_SAMPLE_DATA_QUEUE); LOGGER.debug("controlStoragePolicyProcessorJmsMessageListener(): {}={} jmsMessageListenerContainer.isRunning()={}", ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED.getKey(), jmsMessageListenerEnabled, jmsMessageListenerContainer.isRunning()); if (!jmsMessageListenerEnabled && jmsMessageListenerContainer.isRunning()) { LOGGER.info("controlSampleDataJmsMessageListener(): Stopping the sample data JMS message listener ..."); jmsMessageListenerContainer.stop(); LOGGER.info("controlSampleDataJmsMessageListener(): Done"); } else if (jmsMessageListenerEnabled && !jmsMessageListenerContainer.isRunning()) { LOGGER.info("controlSampleDataJmsMessageListener(): Starting the sample data JMS message listener ..."); jmsMessageListenerContainer.start(); LOGGER.info("controlSampleDataJmsMessageListener(): Done"); } } catch (Exception e) { LOGGER.error("controlSampleDataJmsMessageListener(): Failed to control the sample data Jms message listener service.", e); } }  @Scheduled(fixedDelay = 60000) void controlSampleDataJmsMessageListener(); @JmsListener(id = HerdJmsDestinationResolver.SQS_DESTINATION_SAMPLE_DATA_QUEUE, containerFactory = "jmsListenerContainerFactory", destination = HerdJmsDestinationResolver.SQS_DESTINATION_SAMPLE_DATA_QUEUE) void processMessage(String payload, @Headers Map<Object, Object> allHeaders);  }
@Test public void testControlListener() { configurationHelper = Mockito.mock(ConfigurationHelper.class); ReflectionTestUtils.setField(sampleDataJmsMessageListener, "configurationHelper", configurationHelper); MessageListenerContainer mockMessageListenerContainer = Mockito.mock(MessageListenerContainer.class); when(configurationHelper.getProperty(ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED)).thenReturn("false"); JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext() .getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class); when(registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_SAMPLE_DATA_QUEUE)).thenReturn(mockMessageListenerContainer); when(mockMessageListenerContainer.isRunning()).thenReturn(false); sampleDataJmsMessageListener.controlSampleDataJmsMessageListener(); verify(mockMessageListenerContainer, Mockito.times(0)).stop(); verify(mockMessageListenerContainer, Mockito.times(0)).start(); when(mockMessageListenerContainer.isRunning()).thenReturn(true); sampleDataJmsMessageListener.controlSampleDataJmsMessageListener(); verify(mockMessageListenerContainer).stop(); when(configurationHelper.getProperty(ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED)).thenReturn("true"); when(mockMessageListenerContainer.isRunning()).thenReturn(true); sampleDataJmsMessageListener.controlSampleDataJmsMessageListener(); verify(mockMessageListenerContainer, Mockito.times(0)).start(); when(mockMessageListenerContainer.isRunning()).thenReturn(false); sampleDataJmsMessageListener.controlSampleDataJmsMessageListener(); verify(mockMessageListenerContainer).start(); }