Search in sources :

Example 6 with MessageHandlingException

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

the class BarrierMessageHandler method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    Object key = this.correlationStrategy.getCorrelationKey(requestMessage);
    if (key == null) {
        throw new MessagingException(requestMessage, "Correlation Strategy returned null");
    }
    Thread existing = this.inProcess.putIfAbsent(key, Thread.currentThread());
    if (existing != null) {
        throw new MessagingException(requestMessage, "Correlation key (" + key + ") is already in use by " + existing.getName());
    }
    SynchronousQueue<Message<?>> syncQueue = createOrObtainQueue(key);
    try {
        Message<?> releaseMessage = syncQueue.poll(this.timeout, TimeUnit.MILLISECONDS);
        if (releaseMessage != null) {
            return processRelease(key, requestMessage, releaseMessage);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessageHandlingException(requestMessage, "Interrupted while waiting for release", e);
    } finally {
        this.inProcess.remove(key);
        this.suspensions.remove(key);
    }
    return null;
}
Also used : Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 7 with MessageHandlingException

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

the class RouterTests method testExceptionTypeRouteFlow.

@Test
public void testExceptionTypeRouteFlow() {
    Message<?> failedMessage = new GenericMessage<>("foo");
    IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
    RuntimeException middleCause = new RuntimeException(rootCause);
    MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
    ErrorMessage message = new ErrorMessage(error);
    this.exceptionTypeRouteFlowInput.send(message);
    assertNotNull(this.illegalArgumentChannel.receive(1000));
    assertNull(this.exceptionRouterDefaultChannel.receive(0));
    assertNull(this.runtimeExceptionChannel.receive(0));
    assertNull(this.messageHandlingExceptionChannel.receive(0));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 8 with MessageHandlingException

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

the class ContentEnricherTests method clonePayloadWithFailure.

@Test
public void clonePayloadWithFailure() {
    QueueChannel replyChannel = new QueueChannel();
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("John", "Doe");
        }
    });
    ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setShouldClonePayload(true);
    SpelExpressionParser parser = new SpelExpressionParser();
    Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
    propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
    enricher.setPropertyExpressions(propertyExpressions);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    UncloneableTargetUser target = new UncloneableTargetUser();
    target.setName("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    try {
        enricher.handleMessage(requestMessage);
    } catch (MessageHandlingException e) {
        assertThat(e.getMessage(), containsString("Failed to clone payload object"));
        return;
    }
    fail("Expected a MessageHandlingException to be thrown.");
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 9 with MessageHandlingException

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

the class TcpSendingMessageHandler method doWrite.

/**
 * Method that actually does the write.
 * @param message The message to write.
 * @return the connection.
 */
protected TcpConnection doWrite(Message<?> message) {
    TcpConnection connection = null;
    try {
        connection = obtainConnection(message);
        if (logger.isDebugEnabled()) {
            logger.debug("Got Connection " + connection.getConnectionId());
        }
        connection.send(message);
    } catch (Exception e) {
        String connectionId = null;
        if (connection != null) {
            connectionId = connection.getConnectionId();
        }
        if (e instanceof MessageHandlingException) {
            throw (MessageHandlingException) e;
        }
        throw new MessageHandlingException(message, "Failed to handle message using " + connectionId, e);
    }
    return connection;
}
Also used : TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 10 with MessageHandlingException

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

the class TcpSendingMessageHandler method handleMessageInternal.

/**
 * Writes the message payload to the underlying socket, using the specified
 * message format.
 * @see org.springframework.messaging.MessageHandler#handleMessage(org.springframework.messaging.Message)
 */
@Override
public void handleMessageInternal(final Message<?> message) throws MessageHandlingException {
    if (this.serverConnectionFactory != null) {
        // We don't own the connection, we are asynchronously replying
        Object connectionId = message.getHeaders().get(IpHeaders.CONNECTION_ID);
        TcpConnection connection = null;
        if (connectionId != null) {
            connection = this.connections.get(connectionId);
        }
        if (connection != null) {
            try {
                connection.send(message);
            } catch (Exception e) {
                logger.error("Error sending message", e);
                connection.close();
                if (e instanceof MessageHandlingException) {
                    throw (MessageHandlingException) e;
                } else {
                    throw new MessageHandlingException(message, "Error sending message", e);
                }
            } finally {
                if (this.isSingleUse) {
                    // close after replying
                    connection.close();
                }
            }
        } else {
            logger.error("Unable to find outbound socket for " + message);
            MessageHandlingException messageHandlingException = new MessageHandlingException(message, "Unable to find outbound socket");
            publishNoConnectionEvent(messageHandlingException, (String) connectionId);
            throw messageHandlingException;
        }
        return;
    } else {
        // we own the connection
        TcpConnection connection = null;
        try {
            connection = doWrite(message);
        } catch (MessageHandlingException e) {
            // retry - socket may have closed
            if (e.getCause() instanceof IOException) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Fail on first write attempt", e);
                }
                connection = doWrite(message);
            } else {
                throw e;
            }
        } finally {
            if (connection != null && this.isSingleUse && this.clientConnectionFactory.getListener() == null) {
                // if there's no collaborating inbound adapter, close immediately, otherwise
                // it will close after receiving the reply.
                connection.close();
            }
        }
    }
}
Also used : TcpConnection(org.springframework.integration.ip.tcp.connection.TcpConnection) IOException(java.io.IOException) IOException(java.io.IOException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Aggregations

MessageHandlingException (org.springframework.messaging.MessageHandlingException)66 Test (org.junit.Test)34 ErrorMessage (org.springframework.messaging.support.ErrorMessage)14 IOException (java.io.IOException)12 GenericMessage (org.springframework.messaging.support.GenericMessage)12 Message (org.springframework.messaging.Message)10 MessagingException (org.springframework.messaging.MessagingException)10 File (java.io.File)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 MessageRejectedException (org.springframework.integration.MessageRejectedException)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)3