Search in sources :

Example 6 with MessageTimeoutException

use of org.springframework.integration.MessageTimeoutException 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 7 with MessageTimeoutException

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

the class JmsOutboundGateway method handleRequestMessage.

@Override
protected Object handleRequestMessage(final Message<?> requestMessage) {
    if (!this.initialized) {
        afterPropertiesSet();
    }
    try {
        Object reply;
        if (this.replyContainer == null) {
            reply = sendAndReceiveWithoutContainer(requestMessage);
        } else {
            if (this.idleReplyContainerTimeout > 0) {
                synchronized (this.lifeCycleMonitor) {
                    this.lastSend = System.currentTimeMillis();
                    if (!this.replyContainer.isRunning()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(this.getComponentName() + ": Starting reply container.");
                        }
                        this.replyContainer.start();
                        this.idleTask = getTaskScheduler().scheduleAtFixedRate(new IdleContainerStopper(), this.idleReplyContainerTimeout / 2);
                    }
                }
            }
            reply = this.sendAndReceiveWithContainer(requestMessage);
        }
        if (reply == null) {
            if (this.requiresReply) {
                throw new MessageTimeoutException(requestMessage, "failed to receive JMS response within timeout of: " + this.receiveTimeout + "ms");
            } else {
                return null;
            }
        }
        if (reply instanceof javax.jms.Message) {
            return buildReply((javax.jms.Message) reply);
        } else {
            return reply;
        }
    } catch (JMSException e) {
        throw new MessageHandlingException(requestMessage, e);
    }
}
Also used : Message(org.springframework.messaging.Message) JMSException(javax.jms.JMSException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Aggregations

MessageTimeoutException (org.springframework.integration.MessageTimeoutException)7 CountDownLatch (java.util.concurrent.CountDownLatch)3 Message (org.springframework.messaging.Message)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 RequestReplyExchanger (org.springframework.integration.gateway.RequestReplyExchanger)2 MessagingException (org.springframework.messaging.MessagingException)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1