Search in sources :

Example 1 with MessageTimeoutException

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

the class MessagingGatewaySupport method doSendAndReceive.

@SuppressWarnings("unchecked")
private Object doSendAndReceive(Object object, boolean shouldConvert) {
    this.initializeIfNecessary();
    Assert.notNull(object, "request must not be null");
    MessageChannel requestChannel = getRequestChannel();
    if (requestChannel == null) {
        throw new MessagingException("No request channel available. Cannot send request message.");
    }
    registerReplyMessageCorrelatorIfNecessary();
    Object reply = null;
    Throwable error = null;
    Message<?> requestMessage = null;
    try {
        if (this.countsEnabled) {
            this.messageCount.incrementAndGet();
        }
        if (shouldConvert) {
            reply = this.messagingTemplate.convertSendAndReceive(requestChannel, object, Object.class, this.historyWritingPostProcessor);
            if (reply instanceof Throwable) {
                error = (Throwable) reply;
            }
        } else {
            requestMessage = (object instanceof Message<?>) ? (Message<?>) object : this.requestMapper.toMessage(object);
            requestMessage = this.historyWritingPostProcessor.postProcessMessage(requestMessage);
            reply = this.messagingTemplate.sendAndReceive(requestChannel, requestMessage);
            if (reply instanceof ErrorMessage) {
                error = ((ErrorMessage) reply).getPayload();
            }
        }
        if (reply == null && this.errorOnTimeout) {
            if (object instanceof Message) {
                error = new MessageTimeoutException((Message<?>) object, "No reply received within timeout");
            } else {
                error = new MessageTimeoutException("No reply received within timeout");
            }
        }
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("failure occurred in gateway sendAndReceive: " + e.getMessage());
        }
        error = e;
    }
    if (error != null) {
        MessageChannel errorChannel = getErrorChannel();
        if (errorChannel != null) {
            ErrorMessage errorMessage = buildErrorMessage(requestMessage, error);
            Message<?> errorFlowReply = null;
            try {
                errorFlowReply = this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
            } catch (Exception errorFlowFailure) {
                throw new MessagingException(errorMessage, "failure occurred in error-handling flow", errorFlowFailure);
            }
            if (shouldConvert) {
                Object result = (errorFlowReply != null) ? errorFlowReply.getPayload() : null;
                if (result instanceof Throwable) {
                    this.rethrow((Throwable) result, "error flow returned Exception");
                }
                return result;
            }
            if (errorFlowReply != null && errorFlowReply.getPayload() instanceof Throwable) {
                this.rethrow((Throwable) errorFlowReply.getPayload(), "error flow returned an Error Message");
            }
            if (errorFlowReply == null && this.errorOnTimeout) {
                if (object instanceof Message) {
                    throw new MessageTimeoutException((Message<?>) object, "No reply received from error channel within timeout");
                } else {
                    throw new MessageTimeoutException("No reply received from error channel within timeout");
                }
            }
            return errorFlowReply;
        } else {
            // no errorChannel so we'll propagate
            this.rethrow(error, "gateway received checked Exception");
        }
    }
    return reply;
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageMappingException(org.springframework.integration.mapping.MessageMappingException)

Example 2 with MessageTimeoutException

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

the class HttpRequestHandlingEndpointSupport method actualDoHandleRequest.

@SuppressWarnings({ "rawtypes", "unchecked" })
private Message<?> actualDoHandleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
    this.activeCount.incrementAndGet();
    try {
        ServletServerHttpRequest request = this.prepareRequest(servletRequest);
        Object requestBody = null;
        if (isReadable(request)) {
            requestBody = extractRequestBody(request);
        }
        HttpEntity httpEntity = new HttpEntity(requestBody, request.getHeaders());
        StandardEvaluationContext evaluationContext = this.createEvaluationContext();
        evaluationContext.setRootObject(httpEntity);
        evaluationContext.setVariable("requestAttributes", RequestContextHolder.currentRequestAttributes());
        MultiValueMap<String, String> requestParams = this.convertParameterMap(servletRequest.getParameterMap());
        evaluationContext.setVariable("requestParams", requestParams);
        evaluationContext.setVariable("requestHeaders", new ServletServerHttpRequest(servletRequest).getHeaders());
        Cookie[] requestCookies = servletRequest.getCookies();
        if (!ObjectUtils.isEmpty(requestCookies)) {
            Map<String, Cookie> cookies = new HashMap<String, Cookie>(requestCookies.length);
            for (Cookie requestCookie : requestCookies) {
                cookies.put(requestCookie.getName(), requestCookie);
            }
            evaluationContext.setVariable("cookies", cookies);
        }
        Map<String, String> pathVariables = (Map<String, String>) servletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        if (!CollectionUtils.isEmpty(pathVariables)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Mapped path variables: " + pathVariables);
            }
            evaluationContext.setVariable("pathVariables", pathVariables);
        }
        Map<String, MultiValueMap<String, String>> matrixVariables = (Map<String, MultiValueMap<String, String>>) servletRequest.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
        if (!CollectionUtils.isEmpty(matrixVariables)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Mapped matrix variables: " + matrixVariables);
            }
            evaluationContext.setVariable("matrixVariables", matrixVariables);
        }
        Map<String, Object> headers = getHeaderMapper().toHeaders(request.getHeaders());
        Object payload = null;
        if (getPayloadExpression() != null) {
            // create payload based on SpEL
            payload = getPayloadExpression().getValue(evaluationContext);
        }
        if (!CollectionUtils.isEmpty(getHeaderExpressions())) {
            for (Entry<String, Expression> entry : getHeaderExpressions().entrySet()) {
                String headerName = entry.getKey();
                Expression headerExpression = entry.getValue();
                Object headerValue = headerExpression.getValue(evaluationContext);
                if (headerValue != null) {
                    headers.put(headerName, headerValue);
                }
            }
        }
        if (payload == null) {
            if (requestBody != null) {
                payload = requestBody;
            } else {
                payload = requestParams;
            }
        }
        AbstractIntegrationMessageBuilder<?> messageBuilder = null;
        if (payload instanceof Message<?>) {
            messageBuilder = this.getMessageBuilderFactory().fromMessage((Message<?>) payload).copyHeadersIfAbsent(headers);
        } else {
            messageBuilder = this.getMessageBuilderFactory().withPayload(payload).copyHeaders(headers);
        }
        Message<?> message = messageBuilder.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL, request.getURI().toString()).setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD, request.getMethod().toString()).setHeader(org.springframework.integration.http.HttpHeaders.USER_PRINCIPAL, servletRequest.getUserPrincipal()).build();
        Message<?> reply = null;
        if (this.expectReply) {
            try {
                reply = this.sendAndReceiveMessage(message);
            } catch (MessageTimeoutException e) {
                if (getStatusCodeExpression() != null) {
                    reply = getMessageBuilderFactory().withPayload(e.getMessage()).setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, evaluateHttpStatus()).build();
                } else {
                    reply = getMessageBuilderFactory().withPayload(e.getMessage()).setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, HttpStatus.INTERNAL_SERVER_ERROR).build();
                }
            }
        } else {
            this.send(message);
        }
        return reply;
    } finally {
        this.postProcessRequest(servletRequest);
        this.activeCount.decrementAndGet();
    }
}
Also used : Cookie(javax.servlet.http.Cookie) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HttpEntity(org.springframework.http.HttpEntity) MultipartHttpInputMessage(org.springframework.integration.http.multipart.MultipartHttpInputMessage) Message(org.springframework.messaging.Message) HashMap(java.util.HashMap) Expression(org.springframework.expression.Expression) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 3 with MessageTimeoutException

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

the class TcpOutboundGatewayTests method testGoodNetTimeout.

@Test
public void testGoodNetTimeout() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            int i = 0;
            Socket socket = server.accept();
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ois.readObject();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                Thread.sleep(1000);
                oos.writeObject("Reply" + (i++));
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    ccf.start();
    final TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(ccf);
    gateway.setRequestTimeout(1);
    QueueChannel replyChannel = new QueueChannel();
    gateway.setRequiresReply(true);
    gateway.setOutputChannel(replyChannel);
    @SuppressWarnings("unchecked") Future<Integer>[] results = (Future<Integer>[]) new Future<?>[2];
    for (int i = 0; i < 2; i++) {
        final int j = i;
        results[j] = (this.executor.submit(() -> {
            gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build());
            return 0;
        }));
    }
    Set<String> replies = new HashSet<>();
    int timeouts = 0;
    for (int i = 0; i < 2; i++) {
        try {
            results[i].get();
        } catch (ExecutionException e) {
            if (timeouts > 0) {
                fail("Unexpected " + e.getMessage());
            } else {
                assertNotNull(e.getCause());
                assertTrue(e.getCause() instanceof MessageTimeoutException);
            }
            timeouts++;
            continue;
        }
        Message<?> m = replyChannel.receive(10000);
        assertNotNull(m);
        replies.add((String) m.getPayload());
    }
    if (timeouts < 1) {
        fail("Expected ExecutionException");
    }
    for (int i = 0; i < 1; i++) {
        assertTrue(replies.remove("Reply" + i));
    }
    done.set(true);
    gateway.stop();
    ccf.stop();
    serverSocket.get().close();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) EOFException(java.io.EOFException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Future(java.util.concurrent.Future) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 4 with MessageTimeoutException

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

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

the class TcpOutboundGateway method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    Assert.notNull(this.connectionFactory, this.getClass().getName() + " requires a client connection factory");
    boolean haveSemaphore = false;
    TcpConnection connection = null;
    String connectionId = null;
    try {
        if (!this.isSingleUse) {
            logger.debug("trying semaphore");
            if (!this.semaphore.tryAcquire(this.requestTimeout, TimeUnit.MILLISECONDS)) {
                throw new MessageTimeoutException(requestMessage, "Timed out waiting for connection");
            }
            haveSemaphore = true;
            if (logger.isDebugEnabled()) {
                logger.debug("got semaphore");
            }
        }
        connection = this.connectionFactory.getConnection();
        AsyncReply reply = new AsyncReply(this.remoteTimeoutExpression.getValue(this.evaluationContext, requestMessage, Long.class));
        connectionId = connection.getConnectionId();
        this.pendingReplies.put(connectionId, reply);
        if (logger.isDebugEnabled()) {
            logger.debug("Added pending reply " + connectionId);
        }
        connection.send(requestMessage);
        Message<?> replyMessage = reply.getReply();
        if (replyMessage == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Remote Timeout on " + connectionId);
            }
            // The connection is dirty - force it closed.
            this.connectionFactory.forceClose(connection);
            throw new MessageTimeoutException(requestMessage, "Timed out waiting for response");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Response " + replyMessage);
        }
        return replyMessage;
    } catch (Exception e) {
        logger.error("Tcp Gateway exception", e);
        if (e instanceof MessagingException) {
            throw (MessagingException) e;
        }
        throw new MessagingException("Failed to send or receive", e);
    } finally {
        if (connectionId != null) {
            this.pendingReplies.remove(connectionId);
            if (logger.isDebugEnabled()) {
                logger.debug("Removed pending reply " + connectionId);
            }
            if (this.isSingleUse) {
                connection.close();
            }
        }
        if (haveSemaphore) {
            this.semaphore.release();
            if (logger.isDebugEnabled()) {
                logger.debug("released semaphore");
            }
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException)

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