Search in sources :

Example 21 with RequestReplyExchanger

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

the class RequestReplyScenariosWithNonCachedConsumersTests method messageCorrelationBasedOnRequestCorrelationIdOptimized.

@Test
public void messageCorrelationBasedOnRequestCorrelationIdOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("producer-no-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("optimized", RequestReplyExchanger.class);
        ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueA", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueA", Destination.class);
        new Thread(() -> {
            final Message requestMessage = jmsTemplate.receive(requestDestination);
            jmsTemplate.send(replyDestination, (MessageCreator) session -> {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSCorrelationID());
                return message;
            });
        }).start();
        org.springframework.messaging.Message<?> siReplyMessage = gateway.exchange(new GenericMessage<String>("foo"));
        assertEquals("bar", siReplyMessage.getPayload());
    } finally {
        context.close();
    }
}
Also used : Destination(javax.jms.Destination) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(javax.jms.Message) MessageCreator(org.springframework.jms.core.MessageCreator) ConnectionFactory(javax.jms.ConnectionFactory) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 22 with RequestReplyExchanger

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

the class RequestReplyScenariosWithNonCachedConsumersTests method messageCorrelationBasedOnRequestMessageIdOptimized.

@Test(expected = MessageTimeoutException.class)
public void messageCorrelationBasedOnRequestMessageIdOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("producer-no-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("optimizedMessageId", RequestReplyExchanger.class);
        ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueC", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueC", Destination.class);
        new Thread(() -> {
            final Message requestMessage = jmsTemplate.receive(requestDestination);
            jmsTemplate.send(replyDestination, (MessageCreator) session -> {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            });
        }).start();
        org.springframework.messaging.Message<?> siReplyMessage = gateway.exchange(new GenericMessage<String>("foo"));
        assertEquals("bar", siReplyMessage.getPayload());
    } finally {
        context.close();
    }
}
Also used : Destination(javax.jms.Destination) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(javax.jms.Message) MessageCreator(org.springframework.jms.core.MessageCreator) ConnectionFactory(javax.jms.ConnectionFactory) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 23 with RequestReplyExchanger

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

the class PipelineNamedReplyQueuesJmsTests method test.

public int test(String contextConfig, final int offset) throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(contextConfig, this.getClass());
    final AtomicInteger successCounter = new AtomicInteger();
    final AtomicInteger timeoutCounter = new AtomicInteger();
    final AtomicInteger failureCounter = new AtomicInteger();
    try {
        final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
        final CountDownLatch latch = new CountDownLatch(requests);
        for (int i = 1000000; i < 1000000 + requests * 100000; i += 100000) {
            final int y = i;
            executor.execute(() -> {
                try {
                    assertEquals(y + offset, gateway.exchange(new GenericMessage<Integer>(y)).getPayload());
                    successCounter.incrementAndGet();
                } catch (MessageTimeoutException e) {
                    timeoutCounter.incrementAndGet();
                } catch (Throwable t) {
                    logger.error("gateway invocation failed", t);
                    failureCounter.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            });
        }
        assertTrue(latch.await(120, TimeUnit.SECONDS));
        // technically all we care that its > 0,
        // but reality of this test it has to be something more then 0
        assertTrue(successCounter.get() > 1);
        assertEquals(0, failureCounter.get());
        assertEquals(requests, successCounter.get() + timeoutCounter.get());
        return timeoutCounter.get();
    } finally {
        logger.info("Test config: " + contextConfig);
        logger.info("Success: " + successCounter.get());
        logger.info("Timeout: " + timeoutCounter.get());
        logger.info("Failure: " + failureCounter.get());
        if (timeoutCounter.get() > 0 && context.containsBean("capture")) {
            logger.info(context.getBean(Capture.class).messages);
        }
        context.close();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with RequestReplyExchanger

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

the class RequestReplyScenariosWithCorrelationKeyProvidedTests method messageCorrelationBasedOnProvidedJMSCorrelationID.

@Test
public void messageCorrelationBasedOnProvidedJMSCorrelationID() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("explicit-correlation-key.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean("existingCorrelationKeyGatewayB", RequestReplyExchanger.class);
    String correlationId = UUID.randomUUID().toString().replaceAll("'", "''");
    Message<?> result = gateway.exchange(MessageBuilder.withPayload("foo").setHeader(JmsHeaders.CORRELATION_ID, correlationId).build());
    assertEquals(correlationId, result.getHeaders().get("receivedCorrelationId"));
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 25 with RequestReplyExchanger

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

the class RequestReplyScenariosWithCorrelationKeyProvidedTests method messageCorrelationBasedCustomCorrelationKeyAsJMSCorrelationID.

@Test
public void messageCorrelationBasedCustomCorrelationKeyAsJMSCorrelationID() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("explicit-correlation-key.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean("explicitCorrelationKeyGatewayB", RequestReplyExchanger.class);
    gateway.exchange(MessageBuilder.withPayload("foo").build());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) 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