Search in sources :

Example 6 with JmsListenerEndpointRegistry

use of org.springframework.jms.config.JmsListenerEndpointRegistry in project spring-framework by spring-projects.

the class AbstractJmsAnnotationDrivenTests method testCustomConfiguration.

/**
 * Test for {@link CustomBean} and an manually endpoint registered
 * with "myCustomEndpointId". The custom endpoint does not provide
 * any factory so it's registered with the default one
 */
protected void testCustomConfiguration(ApplicationContext context) {
    JmsListenerContainerTestFactory defaultFactory = context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
    JmsListenerContainerTestFactory customFactory = context.getBean("customFactory", JmsListenerContainerTestFactory.class);
    assertThat(defaultFactory.getListenerContainers().size()).isEqualTo(1);
    assertThat(customFactory.getListenerContainers().size()).isEqualTo(1);
    JmsListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint();
    assertThat(endpoint.getClass()).as("Wrong endpoint type").isEqualTo(SimpleJmsListenerEndpoint.class);
    assertThat(((SimpleJmsListenerEndpoint) endpoint).getMessageListener()).as("Wrong listener set in custom endpoint").isEqualTo(context.getBean("simpleMessageListener"));
    JmsListenerEndpointRegistry customRegistry = context.getBean("customRegistry", JmsListenerEndpointRegistry.class);
    assertThat(customRegistry.getListenerContainerIds().size()).as("Wrong number of containers in the registry").isEqualTo(2);
    assertThat(customRegistry.getListenerContainers().size()).as("Wrong number of containers in the registry").isEqualTo(2);
    assertThat(customRegistry.getListenerContainer("listenerId")).as("Container with custom id on the annotation should be found").isNotNull();
    assertThat(customRegistry.getListenerContainer("myCustomEndpointId")).as("Container created with custom id should be found").isNotNull();
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) JmsListenerEndpoint(org.springframework.jms.config.JmsListenerEndpoint) MethodJmsListenerEndpoint(org.springframework.jms.config.MethodJmsListenerEndpoint) SimpleJmsListenerEndpoint(org.springframework.jms.config.SimpleJmsListenerEndpoint) JmsListenerContainerTestFactory(org.springframework.jms.config.JmsListenerContainerTestFactory)

Example 7 with JmsListenerEndpointRegistry

use of org.springframework.jms.config.JmsListenerEndpointRegistry in project herd by FINRAOS.

the class SearchIndexUpdateJmsMessageListener method controlSearchIndexUpdateJmsMessageListener.

/**
 * Periodically check the configuration and apply the action to the storage policy processor JMS message listener service, if needed.
 */
@Scheduled(fixedDelay = 60000)
public void controlSearchIndexUpdateJmsMessageListener() {
    try {
        // Get the configuration setting.
        Boolean jmsMessageListenerEnabled = Boolean.valueOf(configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_JMS_LISTENER_ENABLED));
        // Get the registry bean.
        JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext().getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class);
        // Get the search index update JMS message listener container.
        MessageListenerContainer jmsMessageListenerContainer = registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_SEARCH_INDEX_UPDATE_QUEUE);
        // Get the current JMS message listener status and the configuration value.
        LOGGER.debug("controlSearchIndexUpdateJmsMessageListener(): {}={} jmsMessageListenerContainer.isRunning()={}", ConfigurationValue.SEARCH_INDEX_UPDATE_JMS_LISTENER_ENABLED.getKey(), jmsMessageListenerEnabled, jmsMessageListenerContainer.isRunning());
        // Apply the relative action if needed.
        if (!jmsMessageListenerEnabled && jmsMessageListenerContainer.isRunning()) {
            LOGGER.info("controlSearchIndexUpdateJmsMessageListener(): Stopping the search index update JMS message listener ...");
            jmsMessageListenerContainer.stop();
            LOGGER.info("controlSearchIndexUpdateJmsMessageListener(): Done");
        } else if (jmsMessageListenerEnabled && !jmsMessageListenerContainer.isRunning()) {
            LOGGER.info("controlSearchIndexUpdateJmsMessageListener(): Starting the search index update JMS message listener ...");
            jmsMessageListenerContainer.start();
            LOGGER.info("controlSearchIndexUpdateJmsMessageListener(): Done");
        }
    } catch (Exception e) {
        LOGGER.error("controlSearchIndexUpdateJmsMessageListener(): Failed to control the search index update Jms message listener service.", e);
    }
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) MessageListenerContainer(org.springframework.jms.listener.MessageListenerContainer) IOException(java.io.IOException) ListenerExecutionFailedException(org.springframework.jms.listener.adapter.ListenerExecutionFailedException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 8 with JmsListenerEndpointRegistry

use of org.springframework.jms.config.JmsListenerEndpointRegistry in project herd by FINRAOS.

the class HerdJmsMessageListenerTest method testControlListener.

@Test
public void testControlListener() {
    configurationHelper = Mockito.mock(ConfigurationHelper.class);
    ReflectionTestUtils.setField(herdJmsMessageListener, "configurationHelper", configurationHelper);
    MessageListenerContainer mockMessageListenerContainer = Mockito.mock(MessageListenerContainer.class);
    // The listener is not enabled
    when(configurationHelper.getProperty(ConfigurationValue.JMS_LISTENER_ENABLED)).thenReturn("false");
    JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext().getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class);
    when(registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_HERD_INCOMING)).thenReturn(mockMessageListenerContainer);
    // the listener is not running, nothing happened
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    herdJmsMessageListener.controlHerdJmsMessageListener();
    verify(mockMessageListenerContainer, Mockito.times(0)).stop();
    verify(mockMessageListenerContainer, Mockito.times(0)).start();
    // the listener is running, but it is not enable, should stop
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    herdJmsMessageListener.controlHerdJmsMessageListener();
    verify(mockMessageListenerContainer).stop();
    // The listener is enabled
    when(configurationHelper.getProperty(ConfigurationValue.JMS_LISTENER_ENABLED)).thenReturn("true");
    // the listener is running, should not call the start method
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    herdJmsMessageListener.controlHerdJmsMessageListener();
    verify(mockMessageListenerContainer, Mockito.times(0)).start();
    // the listener is not running, but it is enabled, should start
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    herdJmsMessageListener.controlHerdJmsMessageListener();
    verify(mockMessageListenerContainer).start();
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) ConfigurationHelper(org.finra.herd.core.helper.ConfigurationHelper) MessageListenerContainer(org.springframework.jms.listener.MessageListenerContainer) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest) Test(org.junit.Test)

Example 9 with JmsListenerEndpointRegistry

use of org.springframework.jms.config.JmsListenerEndpointRegistry in project herd by FINRAOS.

the class StoragePolicyProcessorJmsMessageListenerTest method testControlListener.

@Test
public void testControlListener() {
    configurationHelper = Mockito.mock(ConfigurationHelper.class);
    ReflectionTestUtils.setField(storagePolicyProcessorJmsMessageListener, "configurationHelper", configurationHelper);
    MessageListenerContainer mockMessageListenerContainer = Mockito.mock(MessageListenerContainer.class);
    // The listener is not enabled
    when(configurationHelper.getProperty(ConfigurationValue.STORAGE_POLICY_PROCESSOR_JMS_LISTENER_ENABLED)).thenReturn("false");
    JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext().getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class);
    when(registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_STORAGE_POLICY_SELECTOR_JOB_SQS_QUEUE)).thenReturn(mockMessageListenerContainer);
    // the listener is not running, nothing happened
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    storagePolicyProcessorJmsMessageListener.controlStoragePolicyProcessorJmsMessageListener();
    verify(mockMessageListenerContainer, Mockito.times(0)).stop();
    verify(mockMessageListenerContainer, Mockito.times(0)).start();
    // the listener is running, but it is not enable, should stop
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    storagePolicyProcessorJmsMessageListener.controlStoragePolicyProcessorJmsMessageListener();
    verify(mockMessageListenerContainer).stop();
    // The listener is enabled
    when(configurationHelper.getProperty(ConfigurationValue.STORAGE_POLICY_PROCESSOR_JMS_LISTENER_ENABLED)).thenReturn("true");
    // the listener is running, should not call the start method
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    storagePolicyProcessorJmsMessageListener.controlStoragePolicyProcessorJmsMessageListener();
    verify(mockMessageListenerContainer, Mockito.times(0)).start();
    // the listener is not running, but it is enabled, should start
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    storagePolicyProcessorJmsMessageListener.controlStoragePolicyProcessorJmsMessageListener();
    verify(mockMessageListenerContainer).start();
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) ConfigurationHelper(org.finra.herd.core.helper.ConfigurationHelper) MessageListenerContainer(org.springframework.jms.listener.MessageListenerContainer) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest) Test(org.junit.Test)

Example 10 with JmsListenerEndpointRegistry

use of org.springframework.jms.config.JmsListenerEndpointRegistry in project herd by FINRAOS.

the class SearchIndexUpdateJmsMessageListenerTest method testControlListener.

@Test
public void testControlListener() {
    ReflectionTestUtils.setField(searchIndexUpdateJmsMessageListener, "configurationHelper", configurationHelper);
    MessageListenerContainer mockMessageListenerContainer = Mockito.mock(MessageListenerContainer.class);
    // The listener is not enabled
    when(configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_JMS_LISTENER_ENABLED)).thenReturn("false");
    JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext().getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class);
    when(registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_SEARCH_INDEX_UPDATE_QUEUE)).thenReturn(mockMessageListenerContainer);
    // The listener is not running, nothing happened
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    searchIndexUpdateJmsMessageListener.controlSearchIndexUpdateJmsMessageListener();
    verify(mockMessageListenerContainer, times(0)).stop();
    verify(mockMessageListenerContainer, times(0)).start();
    // The listener is running, but it is not enable, should stop
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    searchIndexUpdateJmsMessageListener.controlSearchIndexUpdateJmsMessageListener();
    verify(mockMessageListenerContainer, times(1)).stop();
    // The listener is enabled
    when(configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_JMS_LISTENER_ENABLED)).thenReturn("true");
    // The listener is running, should not call the start method
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    searchIndexUpdateJmsMessageListener.controlSearchIndexUpdateJmsMessageListener();
    verify(mockMessageListenerContainer, times(0)).start();
    // The listener is not running, but it is enabled, should start
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    searchIndexUpdateJmsMessageListener.controlSearchIndexUpdateJmsMessageListener();
    verify(mockMessageListenerContainer, times(1)).start();
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) MessageListenerContainer(org.springframework.jms.listener.MessageListenerContainer) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest) Test(org.junit.Test)

Aggregations

JmsListenerEndpointRegistry (org.springframework.jms.config.JmsListenerEndpointRegistry)11 MessageListenerContainer (org.springframework.jms.listener.MessageListenerContainer)8 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)4 Test (org.junit.Test)4 Scheduled (org.springframework.scheduling.annotation.Scheduled)4 IOException (java.io.IOException)3 ConfigurationHelper (org.finra.herd.core.helper.ConfigurationHelper)3 JmsListenerContainerTestFactory (org.springframework.jms.config.JmsListenerContainerTestFactory)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1 Test (org.junit.jupiter.api.Test)1 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 JmsListenerEndpoint (org.springframework.jms.config.JmsListenerEndpoint)1 MessageListenerTestContainer (org.springframework.jms.config.MessageListenerTestContainer)1 MethodJmsListenerEndpoint (org.springframework.jms.config.MethodJmsListenerEndpoint)1 SimpleJmsListenerEndpoint (org.springframework.jms.config.SimpleJmsListenerEndpoint)1 ListenerExecutionFailedException (org.springframework.jms.listener.adapter.ListenerExecutionFailedException)1