Search in sources :

Example 26 with RequestReplyExchanger

use of org.springframework.integration.gateway.RequestReplyExchanger in project spring-integration by spring-projects.

the class RequestReplyScenariosWithTempReplyQueuesTests method messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReply.

@Test
public void messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReply() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);
    DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
    dmlc.setConnectionFactory(connectionFactory);
    dmlc.setDestination(requestDestination);
    dmlc.setMessageListener((SessionAwareMessageListener<Message>) (message, session) -> {
        Destination replyTo = null;
        try {
            replyTo = message.getJMSReplyTo();
        } catch (Exception e1) {
            fail();
        }
        String requestPayload = (String) extractPayload(message);
        if (requestPayload.equals("foo")) {
            try {
                Thread.sleep(6000);
            } catch (Exception e2) {
            /*ignore*/
            }
        }
        try {
            TextMessage replyMessage = session.createTextMessage();
            replyMessage.setText(requestPayload);
            replyMessage.setJMSCorrelationID(message.getJMSMessageID());
            MessageProducer producer = session.createProducer(replyTo);
            producer.send(replyMessage);
        } catch (Exception e3) {
        // ignore. the test will fail
        }
    });
    dmlc.afterPropertiesSet();
    dmlc.start();
    try {
        gateway.exchange(new GenericMessage<String>("foo"));
    } catch (Exception e) {
    // ignore
    }
    Thread.sleep(1000);
    try {
        assertEquals("bar", gateway.exchange(new GenericMessage<String>("bar")).getPayload());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    context.close();
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Random(java.util.Random) TestUtils(org.springframework.integration.test.util.TestUtils) ArrayList(java.util.ArrayList) ActiveMqTestUtils(org.springframework.integration.jms.config.ActiveMqTestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JmsTemplate(org.springframework.jms.core.JmsTemplate) Assert.fail(org.junit.Assert.fail) MessageProducer(javax.jms.MessageProducer) Message(javax.jms.Message) ActiveMQMultiContextTests(org.springframework.integration.jms.ActiveMQMultiContextTests) ExecutorService(java.util.concurrent.ExecutorService) BrokerService(org.apache.activemq.broker.BrokerService) TextMessage(javax.jms.TextMessage) Assert.assertTrue(org.junit.Assert.assertTrue) SessionAwareMessageListener(org.springframework.jms.listener.SessionAwareMessageListener) Test(org.junit.Test) Executors(java.util.concurrent.Executors) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) List(java.util.List) MessageCreator(org.springframework.jms.core.MessageCreator) Rule(org.junit.Rule) Destination(javax.jms.Destination) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageProducer(javax.jms.MessageProducer) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 27 with RequestReplyExchanger

use of org.springframework.integration.gateway.RequestReplyExchanger in project spring-integration by spring-projects.

the class RequestReplyScenariosWithTempReplyQueuesTests method brokenBrokerTest.

/**
 * Validates that JOG will recreate a temporary queue
 * once a failure detected and that the messages will still be properly correlated
 */
@Test
public void brokenBrokerTest() throws Exception {
    BrokerService broker = new BrokerService();
    broker.setPersistent(false);
    broker.setUseJmx(false);
    broker.setTransportConnectorURIs(new String[] { "tcp://localhost:61623" });
    broker.setDeleteAllMessagesOnStartup(true);
    broker.start();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("broken-broker.xml", this.getClass());
    final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    int replyCounter = 0;
    int timeoutCounter = 0;
    for (int i = 0; i < 50; i++) {
        try {
            assertEquals(i + "", gateway.exchange(new GenericMessage<String>(String.valueOf(i))).getPayload());
            replyCounter++;
        } catch (Exception e) {
            timeoutCounter++;
        }
        if (i == 0 || i == 20 || i == 40) {
            Object replyDestination = TestUtils.getPropertyValue(context.getBean("jog"), "handler.replyDestination");
            if (replyDestination != null) {
                broker.removeDestination((ActiveMQDestination) replyDestination);
            }
        }
    }
    assertEquals(50, replyCounter + timeoutCounter);
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) BrokerService(org.apache.activemq.broker.BrokerService) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

RequestReplyExchanger (org.springframework.integration.gateway.RequestReplyExchanger)27 Test (org.junit.Test)24 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)21 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)18 GenericMessage (org.springframework.messaging.support.GenericMessage)17 Destination (javax.jms.Destination)11 TextMessage (javax.jms.TextMessage)11 Message (javax.jms.Message)10 JmsTemplate (org.springframework.jms.core.JmsTemplate)10 MessageCreator (org.springframework.jms.core.MessageCreator)10 CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ConnectionFactory (javax.jms.ConnectionFactory)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)5 Message (org.springframework.messaging.Message)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 Date (java.util.Date)4 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4