Search in sources :

Example 1 with JmsListenerEndpointRegistry

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

the class HerdJmsMessageListener method controlHerdJmsMessageListener.

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

Example 2 with JmsListenerEndpointRegistry

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

the class StoragePolicyProcessorJmsMessageListener method controlStoragePolicyProcessorJmsMessageListener.

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

Example 3 with JmsListenerEndpointRegistry

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

the class SampleDataJmsMessageListener method controlSampleDataJmsMessageListener.

/**
 * Periodically check the configuration and apply the action to the storage policy processor JMS message listener service, if needed.
 */
@Scheduled(fixedDelay = 60000)
public void controlSampleDataJmsMessageListener() {
    try {
        // Get the configuration setting.
        Boolean jmsMessageListenerEnabled = Boolean.valueOf(configurationHelper.getProperty(ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED));
        // Get the registry bean.
        JmsListenerEndpointRegistry registry = ApplicationContextHolder.getApplicationContext().getBean("org.springframework.jms.config.internalJmsListenerEndpointRegistry", JmsListenerEndpointRegistry.class);
        // Get the sample data JMS message listener container.
        MessageListenerContainer jmsMessageListenerContainer = registry.getListenerContainer(HerdJmsDestinationResolver.SQS_DESTINATION_SAMPLE_DATA_QUEUE);
        // Get the current JMS message listener status and the configuration value.
        LOGGER.debug("controlStoragePolicyProcessorJmsMessageListener(): {}={} jmsMessageListenerContainer.isRunning()={}", ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED.getKey(), jmsMessageListenerEnabled, jmsMessageListenerContainer.isRunning());
        // Apply the relative action if needed.
        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);
    }
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) MessageListenerContainer(org.springframework.jms.listener.MessageListenerContainer) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) IOException(java.io.IOException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 4 with JmsListenerEndpointRegistry

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

the class SampleDataJmsMessageListenerTest method testControlListener.

@Test
public void testControlListener() {
    configurationHelper = Mockito.mock(ConfigurationHelper.class);
    ReflectionTestUtils.setField(sampleDataJmsMessageListener, "configurationHelper", configurationHelper);
    MessageListenerContainer mockMessageListenerContainer = Mockito.mock(MessageListenerContainer.class);
    // The listener is not enabled
    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);
    // the listener is not running, nothing happened
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    sampleDataJmsMessageListener.controlSampleDataJmsMessageListener();
    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);
    sampleDataJmsMessageListener.controlSampleDataJmsMessageListener();
    verify(mockMessageListenerContainer).stop();
    // The listener is enabled
    when(configurationHelper.getProperty(ConfigurationValue.SAMPLE_DATA_JMS_LISTENER_ENABLED)).thenReturn("true");
    // the listener is running, should not call the start method
    when(mockMessageListenerContainer.isRunning()).thenReturn(true);
    sampleDataJmsMessageListener.controlSampleDataJmsMessageListener();
    verify(mockMessageListenerContainer, Mockito.times(0)).start();
    // the listener is not running, but it is enabled, should start
    when(mockMessageListenerContainer.isRunning()).thenReturn(false);
    sampleDataJmsMessageListener.controlSampleDataJmsMessageListener();
    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 5 with JmsListenerEndpointRegistry

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

the class JmsListenerAnnotationBeanPostProcessor method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    // Remove resolved singleton classes from cache
    this.nonAnnotatedClasses.clear();
    if (this.beanFactory instanceof ListableBeanFactory) {
        // Apply JmsListenerConfigurer beans from the BeanFactory, if any
        Map<String, JmsListenerConfigurer> beans = ((ListableBeanFactory) this.beanFactory).getBeansOfType(JmsListenerConfigurer.class);
        List<JmsListenerConfigurer> configurers = new ArrayList<>(beans.values());
        AnnotationAwareOrderComparator.sort(configurers);
        for (JmsListenerConfigurer configurer : configurers) {
            configurer.configureJmsListeners(this.registrar);
        }
    }
    if (this.containerFactoryBeanName != null) {
        this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
    }
    if (this.registrar.getEndpointRegistry() == null) {
        // Determine JmsListenerEndpointRegistry bean from the BeanFactory
        if (this.endpointRegistry == null) {
            Assert.state(this.beanFactory != null, "BeanFactory must be set to find endpoint registry by bean name");
            this.endpointRegistry = this.beanFactory.getBean(JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME, JmsListenerEndpointRegistry.class);
        }
        this.registrar.setEndpointRegistry(this.endpointRegistry);
    }
    // Set the custom handler method factory once resolved by the configurer
    MessageHandlerMethodFactory handlerMethodFactory = this.registrar.getMessageHandlerMethodFactory();
    if (handlerMethodFactory != null) {
        this.messageHandlerMethodFactory.setMessageHandlerMethodFactory(handlerMethodFactory);
    }
    // Actually register all listeners
    this.registrar.afterPropertiesSet();
}
Also used : JmsListenerEndpointRegistry(org.springframework.jms.config.JmsListenerEndpointRegistry) ArrayList(java.util.ArrayList) DefaultMessageHandlerMethodFactory(org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory) MessageHandlerMethodFactory(org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory)

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