Search in sources :

Example 26 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class JmsOutboundGatewayTests method testConnectionBreakOnReplyMessageIdCorrelation.

@Test
public void testConnectionBreakOnReplyMessageIdCorrelation() throws Exception {
    CachingConnectionFactory connectionFactory1 = new CachingConnectionFactory(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"));
    connectionFactory1.setCacheConsumers(false);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setConnectionFactory(connectionFactory1);
    String requestQ = "requests1";
    gateway.setRequestDestinationName(requestQ);
    String replyQ = "replies1";
    gateway.setReplyDestinationName(replyQ);
    QueueChannel queueChannel = new QueueChannel();
    gateway.setOutputChannel(queueChannel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setReceiveTimeout(60000);
    gateway.afterPropertiesSet();
    gateway.start();
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> gateway.handleMessage(new GenericMessage<String>("foo")));
    CachingConnectionFactory connectionFactory2 = new CachingConnectionFactory(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"));
    connectionFactory2.setCacheConsumers(false);
    JmsTemplate template = new JmsTemplate(connectionFactory2);
    template.setReceiveTimeout(10000);
    template.afterPropertiesSet();
    final Message request = template.receive(requestQ);
    assertNotNull(request);
    connectionFactory1.resetConnection();
    MessageCreator reply = session -> {
        TextMessage reply1 = session.createTextMessage("bar");
        reply1.setJMSCorrelationID(request.getJMSMessageID());
        return reply1;
    };
    template.send(replyQ, reply);
    org.springframework.messaging.Message<?> received = queueChannel.receive(20000);
    assertNotNull(received);
    assertEquals("bar", received.getPayload());
    gateway.stop();
    connectionFactory1.destroy();
    connectionFactory2.destroy();
    exec.shutdownNow();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TestUtils(org.springframework.integration.test.util.TestUtils) ArrayList(java.util.ArrayList) Session(javax.jms.Session) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JmsTemplate(org.springframework.jms.core.JmsTemplate) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Message(javax.jms.Message) ExecutorService(java.util.concurrent.ExecutorService) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) Connection(javax.jms.Connection) JmsException(org.springframework.jms.JmsException) Assert.assertNotNull(org.junit.Assert.assertNotNull) TextMessage(javax.jms.TextMessage) ObjectUtils(org.springframework.util.ObjectUtils) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) List(java.util.List) MessageCreator(org.springframework.jms.core.MessageCreator) MessageConsumer(javax.jms.MessageConsumer) Rule(org.junit.Rule) Log4j2LevelAdjuster(org.springframework.integration.test.rule.Log4j2LevelAdjuster) Destination(javax.jms.Destination) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ReplyContainerProperties(org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties) GenericMessage(org.springframework.messaging.support.GenericMessage) TemporaryQueue(javax.jms.TemporaryQueue) Assert.assertEquals(org.junit.Assert.assertEquals) ConnectionFactory(javax.jms.ConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageCreator(org.springframework.jms.core.MessageCreator) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 27 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class OutboundGatewayConnectionTests method testContainerWithDestBrokenConnection.

// need a more reliable stop/start for AMQ
@Test
// need a more reliable stop/start for AMQ
@Ignore
public void testContainerWithDestBrokenConnection() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    BrokerService broker = new BrokerService();
    broker.addConnector("tcp://localhost:61616?broker.persistent=false");
    broker.start();
    ActiveMQConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    ConnectionFactory connectionFactory = new CachingConnectionFactory(amqConnectionFactory);
    gateway.setConnectionFactory(connectionFactory);
    gateway.setRequestDestination(requestQueue1);
    gateway.setReplyDestination(replyQueue1);
    gateway.setCorrelationKey("JMSCorrelationID");
    gateway.setUseReplyContainer(true);
    gateway.afterPropertiesSet();
    gateway.start();
    final AtomicReference<Object> reply = new AtomicReference<Object>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        latch1.countDown();
        try {
            reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
        } finally {
            latch2.countDown();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(amqConnectionFactory);
    template.setReceiveTimeout(5000);
    javax.jms.Message request = template.receive(requestQueue1);
    assertNotNull(request);
    final javax.jms.Message jmsReply = request;
    template.send(request.getJMSReplyTo(), (MessageCreator) session -> jmsReply);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    broker.stop();
    broker = new BrokerService();
    broker.addConnector("tcp://localhost:61616?broker.persistent=false");
    broker.start();
    final CountDownLatch latch3 = new CountDownLatch(1);
    final CountDownLatch latch4 = new CountDownLatch(1);
    exec.execute(() -> {
        latch3.countDown();
        try {
            reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
        } finally {
            latch4.countDown();
        }
    });
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    template = new JmsTemplate();
    template.setConnectionFactory(amqConnectionFactory);
    template.setReceiveTimeout(5000);
    request = template.receive(requestQueue1);
    assertNotNull(request);
    final javax.jms.Message jmsReply2 = request;
    template.send(request.getJMSReplyTo(), (MessageCreator) session -> jmsReply2);
    assertTrue(latch4.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    gateway.stop();
    broker.stop();
    scheduler.destroy();
    exec.shutdownNow();
}
Also used : BrokerService(org.apache.activemq.broker.BrokerService) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) Executors(java.util.concurrent.Executors) IntegrationContextUtils(org.springframework.integration.context.IntegrationContextUtils) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MessageCreator(org.springframework.jms.core.MessageCreator) Ignore(org.junit.Ignore) Destination(javax.jms.Destination) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) GenericMessage(org.springframework.messaging.support.GenericMessage) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ExecutorService(java.util.concurrent.ExecutorService) ConnectionFactory(javax.jms.ConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) BrokerService(org.apache.activemq.broker.BrokerService) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory 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)

Example 29 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class PollableJmsChannelTests method qos.

@Test
public void qos() throws Exception {
    this.queue = new ActiveMQQueue("pollableJmsChannelTestQueue");
    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory);
    ccf.setCacheConsumers(false);
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestination(this.queue);
    factoryBean.setExplicitQosEnabled(true);
    factoryBean.setPriority(5);
    int ttl = 10000;
    factoryBean.setTimeToLive(ttl);
    factoryBean.setDeliveryPersistent(false);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    final JmsTemplate receiver = new JmsTemplate(this.connectionFactory);
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    final AtomicReference<javax.jms.Message> message = new AtomicReference<javax.jms.Message>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        message.set(receiver.receive(queue));
        latch1.countDown();
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertNotNull(message.get());
    assertEquals(5, message.get().getJMSPriority());
    assertTrue(message.get().getJMSExpiration() <= System.currentTimeMillis() + ttl);
    assertTrue(message.get().toString().contains("persistent = false"));
    message.set(null);
    final CountDownLatch latch2 = new CountDownLatch(1);
    boolean sent2 = channel.send(MessageBuilder.withPayload("foo").setPriority(6).build());
    assertTrue(sent2);
    exec.execute(() -> {
        message.set(receiver.receive(queue));
        latch2.countDown();
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(message.get());
    assertEquals(6, message.get().getJMSPriority());
    assertTrue(message.get().getJMSExpiration() <= System.currentTimeMillis() + ttl);
    assertTrue(message.get().toString().contains("persistent = false"));
    exec.shutdownNow();
}
Also used : Message(org.springframework.messaging.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) JmsTemplate(org.springframework.jms.core.JmsTemplate) Test(org.junit.Test)

Example 30 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class PollableJmsChannelTests method queueNameWithTruePreReceiveInterceptors.

@Test
public void queueNameWithTruePreReceiveInterceptors() 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(true));
    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);
    assertNotNull(result1);
    verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
    verify(interceptor, times(1)).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)

Aggregations

CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)32 Test (org.junit.Test)21 JmsTemplate (org.springframework.jms.core.JmsTemplate)20 TextMessage (javax.jms.TextMessage)15 Message (javax.jms.Message)11 GenericMessage (org.springframework.messaging.support.GenericMessage)11 MessageCreator (org.springframework.jms.core.MessageCreator)10 BeanFactory (org.springframework.beans.factory.BeanFactory)9 Destination (javax.jms.Destination)8 ComponentLog (org.apache.nifi.logging.ComponentLog)7 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)6 JmsChannelFactoryBean (org.springframework.integration.jms.config.JmsChannelFactoryBean)6 ConnectionFactory (javax.jms.ConnectionFactory)5 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)5 ArrayList (java.util.ArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Session (javax.jms.Session)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executors (java.util.concurrent.Executors)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3