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);
}
}
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;
}
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));
});
}
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);
}
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);
}
Aggregations