Search in sources :

Example 1 with ReplyContainerProperties

use of org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties in project spring-integration by spring-projects.

the class JmsOutboundGatewayTests method testReplyContainerRecovery.

@SuppressWarnings("serial")
@Test
public void testReplyContainerRecovery() throws Exception {
    JmsOutboundGateway gateway = new JmsOutboundGateway();
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    gateway.setConnectionFactory(connectionFactory);
    gateway.setRequestDestinationName("foo");
    gateway.setUseReplyContainer(true);
    ReplyContainerProperties replyContainerProperties = new ReplyContainerProperties();
    final List<Throwable> errors = new ArrayList<Throwable>();
    ExecutorService exec = Executors.newFixedThreadPool(10);
    ErrorHandlingTaskExecutor errorHandlingTaskExecutor = new ErrorHandlingTaskExecutor(exec, t -> {
        errors.add(t);
        throw new RuntimeException(t);
    });
    replyContainerProperties.setTaskExecutor(errorHandlingTaskExecutor);
    replyContainerProperties.setRecoveryInterval(100L);
    gateway.setReplyContainerProperties(replyContainerProperties);
    final Connection connection = mock(Connection.class);
    final AtomicInteger connectionAttempts = new AtomicInteger();
    doAnswer(invocation -> {
        int theCount = connectionAttempts.incrementAndGet();
        if (theCount > 1 && theCount < 4) {
            throw new JmsException("bar") {
            };
        }
        return connection;
    }).when(connectionFactory).createConnection();
    Session session = mock(Session.class);
    when(connection.createSession(false, 1)).thenReturn(session);
    MessageConsumer consumer = mock(MessageConsumer.class);
    when(session.createConsumer(any(Destination.class), isNull())).thenReturn(consumer);
    when(session.createTemporaryQueue()).thenReturn(mock(TemporaryQueue.class));
    final Message message = mock(Message.class);
    final AtomicInteger count = new AtomicInteger();
    doAnswer(invocation -> {
        int theCount = count.incrementAndGet();
        if (theCount > 1 && theCount < 4) {
            throw new JmsException("foo") {
            };
        }
        if (theCount > 4) {
            Thread.sleep(100);
            return null;
        }
        return message;
    }).when(consumer).receive(anyLong());
    when(message.getJMSCorrelationID()).thenReturn("foo");
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    beanFactory.registerSingleton("taskScheduler", taskScheduler);
    gateway.setBeanFactory(beanFactory);
    gateway.afterPropertiesSet();
    gateway.start();
    try {
        int n = 0;
        while (n++ < 100 && count.get() < 5) {
            Thread.sleep(100);
        }
        assertTrue(count.get() > 4);
        assertEquals(0, errors.size());
    } finally {
        gateway.stop();
        exec.shutdownNow();
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) JmsException(org.springframework.jms.JmsException) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReplyContainerProperties(org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties) ExecutorService(java.util.concurrent.ExecutorService) TemporaryQueue(javax.jms.TemporaryQueue) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) Session(javax.jms.Session) Test(org.junit.Test)

Example 2 with ReplyContainerProperties

use of org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties in project spring-integration by spring-projects.

the class JmsOutboundGatewayTests method testContainerBeanNameWhenNoGatewayBeanName.

@Test
public void testContainerBeanNameWhenNoGatewayBeanName() {
    JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setConnectionFactory(mock(ConnectionFactory.class));
    gateway.setRequestDestinationName("foo");
    gateway.setUseReplyContainer(true);
    gateway.setReplyContainerProperties(new ReplyContainerProperties());
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    assertEquals("JMS_OutboundGateway@" + ObjectUtils.getIdentityHexString(gateway) + ".replyListener", TestUtils.getPropertyValue(gateway, "replyContainer.beanName"));
}
Also used : CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ReplyContainerProperties(org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Aggregations

ConnectionFactory (javax.jms.ConnectionFactory)2 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)2 Test (org.junit.Test)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 ReplyContainerProperties (org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties)2 CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)2 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Connection (javax.jms.Connection)1 Destination (javax.jms.Destination)1 Message (javax.jms.Message)1 MessageConsumer (javax.jms.MessageConsumer)1 Session (javax.jms.Session)1 TemporaryQueue (javax.jms.TemporaryQueue)1 TextMessage (javax.jms.TextMessage)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ErrorHandlingTaskExecutor (org.springframework.integration.util.ErrorHandlingTaskExecutor)1 JmsException (org.springframework.jms.JmsException)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1