org.springframework.jms.listener.MessageListenerContainer Java Examples

The following examples show how to use org.springframework.jms.listener.MessageListenerContainer. 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: JmsListenerEndpointRegistry.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create and start a new container using the specified factory.
 */
protected MessageListenerContainer createListenerContainer(JmsListenerEndpoint endpoint,
		JmsListenerContainerFactory<?> factory) {

	MessageListenerContainer listenerContainer = factory.createListenerContainer(endpoint);

	if (listenerContainer instanceof InitializingBean) {
		try {
			((InitializingBean) listenerContainer).afterPropertiesSet();
		}
		catch (Exception ex) {
			throw new BeanInitializationException("Failed to initialize message listener container", ex);
		}
	}

	int containerPhase = listenerContainer.getPhase();
	if (containerPhase < Integer.MAX_VALUE) {  // a custom phase value
		if (this.phase < Integer.MAX_VALUE && this.phase != containerPhase) {
			throw new IllegalStateException("Encountered phase mismatch between container factory definitions: " +
					this.phase + " vs " + containerPhase);
		}
		this.phase = listenerContainer.getPhase();
	}

	return listenerContainer;
}
 
Example #2
Source File: JmsListenerEndpointRegistry.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create and start a new container using the specified factory.
 */
protected MessageListenerContainer createListenerContainer(JmsListenerEndpoint endpoint,
		JmsListenerContainerFactory<?> factory) {

	MessageListenerContainer listenerContainer = factory.createListenerContainer(endpoint);

	if (listenerContainer instanceof InitializingBean) {
		try {
			((InitializingBean) listenerContainer).afterPropertiesSet();
		}
		catch (Exception ex) {
			throw new BeanInitializationException("Failed to initialize message listener container", ex);
		}
	}

	int containerPhase = listenerContainer.getPhase();
	if (containerPhase < Integer.MAX_VALUE) {  // a custom phase value
		if (this.phase < Integer.MAX_VALUE && this.phase != containerPhase) {
			throw new IllegalStateException("Encountered phase mismatch between container factory definitions: " +
					this.phase + " vs " + containerPhase);
		}
		this.phase = listenerContainer.getPhase();
	}

	return listenerContainer;
}
 
Example #3
Source File: JmsListenerEndpointRegistry.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a message listener container for the given {@link JmsListenerEndpoint}.
 * <p>This create the necessary infrastructure to honor that endpoint
 * with regards to its configuration.
 * <p>The {@code startImmediately} flag determines if the container should be
 * started immediately.
 * @param endpoint the endpoint to add
 * @param factory the listener factory to use
 * @param startImmediately start the container immediately if necessary
 * @see #getListenerContainers()
 * @see #getListenerContainer(String)
 */
public void registerListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory,
		boolean startImmediately) {

	Assert.notNull(endpoint, "Endpoint must not be null");
	Assert.notNull(factory, "Factory must not be null");
	String id = endpoint.getId();
	Assert.hasText(id, "Endpoint id must be set");

	synchronized (this.listenerContainers) {
		if (this.listenerContainers.containsKey(id)) {
			throw new IllegalStateException("Another endpoint is already registered with id '" + id + "'");
		}
		MessageListenerContainer container = createListenerContainer(endpoint, factory);
		this.listenerContainers.put(id, container);
		if (startImmediately) {
			startIfNecessary(container);
		}
	}
}
 
Example #4
Source File: JmsListenerEndpointRegistry.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a message listener container for the given {@link JmsListenerEndpoint}.
 * <p>This create the necessary infrastructure to honor that endpoint
 * with regards to its configuration.
 * <p>The {@code startImmediately} flag determines if the container should be
 * started immediately.
 * @param endpoint the endpoint to add
 * @param factory the listener factory to use
 * @param startImmediately start the container immediately if necessary
 * @see #getListenerContainers()
 * @see #getListenerContainer(String)
 */
public void registerListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory,
		boolean startImmediately) {

	Assert.notNull(endpoint, "Endpoint must not be null");
	Assert.notNull(factory, "Factory must not be null");

	String id = endpoint.getId();
	Assert.notNull(id, "Endpoint id must not be null");
	synchronized (this.listenerContainers) {
		Assert.state(!this.listenerContainers.containsKey(id),
				"Another endpoint is already registered with id '" + id + "'");
		MessageListenerContainer container = createListenerContainer(endpoint, factory);
		this.listenerContainers.put(id, container);
		if (startImmediately) {
			startIfNecessary(container);
		}
	}
}
 
Example #5
Source File: JmsListenerEndpointRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a message listener container for the given {@link JmsListenerEndpoint}.
 * <p>This create the necessary infrastructure to honor that endpoint
 * with regards to its configuration.
 * <p>The {@code startImmediately} flag determines if the container should be
 * started immediately.
 * @param endpoint the endpoint to add
 * @param factory the listener factory to use
 * @param startImmediately start the container immediately if necessary
 * @see #getListenerContainers()
 * @see #getListenerContainer(String)
 */
public void registerListenerContainer(JmsListenerEndpoint endpoint, JmsListenerContainerFactory<?> factory,
		boolean startImmediately) {

	Assert.notNull(endpoint, "Endpoint must not be null");
	Assert.notNull(factory, "Factory must not be null");
	String id = endpoint.getId();
	Assert.hasText(id, "Endpoint id must be set");

	synchronized (this.listenerContainers) {
		if (this.listenerContainers.containsKey(id)) {
			throw new IllegalStateException("Another endpoint is already registered with id '" + id + "'");
		}
		MessageListenerContainer container = createListenerContainer(endpoint, factory);
		this.listenerContainers.put(id, container);
		if (startImmediately) {
			startIfNecessary(container);
		}
	}
}
 
Example #6
Source File: JmsListenerEndpointRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create and start a new container using the specified factory.
 */
protected MessageListenerContainer createListenerContainer(JmsListenerEndpoint endpoint,
		JmsListenerContainerFactory<?> factory) {

	MessageListenerContainer listenerContainer = factory.createListenerContainer(endpoint);

	if (listenerContainer instanceof InitializingBean) {
		try {
			((InitializingBean) listenerContainer).afterPropertiesSet();
		}
		catch (Exception ex) {
			throw new BeanInitializationException("Failed to initialize message listener container", ex);
		}
	}

	int containerPhase = listenerContainer.getPhase();
	if (containerPhase < Integer.MAX_VALUE) {  // a custom phase value
		if (this.phase < Integer.MAX_VALUE && this.phase != containerPhase) {
			throw new IllegalStateException("Encountered phase mismatch between container factory definitions: " +
					this.phase + " vs " + containerPhase);
		}
		this.phase = listenerContainer.getPhase();
	}

	return listenerContainer;
}
 
Example #7
Source File: StubRunnerJmsConfiguration.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private void registerContainers(ConfigurableListableBeanFactory beanFactory,
		List<Contract> matchingContracts, String flowName,
		StubRunnerJmsRouter listener) {
	// listener's container
	ConnectionFactory connectionFactory = beanFactory
			.getBean(ConnectionFactory.class);
	for (Contract matchingContract : matchingContracts) {
		if (matchingContract.getInput() == null) {
			continue;
		}
		String destination = MapConverter.getStubSideValuesForNonBody(
				matchingContract.getInput().getMessageFrom()).toString();
		MessageListenerContainer container = listenerContainer(destination,
				connectionFactory, listener);
		String containerName = flowName + ".container";
		Object initializedContainer = beanFactory.initializeBean(container,
				containerName);
		beanFactory.registerSingleton(containerName, initializedContainer);
	}
}
 
Example #8
Source File: JmsListenerEndpointRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void stop(Runnable callback) {
	Collection<MessageListenerContainer> listenerContainers = getListenerContainers();
	AggregatingCallback aggregatingCallback = new AggregatingCallback(listenerContainers.size(), callback);
	for (MessageListenerContainer listenerContainer : listenerContainers) {
		listenerContainer.stop(aggregatingCallback);
	}
}
 
Example #9
Source File: MethodJmsListenerEndpoint.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected MessagingMessageListenerAdapter createMessageListener(MessageListenerContainer container) {
	Assert.state(this.messageHandlerMethodFactory != null,
			"Could not create message listener - MessageHandlerMethodFactory not set");
	MessagingMessageListenerAdapter messageListener = createMessageListenerInstance();
	InvocableHandlerMethod invocableHandlerMethod =
			this.messageHandlerMethodFactory.createInvocableHandlerMethod(getBean(), getMethod());
	messageListener.setHandlerMethod(invocableHandlerMethod);
	String responseDestination = getDefaultResponseDestination();
	if (StringUtils.hasText(responseDestination)) {
		if (container.isReplyPubSubDomain()) {
			messageListener.setDefaultResponseTopicName(responseDestination);
		}
		else {
			messageListener.setDefaultResponseQueueName(responseDestination);
		}
	}
	MessageConverter messageConverter = container.getMessageConverter();
	if (messageConverter != null) {
		messageListener.setMessageConverter(messageConverter);
	}
	DestinationResolver destinationResolver = container.getDestinationResolver();
	if (destinationResolver != null) {
		messageListener.setDestinationResolver(destinationResolver);
	}
	return messageListener;
}
 
Example #10
Source File: JmsListenerEndpointRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer instanceof DisposableBean) {
			try {
				((DisposableBean) listenerContainer).destroy();
			}
			catch (Throwable ex) {
				logger.warn("Failed to destroy message listener container", ex);
			}
		}
	}
}
 
Example #11
Source File: AbstractJmsListenerEndpoint.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setupListenerContainer(MessageListenerContainer listenerContainer) {
	if (listenerContainer instanceof AbstractMessageListenerContainer) {
		setupJmsListenerContainer((AbstractMessageListenerContainer) listenerContainer);
	}
	else {
		new JcaEndpointConfigurer().configureEndpoint(listenerContainer);
	}
}
 
Example #12
Source File: MethodJmsListenerEndpointTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private MessagingMessageListenerAdapter createInstance(
		DefaultMessageHandlerMethodFactory factory, Method method, MessageListenerContainer container) {

	MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint();
	endpoint.setBean(this.sample);
	endpoint.setMethod(method);
	endpoint.setMessageHandlerMethodFactory(factory);
	return endpoint.createMessageListener(container);
}
 
Example #13
Source File: JmsListenerEndpointTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void setupMessageContainerUnsupportedContainer() {
	MessageListenerContainer container = mock(MessageListenerContainer.class);
	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	endpoint.setMessageListener(new MessageListenerAdapter());

	thrown.expect(IllegalArgumentException.class);
	endpoint.setupListenerContainer(container);
}
 
Example #14
Source File: MethodJmsListenerEndpoint.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected MessagingMessageListenerAdapter createMessageListener(MessageListenerContainer container) {
	Assert.state(this.messageHandlerMethodFactory != null,
			"Could not create message listener - MessageHandlerMethodFactory not set");
	MessagingMessageListenerAdapter messageListener = createMessageListenerInstance();
	InvocableHandlerMethod invocableHandlerMethod =
			this.messageHandlerMethodFactory.createInvocableHandlerMethod(getBean(), getMethod());
	messageListener.setHandlerMethod(invocableHandlerMethod);
	String responseDestination = getDefaultResponseDestination();
	if (StringUtils.hasText(responseDestination)) {
		if (container.isReplyPubSubDomain()) {
			messageListener.setDefaultResponseTopicName(responseDestination);
		}
		else {
			messageListener.setDefaultResponseQueueName(responseDestination);
		}
	}
	QosSettings responseQosSettings = container.getReplyQosSettings();
	if (responseQosSettings != null) {
		messageListener.setResponseQosSettings(responseQosSettings);
	}
	MessageConverter messageConverter = container.getMessageConverter();
	if (messageConverter != null) {
		messageListener.setMessageConverter(messageConverter);
	}
	DestinationResolver destinationResolver = container.getDestinationResolver();
	if (destinationResolver != null) {
		messageListener.setDestinationResolver(destinationResolver);
	}
	return messageListener;
}
 
Example #15
Source File: JmsListenerEndpointRegistry.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void destroy() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer instanceof DisposableBean) {
			try {
				((DisposableBean) listenerContainer).destroy();
			}
			catch (Throwable ex) {
				logger.warn("Failed to destroy message listener container", ex);
			}
		}
	}
}
 
Example #16
Source File: JmsListenerEndpointRegistry.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean isRunning() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer.isRunning()) {
			return true;
		}
	}
	return false;
}
 
Example #17
Source File: JmsListenerEndpointRegistry.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean isRunning() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer.isRunning()) {
			return true;
		}
	}
	return false;
}
 
Example #18
Source File: JmsListenerEndpointRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isRunning() {
	for (MessageListenerContainer listenerContainer : getListenerContainers()) {
		if (listenerContainer.isRunning()) {
			return true;
		}
	}
	return false;
}
 
Example #19
Source File: AbstractJmsListenerEndpoint.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void setupListenerContainer(MessageListenerContainer listenerContainer) {
	if (listenerContainer instanceof AbstractMessageListenerContainer) {
		setupJmsListenerContainer((AbstractMessageListenerContainer) listenerContainer);
	}
	else {
		new JcaEndpointConfigurer().configureEndpoint(listenerContainer);
	}
}
 
Example #20
Source File: JmsListenerEndpointTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void setupMessageContainerUnsupportedContainer() {
	MessageListenerContainer container = mock(MessageListenerContainer.class);
	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	endpoint.setMessageListener(new MessageListenerAdapter());

	thrown.expect(IllegalArgumentException.class);
	endpoint.setupListenerContainer(container);
}
 
Example #21
Source File: MethodJmsListenerEndpointTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private MessagingMessageListenerAdapter createInstance(
		DefaultMessageHandlerMethodFactory factory, Method method, MessageListenerContainer container) {

	MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint();
	endpoint.setBean(sample);
	endpoint.setMethod(method);
	endpoint.setMessageHandlerMethodFactory(factory);
	return endpoint.createMessageListener(container);
}
 
Example #22
Source File: MockServerConfig.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public MessageListenerContainer mockServerListener(ActiveMQConnectionFactory connectionFactory) {
  DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
  container.setConnectionFactory(connectionFactory);
  container.setDestination(new ActiveMQQueue("c2mon.client.request"));
  container.setMessageListener((SessionAwareMessageListener) (message, session) -> {
    session.createProducer(message.getJMSReplyTo()).send(session.createTextMessage("[]"));
  });
  return container;
}
 
Example #23
Source File: StubRunnerJmsConfiguration.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private MessageListenerContainer listenerContainer(String queueName,
		ConnectionFactory connectionFactory, MessageListener listener) {
	DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
	container.setConnectionFactory(connectionFactory);
	container.setDestinationName(queueName);
	container.setMessageListener(listener);
	return container;
}
 
Example #24
Source File: JmsChannelModelProcessor.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void unregisterChannelModel(ChannelModel channelModel, String tenantId, EventRepositoryService eventRepositoryService) {
    String endpointId = getEndpointId(channelModel,tenantId);
    // currently it is not possible to unregister a listener container
    // In order not to do a lot of the logic that Spring does we are manually accessing the containers to remove them
    // see https://github.com/spring-projects/spring-framework/issues/24228
    MessageListenerContainer listenerContainer = endpointRegistry.getListenerContainer(endpointId);
    if (listenerContainer != null) {
        listenerContainer.stop();
    }

    if (listenerContainer instanceof DisposableBean) {
        try {
            ((DisposableBean) listenerContainer).destroy();
        } catch (Exception e) {
            throw new RuntimeException("Failed to destroy listener container", e);
        }
    }

    Field listenerContainersField = ReflectionUtils.findField(endpointRegistry.getClass(), "listenerContainers");
    if (listenerContainersField != null) {
        listenerContainersField.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<String, MessageListenerContainer> listenerContainers = (Map<String, MessageListenerContainer>) ReflectionUtils.getField(listenerContainersField, endpointRegistry);
        if (listenerContainers != null) {
            listenerContainers.remove(endpointId);
        }
    } else {
        throw new IllegalStateException("Endpoint registry " + endpointRegistry + " does not have listenerContainers field");
    }
}
 
Example #25
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 #26
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 #27
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 #28
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 #29
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 #30
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();
}