org.springframework.jms.config.JmsListenerEndpointRegistry Java Examples

The following examples show how to use org.springframework.jms.config.JmsListenerEndpointRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractJmsAnnotationDrivenTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 */
public void testCustomConfiguration(ApplicationContext context) {
	JmsListenerContainerTestFactory defaultFactory =
			context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
	JmsListenerContainerTestFactory customFactory =
			context.getBean("customFactory", JmsListenerContainerTestFactory.class);
	assertEquals(1, defaultFactory.getListenerContainers().size());
	assertEquals(1, customFactory.getListenerContainers().size());
	JmsListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint();
	assertEquals("Wrong endpoint type", SimpleJmsListenerEndpoint.class, endpoint.getClass());
	assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"),
			((SimpleJmsListenerEndpoint) endpoint).getMessageListener());

	JmsListenerEndpointRegistry customRegistry =
			context.getBean("customRegistry", JmsListenerEndpointRegistry.class);
	assertEquals("Wrong number of containers in the registry", 2,
			customRegistry.getListenerContainerIds().size());
	assertEquals("Wrong number of containers in the registry", 2,
			customRegistry.getListenerContainers().size());
	assertNotNull("Container with custom id on the annotation should be found",
			customRegistry.getListenerContainer("listenerId"));
	assertNotNull("Container created with custom id should be found",
			customRegistry.getListenerContainer("myCustomEndpointId"));
}
 
Example #2
Source File: AbstractJmsAnnotationDrivenTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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
 */
public void testCustomConfiguration(ApplicationContext context) {
	JmsListenerContainerTestFactory defaultFactory =
			context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
	JmsListenerContainerTestFactory customFactory =
			context.getBean("customFactory", JmsListenerContainerTestFactory.class);
	assertEquals(1, defaultFactory.getListenerContainers().size());
	assertEquals(1, customFactory.getListenerContainers().size());
	JmsListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint();
	assertEquals("Wrong endpoint type", SimpleJmsListenerEndpoint.class, endpoint.getClass());
	assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"),
			((SimpleJmsListenerEndpoint) endpoint).getMessageListener());

	JmsListenerEndpointRegistry customRegistry =
			context.getBean("customRegistry", JmsListenerEndpointRegistry.class);
	assertEquals("Wrong number of containers in the registry", 2,
			customRegistry.getListenerContainerIds().size());
	assertEquals("Wrong number of containers in the registry", 2,
			customRegistry.getListenerContainers().size());
	assertNotNull("Container with custom id on the annotation should be found",
			customRegistry.getListenerContainer("listenerId"));
	assertNotNull("Container created with custom id should be found",
			customRegistry.getListenerContainer("myCustomEndpointId"));
}
 
Example #3
Source File: AbstractJmsAnnotationDrivenTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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
 */
public void testCustomConfiguration(ApplicationContext context) {
	JmsListenerContainerTestFactory defaultFactory =
			context.getBean("jmsListenerContainerFactory", JmsListenerContainerTestFactory.class);
	JmsListenerContainerTestFactory customFactory =
			context.getBean("customFactory", JmsListenerContainerTestFactory.class);
	assertEquals(1, defaultFactory.getListenerContainers().size());
	assertEquals(1, customFactory.getListenerContainers().size());
	JmsListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint();
	assertEquals("Wrong endpoint type", SimpleJmsListenerEndpoint.class, endpoint.getClass());
	assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"),
			((SimpleJmsListenerEndpoint) endpoint).getMessageListener());

	JmsListenerEndpointRegistry customRegistry =
			context.getBean("customRegistry", JmsListenerEndpointRegistry.class);
	assertEquals("Wrong number of containers in the registry", 2,
			customRegistry.getListenerContainerIds().size());
	assertEquals("Wrong number of containers in the registry", 2,
			customRegistry.getListenerContainers().size());
	assertNotNull("Container with custom id on the annotation should be found",
			customRegistry.getListenerContainer("listenerId"));
	assertNotNull("Container created with custom id should be found",
			customRegistry.getListenerContainer("myCustomEndpointId"));
}
 
Example #4
Source File: EnableJmsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void containerCanBeStarterViaTheRegistry() {
	ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
			EnableJmsAutoStartupFalseConfig.class, DefaultBean.class);
	JmsListenerContainerTestFactory factory =
			context.getBean(JmsListenerContainerTestFactory.class);
	MessageListenerTestContainer container = factory.getListenerContainers().get(0);
	assertFalse(container.isAutoStartup());
	assertFalse(container.isStarted());
	JmsListenerEndpointRegistry registry = context.getBean(JmsListenerEndpointRegistry.class);
	registry.start();
	assertTrue(container.isStarted());
}
 
Example #5
Source File: SearchIndexUpdateJmsMessageListener.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #6
Source File: EventRegistryJmsConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public JmsChannelModelProcessor jmsChannelDefinitionProcessor(JmsListenerEndpointRegistry endpointRegistry, JmsOperations jmsOperations) {
    JmsChannelModelProcessor jmsChannelDeployer = new JmsChannelModelProcessor();
    jmsChannelDeployer.setEndpointRegistry(endpointRegistry);
    jmsChannelDeployer.setJmsOperations(jmsOperations);

    return jmsChannelDeployer;
}
 
Example #7
Source File: HerdJmsMessageListener.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #8
Source File: SampleDataJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Bean(name = "org.springframework.jms.config.internalJmsListenerEndpointRegistry")
JmsListenerEndpointRegistry registry()
{
    //if Mockito not found return null
    try
    {
        Class.forName("org.mockito.Mockito");
    }
    catch (ClassNotFoundException ignored)
    {
        return null;
    }

    return Mockito.mock(JmsListenerEndpointRegistry.class);
}
 
Example #9
Source File: SampleDataJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #10
Source File: JmsListenerAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterSingletonsInstantiated() {
	this.registrar.setBeanFactory(this.beanFactory);

	if (this.beanFactory instanceof ListableBeanFactory) {
		Map<String, JmsListenerConfigurer> instances =
				((ListableBeanFactory) this.beanFactory).getBeansOfType(JmsListenerConfigurer.class);
		for (JmsListenerConfigurer configurer : instances.values()) {
			configurer.configureJmsListeners(this.registrar);
		}
	}

	if (this.registrar.getEndpointRegistry() == null) {
		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);
	}

	if (this.containerFactoryBeanName != null) {
		this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
	}

	// 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();
}
 
Example #11
Source File: SearchIndexUpdateJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Bean(name = "org.springframework.jms.config.internalJmsListenerEndpointRegistry")
JmsListenerEndpointRegistry registry()
{
    //if Mockito not found return null
    try
    {
        Class.forName("org.mockito.Mockito");
    }
    catch (ClassNotFoundException ignored)
    {
        return null;
    }

    return Mockito.mock(JmsListenerEndpointRegistry.class);
}
 
Example #12
Source File: SearchIndexUpdateJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #13
Source File: HerdJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Bean(name = "org.springframework.jms.config.internalJmsListenerEndpointRegistry")
JmsListenerEndpointRegistry registry()
{
    //if Mockito not found return null
    try
    {
        Class.forName("org.mockito.Mockito");
    }
    catch (ClassNotFoundException ignored)
    {
        return null;
    }

    return Mockito.mock(JmsListenerEndpointRegistry.class);
}
 
Example #14
Source File: HerdJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #15
Source File: JmsListenerAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof AopInfrastructureBean || bean instanceof JmsListenerContainerFactory ||
			bean instanceof JmsListenerEndpointRegistry) {
		// Ignore AOP infrastructure such as scoped proxies.
		return bean;
	}

	Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
	if (!this.nonAnnotatedClasses.contains(targetClass)) {
		Map<Method, Set<JmsListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
				(MethodIntrospector.MetadataLookup<Set<JmsListener>>) method -> {
					Set<JmsListener> listenerMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
							method, JmsListener.class, JmsListeners.class);
					return (!listenerMethods.isEmpty() ? listenerMethods : null);
				});
		if (annotatedMethods.isEmpty()) {
			this.nonAnnotatedClasses.add(targetClass);
			if (logger.isTraceEnabled()) {
				logger.trace("No @JmsListener annotations found on bean type: " + targetClass);
			}
		}
		else {
			// Non-empty set of methods
			annotatedMethods.forEach((method, listeners) ->
					listeners.forEach(listener -> processJmsListener(listener, method, bean)));
			if (logger.isDebugEnabled()) {
				logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName +
						"': " + annotatedMethods);
			}
		}
	}
	return bean;
}
 
Example #16
Source File: JmsListenerAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
}
 
Example #17
Source File: StoragePolicyProcessorJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Bean(name = "org.springframework.jms.config.internalJmsListenerEndpointRegistry")
JmsListenerEndpointRegistry registry()
{
    //if Mockito not found return null
    try
    {
        Class.forName("org.mockito.Mockito");
    }
    catch (ClassNotFoundException ignored)
    {
        return null;
    }

    return Mockito.mock(JmsListenerEndpointRegistry.class);
}
 
Example #18
Source File: StoragePolicyProcessorJmsMessageListenerTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #19
Source File: EnableJmsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void containerCanBeStarterViaTheRegistry() {
	ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
			EnableJmsAutoStartupFalseConfig.class, DefaultBean.class);
	JmsListenerContainerTestFactory factory =
			context.getBean(JmsListenerContainerTestFactory.class);
	MessageListenerTestContainer container = factory.getListenerContainers().get(0);
	assertFalse(container.isAutoStartup());
	assertFalse(container.isStarted());
	JmsListenerEndpointRegistry registry = context.getBean(JmsListenerEndpointRegistry.class);
	registry.start();
	assertTrue(container.isStarted());
}
 
Example #20
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
JmsListenerConfigurer configureTracing(BeanFactory beanFactory,
		JmsListenerEndpointRegistry defaultRegistry) {
	return registrar -> {
		TracingJmsBeanPostProcessor processor = beanFactory
				.getBean(TracingJmsBeanPostProcessor.class);
		JmsListenerEndpointRegistry registry = registrar.getEndpointRegistry();
		registrar.setEndpointRegistry((JmsListenerEndpointRegistry) processor
				.wrap(registry == null ? defaultRegistry : registry));
	};
}
 
Example #21
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
Object wrap(Object bean) {
	if (typeMatches(bean)) {
		return new TracingJmsListenerEndpointRegistry(
				(JmsListenerEndpointRegistry) bean, this.beanFactory);
	}
	return bean;
}
 
Example #22
Source File: JmsListenerAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	if (bean instanceof AopInfrastructureBean || bean instanceof JmsListenerContainerFactory ||
			bean instanceof JmsListenerEndpointRegistry) {
		// Ignore AOP infrastructure such as scoped proxies.
		return bean;
	}

	Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
	if (!this.nonAnnotatedClasses.contains(targetClass) &&
			AnnotationUtils.isCandidateClass(targetClass, JmsListener.class)) {
		Map<Method, Set<JmsListener>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
				(MethodIntrospector.MetadataLookup<Set<JmsListener>>) method -> {
					Set<JmsListener> listenerMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
							method, JmsListener.class, JmsListeners.class);
					return (!listenerMethods.isEmpty() ? listenerMethods : null);
				});
		if (annotatedMethods.isEmpty()) {
			this.nonAnnotatedClasses.add(targetClass);
			if (logger.isTraceEnabled()) {
				logger.trace("No @JmsListener annotations found on bean type: " + targetClass);
			}
		}
		else {
			// Non-empty set of methods
			annotatedMethods.forEach((method, listeners) ->
					listeners.forEach(listener -> processJmsListener(listener, method, bean)));
			if (logger.isDebugEnabled()) {
				logger.debug(annotatedMethods.size() + " @JmsListener methods processed on bean '" + beanName +
						"': " + annotatedMethods);
			}
		}
	}
	return bean;
}
 
Example #23
Source File: JmsListenerAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}
 
Example #24
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
private boolean typeMatches(Object bean) {
	return bean instanceof JmsListenerEndpointRegistry
			&& !(bean instanceof TracingJmsListenerEndpointRegistry);
}
 
Example #25
Source File: StoragePolicyProcessorJmsMessageListener.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example #26
Source File: SampleDataJmsMessageListener.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example #27
Source File: JmsChannelModelProcessor.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
public void setEndpointRegistry(JmsListenerEndpointRegistry endpointRegistry) {
    this.endpointRegistry = endpointRegistry;
}
 
Example #28
Source File: JmsChannelModelProcessor.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
public JmsListenerEndpointRegistry getEndpointRegistry() {
    return endpointRegistry;
}
 
Example #29
Source File: EnableJmsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Bean
public JmsListenerEndpointRegistry customRegistry() {
	return new JmsListenerEndpointRegistry();
}
 
Example #30
Source File: JmsListenerAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Bean
public JmsListenerEndpointRegistry jmsListenerEndpointRegistry() {
	return new JmsListenerEndpointRegistry();
}