Search in sources :

Example 16 with RequestReplyExchanger

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

the class RequestReplyScenariosWithTempReplyQueuesTests method testConcurrently.

@Test
public void testConcurrently() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("mult-producer-and-consumers-temp-reply.xml", this.getClass());
    final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    final int testNumbers = 30;
    final CountDownLatch latch = new CountDownLatch(testNumbers);
    final AtomicInteger failures = new AtomicInteger();
    final AtomicInteger timeouts = new AtomicInteger();
    final AtomicInteger missmatches = new AtomicInteger();
    for (int i = 0; i < testNumbers; i++) {
        final int y = i;
        executor.execute(() -> {
            try {
                String reply = (String) gateway.exchange(new GenericMessage<String>(String.valueOf(y))).getPayload();
                if (!String.valueOf(y).equals(reply)) {
                    missmatches.incrementAndGet();
                }
            } catch (Exception e) {
                if (e instanceof MessageDeliveryException) {
                    timeouts.incrementAndGet();
                } else {
                    failures.incrementAndGet();
                }
            }
            latch.countDown();
        });
    }
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    print(failures, timeouts, missmatches, testNumbers);
    assertEquals(0, missmatches.get());
    assertEquals(0, failures.get());
    assertEquals(0, timeouts.get());
    context.close();
    executor.shutdownNow();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) CountDownLatch(java.util.concurrent.CountDownLatch) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 17 with RequestReplyExchanger

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

the class ServiceActivatorMethodResolutionTests method testRequestReplyExchangerSeveralMethods.

@Test
public /*
	 * A handler and message handler fallback (RRE); don't force RRE
	 */
void testRequestReplyExchangerSeveralMethods() {
    RequestReplyExchanger testBean = new RequestReplyExchanger() {

        @Override
        public Message<?> exchange(Message<?> request) {
            return request;
        }

        @SuppressWarnings("unused")
        public String foo(String request) {
            return request.toUpperCase();
        }
    };
    ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean);
    PollableChannel outputChannel = new QueueChannel();
    serviceActivator.setOutputChannel(outputChannel);
    Message<?> test = new GenericMessage<Object>(new Date());
    serviceActivator.handleMessage(test);
    assertEquals(test, outputChannel.receive(10));
    test = new GenericMessage<Object>("foo");
    serviceActivator.handleMessage(test);
    assertEquals("FOO", outputChannel.receive(10).getPayload());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) PollableChannel(org.springframework.messaging.PollableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Date(java.util.Date) Test(org.junit.Test)

Example 18 with RequestReplyExchanger

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

the class PipelineJmsTests method test.

public void test(String contextConfig) throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(contextConfig, this.getClass());
    final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    final CountDownLatch latch = new CountDownLatch(requests);
    final AtomicInteger successCounter = new AtomicInteger();
    final AtomicInteger timeoutCounter = new AtomicInteger();
    final AtomicInteger failureCounter = new AtomicInteger();
    try {
        for (int i = 0; i < requests; i++) {
            final int y = i;
            executor.execute(() -> {
                try {
                    assertEquals(y, gateway.exchange(new GenericMessage<Integer>(y)).getPayload());
                    successCounter.incrementAndGet();
                } catch (MessageTimeoutException e) {
                    timeoutCounter.incrementAndGet();
                } catch (Throwable t) {
                    failureCounter.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await();
    } finally {
        logger.info("Test config: " + contextConfig);
        logger.info("Success: " + successCounter.get());
        logger.info("Timeout: " + timeoutCounter.get());
        logger.info("Failure: " + failureCounter.get());
        // 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());
        context.close();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with RequestReplyExchanger

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

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestCorrelationIdOptimized.

@Test
public void messageCorrelationBasedOnRequestCorrelationIdOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("correlationPropagatingConsumerWithOptimization", RequestReplyExchanger.class);
        CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueOptimizedC", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueOptimizedC", 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) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageCreator(org.springframework.jms.core.MessageCreator) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 20 with RequestReplyExchanger

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

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestCorrelationIdNonOptimized.

@Test(expected = MessageTimeoutException.class)
public void messageCorrelationBasedOnRequestCorrelationIdNonOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("correlationPropagatingConsumerWithoutOptimization", RequestReplyExchanger.class);
        CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueNonOptimizedD", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueNonOptimizedD", 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();
        gateway.exchange(new GenericMessage<String>("foo"));
    } finally {
        context.close();
    }
}
Also used : Destination(javax.jms.Destination) 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) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) MessageCreator(org.springframework.jms.core.MessageCreator) TextMessage(javax.jms.TextMessage) 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