Search in sources :

Example 1 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project spring-framework by spring-projects.

the class GenericMessagingTemplate method doSend.

@Override
protected final void doSend(MessageChannel channel, Message<?> message) {
    Assert.notNull(channel, "MessageChannel is required");
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor != null && accessor.isMutable()) {
        accessor.setImmutable();
    }
    long timeout = this.sendTimeout;
    boolean sent = (timeout >= 0 ? channel.send(message, timeout) : channel.send(message));
    if (!sent) {
        throw new MessageDeliveryException(message, "Failed to send message to channel '" + channel + "' within timeout: " + timeout);
    }
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 2 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException in project camel by apache.

the class CamelTargetAdapter method send.

public boolean send(Message<?> message) throws Exception {
    boolean result = false;
    ExchangePattern pattern;
    if (isExpectReply()) {
        pattern = ExchangePattern.InOut;
    } else {
        pattern = ExchangePattern.InOnly;
    }
    Exchange inExchange = new DefaultExchange(getCamelContext(), pattern);
    SpringIntegrationBinding.storeToCamelMessage(message, inExchange.getIn());
    Exchange outExchange = getCamelTemplate().send(getCamelEndpointUri(), inExchange);
    org.apache.camel.Message camelMsg = outExchange.hasOut() ? outExchange.getOut() : outExchange.getIn();
    if (camelMsg.isFault()) {
        result = true;
    }
    Message<?> response;
    if (isExpectReply()) {
        //Check the message header for the return address
        response = SpringIntegrationBinding.storeToSpringIntegrationMessage(outExchange.getOut());
        if (replyChannel == null) {
            MessageChannel messageReplyChannel = (MessageChannel) message.getHeaders().get(MessageHeaders.REPLY_CHANNEL);
            if (messageReplyChannel != null) {
                result = messageReplyChannel.send(response);
            } else {
                throw new MessageDeliveryException(response, "Cannot resolve ReplyChannel from message: " + message);
            }
        } else {
            result = replyChannel.send(response);
        }
    }
    return result;
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) MessageChannel(org.springframework.messaging.MessageChannel) ExchangePattern(org.apache.camel.ExchangePattern) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 3 with MessageDeliveryException

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

the class MessagingGatewaySupport method doSendAndReceiveMessageReactive.

@SuppressWarnings("unchecked")
private Mono<Message<?>> doSendAndReceiveMessageReactive(MessageChannel requestChannel, Object object, boolean error) {
    return Mono.defer(() -> {
        Message<?> message;
        try {
            message = object instanceof Message<?> ? (Message<?>) object : this.requestMapper.toMessage(object);
            message = this.historyWritingPostProcessor.postProcessMessage(message);
        } catch (Exception e) {
            throw new MessageMappingException("Cannot map to message: " + object, e);
        }
        Object originalReplyChannelHeader = message.getHeaders().getReplyChannel();
        Object originalErrorChannelHeader = message.getHeaders().getErrorChannel();
        FutureReplyChannel replyChannel = new FutureReplyChannel();
        Message<?> requestMessage = MutableMessageBuilder.fromMessage(message).setReplyChannel(replyChannel).setHeader(this.messagingTemplate.getSendTimeoutHeader(), null).setHeader(this.messagingTemplate.getReceiveTimeoutHeader(), null).setErrorChannel(replyChannel).build();
        if (requestChannel instanceof ReactiveStreamsSubscribableChannel) {
            ((ReactiveStreamsSubscribableChannel) requestChannel).subscribeTo(Mono.just(requestMessage));
        } else {
            long sendTimeout = sendTimeout(requestMessage);
            boolean sent = sendTimeout >= 0 ? requestChannel.send(requestMessage, sendTimeout) : requestChannel.send(requestMessage);
            if (!sent) {
                throw new MessageDeliveryException(requestMessage, "Failed to send message to channel '" + requestChannel + "' within timeout: " + sendTimeout);
            }
        }
        return Mono.fromFuture(replyChannel.messageFuture).doOnSubscribe(s -> {
            if (!error && this.countsEnabled) {
                this.messageCount.incrementAndGet();
            }
        }).<Message<?>>map(replyMessage -> MessageBuilder.fromMessage(replyMessage).setHeader(MessageHeaders.REPLY_CHANNEL, originalReplyChannelHeader).setHeader(MessageHeaders.ERROR_CHANNEL, originalErrorChannelHeader).build()).onErrorResume(t -> error ? Mono.error(t) : handleSendError(requestMessage, t));
    });
}
Also used : MessagingException(org.springframework.messaging.MessagingException) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) TrackableComponent(org.springframework.integration.support.management.TrackableComponent) IntegrationManagedResource(org.springframework.integration.support.management.IntegrationManagedResource) SubscribableChannel(org.springframework.messaging.SubscribableChannel) CompletableFuture(java.util.concurrent.CompletableFuture) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ErrorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) OutboundMessageMapper(org.springframework.integration.mapping.OutboundMessageMapper) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) Map(java.util.Map) InboundMessageMapper(org.springframework.integration.mapping.InboundMessageMapper) Nullable(org.springframework.lang.Nullable) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Subscriber(org.reactivestreams.Subscriber) HistoryWritingMessagePostProcessor(org.springframework.integration.history.HistoryWritingMessagePostProcessor) DefaultErrorMessageStrategy(org.springframework.integration.support.DefaultErrorMessageStrategy) MessageBuilderFactory(org.springframework.integration.support.MessageBuilderFactory) SimpleMessageConverter(org.springframework.integration.support.converter.SimpleMessageConverter) Mono(reactor.core.publisher.Mono) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) MessageSourceMetrics(org.springframework.integration.support.management.MessageSourceMetrics) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) BridgeHandler(org.springframework.integration.handler.BridgeHandler) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AttributeAccessor(org.springframework.core.AttributeAccessor) MessageMappingException(org.springframework.integration.mapping.MessageMappingException) ErrorMessageUtils(org.springframework.integration.support.ErrorMessageUtils) MessageBuilder(org.springframework.messaging.support.MessageBuilder) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) MutableMessageBuilder(org.springframework.integration.support.MutableMessageBuilder) Assert(org.springframework.util.Assert) MessageMappingException(org.springframework.integration.mapping.MessageMappingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageMappingException(org.springframework.integration.mapping.MessageMappingException)

Example 4 with MessageDeliveryException

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

the class DelayHandlerTests method errorChannelNameHeaderAndHandlerThrowsExceptionWithDelay.

@Test
public void errorChannelNameHeaderAndHandlerThrowsExceptionWithDelay() throws Exception {
    String errorChannelName = "customErrorChannel";
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(errorChannelName, DirectChannel.class);
    context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
    context.refresh();
    DirectChannel customErrorChannel = (DirectChannel) context.getBean(errorChannelName);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setBeanFactory(context);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    customErrorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").setErrorChannelName(errorChannelName).build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 5 with MessageDeliveryException

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

the class DelayHandlerTests method defaultErrorChannelAndHandlerThrowsExceptionWithDelay.

@Test
public void defaultErrorChannelAndHandlerThrowsExceptionWithDelay() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
    context.refresh();
    DirectChannel defaultErrorChannel = (DirectChannel) context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setBeanFactory(context);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    defaultErrorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Aggregations

MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)47 Test (org.junit.Test)19 GenericMessage (org.springframework.messaging.support.GenericMessage)17 QueueChannel (org.springframework.integration.channel.QueueChannel)13 MessagingException (org.springframework.messaging.MessagingException)10 MessageChannel (org.springframework.messaging.MessageChannel)8 Message (org.springframework.messaging.Message)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 MessageHandler (org.springframework.messaging.MessageHandler)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 PollableChannel (org.springframework.messaging.PollableChannel)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)3 FileNotFoundException (java.io.FileNotFoundException)2 Date (java.util.Date)2 List (java.util.List)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2