Example usage for org.springframework.integration.gateway RequestReplyExchanger exchange

List of usage examples for org.springframework.integration.gateway RequestReplyExchanger exchange

Introduction

In this page you can find the example usage for org.springframework.integration.gateway RequestReplyExchanger exchange.

Prototype

Message<?> exchange(Message<?> request) throws MessagingException;

Source Link

Usage

From source file:org.acme.echo.module.config.ModuleConfigTest.java

@Test
public void testModule() {
    ApplicationContext ac = new ClassPathXmlApplicationContext("integration-config.xml", this.getClass());
    RequestReplyExchanger gateway = ac.getBean("echoGateway", RequestReplyExchanger.class);
    Message<?> replyMessage = gateway.exchange(new GenericMessage<String>("foo"));
    assertEquals("FOO", replyMessage.getPayload());
}

From source file:org.springframework.integration.jms.request_reply.PipelineJmsTests.java

public void test(String contextConfig) throws Exception {
    ActiveMqTestUtils.prepare();/*from ww w .j ava  2  s.c om*/
    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(new Runnable() {
                public void run() {
                    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() > 10);
        assertEquals(0, failureCounter.get());
        assertEquals(requests, successCounter.get() + timeoutCounter.get());
        context.destroy();
    }
}

From source file:org.springframework.integration.jms.request_reply.PipelineNamedReplyQueuesJmsTests.java

public int test(String contextConfig, final int offset) throws Exception {
    ActiveMqTestUtils.prepare();/*from w w  w . j a v  a 2  s  . c  o  m*/
    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 = 0; i < requests; i++) {
            final int y = i;
            executor.execute(new Runnable() {
                public void run() {
                    try {
                        assertEquals(y + offset, gateway.exchange(new GenericMessage<Integer>(y)).getPayload());
                        successCounter.incrementAndGet();
                    } catch (MessageTimeoutException e) {
                        timeoutCounter.incrementAndGet();
                    } catch (Throwable t) {
                        t.printStackTrace();
                        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() > 10);
        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());
        context.destroy();
    }
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@SuppressWarnings("resource")
@Test// www  . java 2  s  . c  o m
public void messageCorrelationBasedOnRequestMessageId() throws Exception {
    ActiveMqTestUtils.prepare();

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
    final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);

    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);

    new Thread(() -> {
        final Message requestMessage = jmsTemplate.receive(requestDestination);
        Destination replyTo = null;
        try {
            replyTo = requestMessage.getJMSReplyTo();
        } catch (Exception e) {
            fail();
        }
        jmsTemplate.send(replyTo, (MessageCreator) session -> {
            try {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            } catch (Exception e) {
                // ignore
            }
            return null;
        });
    }).start();
    gateway.exchange(new GenericMessage<String>("foo"));
    context.close();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@Test
public void messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReply() throws Exception {
    ActiveMqTestUtils.prepare();// w  ww.ja  v  a2s .c  om
    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();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

/**
 * Validates that JOG will recreate a temporary queue
 * once a failure detected and that the messages will still be properly correlated
 *//*from w  w w .  j  av  a 2 s .  c  om*/
@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();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@Test
public void testConcurrently() throws Exception {
    ActiveMqTestUtils.prepare();/* w  w  w. j a  v  a  2  s. co m*/
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "mult-producer-and-consumers-temp-reply.xml", this.getClass());
    final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    Executor executor = Executors.newFixedThreadPool(10);
    final int testNumbers = 100;
    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();
                }
            }
            //               if (latch.getCount()%100 == 0){
            //                  long count = testNumbers-latch.getCount();
            //                  if (count > 0){
            //                     print(failures, timeouts, missmatches, testNumbers-latch.getCount());
            //                  }
            //               }
            latch.countDown();
        });
    }
    latch.await();
    print(failures, timeouts, missmatches, testNumbers);
    Thread.sleep(5000);
    assertEquals(0, missmatches.get());
    assertEquals(0, failures.get());
    assertEquals(0, timeouts.get());
    context.close();
}