Search in sources :

Example 1 with JmsChannelFactoryBean

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

the class SubscribableJmsChannelTests method queueName.

@Test
public void queueName() 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.setDestinationName("dynamicQueue");
    factoryBean.setPubSubDomain(false);
    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"));
    assertTrue("Countdown latch should have counted down to 0 but was " + latch.getCount(), 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 2 with JmsChannelFactoryBean

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

the class SubscribableJmsChannelTests method topicReference.

@Test
public void topicReference() 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.setDestination(this.topic);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();
    channel.subscribe(handler1);
    channel.subscribe(handler2);
    channel.start();
    if (!waitUntilRegisteredWithDestination(channel, 10000)) {
        fail("Listener failed to subscribe to topic");
    }
    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 3 with JmsChannelFactoryBean

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

the class PollableJmsChannelTests method queueReference.

@Test
public void queueReference() throws Exception {
    this.queue = new ActiveMQQueue("pollableJmsChannelTestQueue");
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    CachingConnectionFactory ccf = new CachingConnectionFactory(this.connectionFactory);
    ccf.setCacheConsumers(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestination(this.queue);
    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) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Test(org.junit.Test)

Example 4 with JmsChannelFactoryBean

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

the class PollableJmsChannelTests method queueNameWithFalsePreReceiveInterceptors.

@Test
public void queueNameWithFalsePreReceiveInterceptors() 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);
    List<ChannelInterceptor> interceptorList = new ArrayList<ChannelInterceptor>();
    ChannelInterceptor interceptor = spy(new SampleInterceptor(false));
    interceptorList.add(interceptor);
    factoryBean.setInterceptors(interceptorList);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    Message<?> result1 = channel.receive(10000);
    assertNull(result1);
    verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
    verify(interceptor, times(0)).postReceive(Mockito.any(Message.class), Mockito.any(MessageChannel.class));
}
Also used : Message(org.springframework.messaging.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) MessageChannel(org.springframework.messaging.MessageChannel) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 5 with JmsChannelFactoryBean

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

the class PollableJmsChannelTests method selector.

@Test
public void selector() throws Exception {
    this.queue = new ActiveMQQueue("pollableJmsChannelSelectorTestQueue");
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    CachingConnectionFactory ccf = new CachingConnectionFactory(this.connectionFactory);
    ccf.setCacheConsumers(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestination(this.queue);
    factoryBean.setMessageSelector("baz='qux'");
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    Message<?> result1 = channel.receive(100);
    assertNull(result1);
    JmsTemplate jmsTemplate = new JmsTemplate(this.connectionFactory);
    jmsTemplate.setDefaultDestinationName("pollableJmsChannelSelectorTestQueue");
    jmsTemplate.send(session -> {
        TextMessage message = session.createTextMessage("bar");
        message.setStringProperty("baz", "qux");
        return message;
    });
    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) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) 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