Search in sources :

Example 6 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class SubscribableJmsChannelTests method dispatcherHasNoSubscribersTopic.

@Test
public void dispatcherHasNoSubscribersTopic() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(true);
    factoryBean.setConnectionFactory(this.connectionFactory);
    factoryBean.setDestinationName("noSubscribersTopic");
    factoryBean.setBeanName("noSubscribersChannel");
    factoryBean.setPubSubDomain(true);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();
    AbstractMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container", AbstractMessageListenerContainer.class);
    MessageListener listener = (MessageListener) container.getMessageListener();
    List<String> logList = insertMockLoggerInListener(channel);
    listener.onMessage(new StubTextMessage("Hello, world!"));
    verifyLogReceived(logList);
}
Also used : JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(javax.jms.MessageListener) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AbstractMessageListenerContainer(org.springframework.jms.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 7 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class SubscribableJmsChannelTests method dispatcherHasNoSubscribersQueue.

@Test
public void dispatcherHasNoSubscribersQueue() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(true);
    factoryBean.setConnectionFactory(this.connectionFactory);
    factoryBean.setDestinationName("noSubscribersQueue");
    factoryBean.setBeanName("noSubscribersChannel");
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();
    AbstractMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container", AbstractMessageListenerContainer.class);
    MessageListener listener = (MessageListener) container.getMessageListener();
    try {
        listener.onMessage(new StubTextMessage("Hello, world!"));
        fail("Exception expected");
    } catch (MessageDeliveryException e) {
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for jms-channel 'noSubscribersChannel'."));
    }
}
Also used : JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(javax.jms.MessageListener) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AbstractMessageListenerContainer(org.springframework.jms.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 8 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class SubscribableJmsChannelTests method topicName.

@Test
public void topicName() throws Exception {
    final CountDownLatch latch = new CountDownLatch(4);
    final List<Message<?>> receivedList1 = Collections.synchronizedList(new ArrayList<Message<?>>());
    MessageHandler handler1 = message -> {
        receivedList1.add(message);
        latch.countDown();
    };
    final List<Message<?>> receivedList2 = Collections.synchronizedList(new ArrayList<Message<?>>());
    MessageHandler handler2 = message -> {
        receivedList2.add(message);
        latch.countDown();
    };
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(true);
    factoryBean.setConnectionFactory(this.connectionFactory);
    factoryBean.setDestinationName("dynamicTopic");
    factoryBean.setPubSubDomain(true);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();
    channel.start();
    if (!waitUntilRegisteredWithDestination(channel, 10000)) {
        fail("Listener failed to subscribe to topic");
    }
    channel.subscribe(handler1);
    channel.subscribe(handler2);
    channel.send(new GenericMessage<String>("foo"));
    channel.send(new GenericMessage<String>("bar"));
    latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
    assertEquals(2, receivedList1.size());
    assertEquals("foo", receivedList1.get(0).getPayload());
    assertEquals("bar", receivedList1.get(1).getPayload());
    assertEquals(2, receivedList2.size());
    assertEquals("foo", receivedList2.get(0).getPayload());
    assertEquals("bar", receivedList2.get(1).getPayload());
    channel.stop();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageListener(javax.jms.MessageListener) TestUtils(org.springframework.integration.test.util.TestUtils) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ActiveMqTestUtils(org.springframework.integration.jms.config.ActiveMqTestUtils) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Mockito.doAnswer(org.mockito.Mockito.doAnswer) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TimeUnit(java.util.concurrent.TimeUnit) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) CountDownLatch(java.util.concurrent.CountDownLatch) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) List(java.util.List) Destination(javax.jms.Destination) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) AbstractMessageListenerContainer(org.springframework.jms.listener.AbstractMessageListenerContainer) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class SubscribableJmsChannelTests method queueReference.

@Test
public void queueReference() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    final List<Message<?>> receivedList1 = Collections.synchronizedList(new ArrayList<Message<?>>());
    MessageHandler handler1 = message -> {
        receivedList1.add(message);
        latch.countDown();
    };
    final List<Message<?>> receivedList2 = Collections.synchronizedList(new ArrayList<Message<?>>());
    MessageHandler handler2 = message -> {
        receivedList2.add(message);
        latch.countDown();
    };
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(true);
    factoryBean.setConnectionFactory(this.connectionFactory);
    factoryBean.setDestination(this.queue);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();
    channel.start();
    channel.subscribe(handler1);
    channel.subscribe(handler2);
    channel.send(new GenericMessage<String>("foo"));
    channel.send(new GenericMessage<String>("bar"));
    latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
    assertEquals(1, receivedList1.size());
    assertNotNull(receivedList1.get(0));
    assertEquals("foo", receivedList1.get(0).getPayload());
    assertEquals(1, receivedList2.size());
    assertNotNull(receivedList2.get(0));
    assertEquals("bar", receivedList2.get(0).getPayload());
    channel.stop();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageListener(javax.jms.MessageListener) TestUtils(org.springframework.integration.test.util.TestUtils) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ActiveMqTestUtils(org.springframework.integration.jms.config.ActiveMqTestUtils) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Mockito.doAnswer(org.mockito.Mockito.doAnswer) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TimeUnit(java.util.concurrent.TimeUnit) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) CountDownLatch(java.util.concurrent.CountDownLatch) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) List(java.util.List) Destination(javax.jms.Destination) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) AbstractMessageListenerContainer(org.springframework.jms.listener.AbstractMessageListenerContainer) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class PollableJmsChannelTests method queueName.

@Test
public void queueName() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    CachingConnectionFactory ccf = new CachingConnectionFactory(this.connectionFactory);
    ccf.setCacheConsumers(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestinationName("someDynamicQueue");
    factoryBean.setPubSubDomain(false);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    boolean sent2 = channel.send(new GenericMessage<String>("bar"));
    assertTrue(sent2);
    Message<?> result1 = channel.receive(10000);
    assertNotNull(result1);
    assertEquals("foo", result1.getPayload());
    Message<?> result2 = channel.receive(10000);
    assertNotNull(result2);
    assertEquals("bar", result2.getPayload());
}
Also used : JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 BeanFactory (org.springframework.beans.factory.BeanFactory)12 JmsChannelFactoryBean (org.springframework.integration.jms.config.JmsChannelFactoryBean)12 CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)10 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)7 Message (org.springframework.messaging.Message)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ArrayList (java.util.ArrayList)6 MessageListener (javax.jms.MessageListener)6 AbstractMessageListenerContainer (org.springframework.jms.listener.AbstractMessageListenerContainer)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 Collections (java.util.Collections)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Destination (javax.jms.Destination)4 TextMessage (javax.jms.TextMessage)4 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4