use of org.springframework.messaging.MessageHandlingException in project spring-cloud-stream by spring-cloud.
the class DefaultPollableMessageSource method poll.
@Override
public boolean poll(MessageHandler handler, ParameterizedTypeReference<?> type) {
Message<?> message = this.receive(type);
if (message == null) {
return false;
}
AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
try {
if (this.retryTemplate == null) {
if (this.errorChannel == null) {
this.handle(message, handler);
} else {
try {
this.handle(message, handler);
} catch (Exception e) {
this.messagingTemplate.send(this.errorChannel, this.errorMessageStrategy.buildErrorMessage(e, attributesHolder.get()));
}
}
} else {
this.retryTemplate.execute(context -> {
this.handle(message, handler);
return null;
}, this.recoveryCallback);
}
return true;
} catch (Exception e) {
AckUtils.autoNack(ackCallback);
if (e instanceof MessageHandlingException && ((MessageHandlingException) e).getFailedMessage().equals(message)) {
throw (MessageHandlingException) e;
}
throw new MessageHandlingException(message, e);
} finally {
AckUtils.autoAck(ackCallback);
}
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class EnricherParserTests3 method testTargetBeanResolver.
@Test
public void testTargetBeanResolver() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail-context.xml", this.getClass());
MessageChannel beanResolveIn = context.getBean("beanResolveIn", MessageChannel.class);
SomeBean payload = new SomeBean("foo");
assertEquals("foo", payload.getNested().getValue());
try {
beanResolveIn.send(new GenericMessage<SomeBean>(payload));
fail("Expected SpEL Exception");
} catch (MessageHandlingException e) {
assertTrue(e.getCause() instanceof SpelEvaluationException);
}
context.close();
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class MessageSourcePollingTemplate method poll.
@Override
public boolean poll(MessageHandler handler) {
Assert.notNull(handler, "'handler' cannot be null");
Message<?> message = this.source.receive();
if (message != null) {
AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
try {
handler.handleMessage(message);
AckUtils.autoAck(ackCallback);
} catch (Exception e) {
AckUtils.autoNack(ackCallback);
throw new MessageHandlingException(message, e);
}
return true;
}
return false;
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class AbstractMessageProducingHandler method produceOutput.
protected void produceOutput(Object reply, final Message<?> requestMessage) {
final MessageHeaders requestHeaders = requestMessage.getHeaders();
Object replyChannel = null;
if (getOutputChannel() == null) {
Map<?, ?> routingSlipHeader = requestHeaders.get(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Map.class);
if (routingSlipHeader != null) {
Assert.isTrue(routingSlipHeader.size() == 1, "The RoutingSlip header value must be a SingletonMap");
Object key = routingSlipHeader.keySet().iterator().next();
Object value = routingSlipHeader.values().iterator().next();
Assert.isInstanceOf(List.class, key, "The RoutingSlip key must be List");
Assert.isInstanceOf(Integer.class, value, "The RoutingSlip value must be Integer");
List<?> routingSlip = (List<?>) key;
AtomicInteger routingSlipIndex = new AtomicInteger((Integer) value);
replyChannel = getOutputChannelFromRoutingSlip(reply, requestMessage, routingSlip, routingSlipIndex);
if (replyChannel != null) {
// TODO Migrate to the SF MessageBuilder
AbstractIntegrationMessageBuilder<?> builder = null;
if (reply instanceof Message) {
builder = this.getMessageBuilderFactory().fromMessage((Message<?>) reply);
} else if (reply instanceof AbstractIntegrationMessageBuilder) {
builder = (AbstractIntegrationMessageBuilder<?>) reply;
} else {
builder = this.getMessageBuilderFactory().withPayload(reply);
}
builder.setHeader(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Collections.singletonMap(routingSlip, routingSlipIndex.get()));
reply = builder;
}
}
if (replyChannel == null) {
replyChannel = requestHeaders.getReplyChannel();
if (replyChannel == null && reply instanceof Message) {
replyChannel = ((Message<?>) reply).getHeaders().getReplyChannel();
}
}
}
if (this.async && (reply instanceof ListenableFuture<?> || reply instanceof Publisher<?>)) {
if (reply instanceof ListenableFuture<?> || !(getOutputChannel() instanceof ReactiveStreamsSubscribableChannel)) {
ListenableFuture<?> future;
if (reply instanceof ListenableFuture<?>) {
future = (ListenableFuture<?>) reply;
} else {
SettableListenableFuture<Object> settableListenableFuture = new SettableListenableFuture<>();
Mono.from((Publisher<?>) reply).subscribe(settableListenableFuture::set, settableListenableFuture::setException);
future = settableListenableFuture;
}
Object theReplyChannel = replyChannel;
future.addCallback(new ListenableFutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
Message<?> replyMessage = null;
try {
replyMessage = createOutputMessage(result, requestHeaders);
sendOutput(replyMessage, theReplyChannel, false);
} catch (Exception e) {
Exception exceptionToLogAndSend = e;
if (!(e instanceof MessagingException)) {
exceptionToLogAndSend = new MessageHandlingException(requestMessage, e);
if (replyMessage != null) {
exceptionToLogAndSend = new MessagingException(replyMessage, exceptionToLogAndSend);
}
}
logger.error("Failed to send async reply: " + result.toString(), exceptionToLogAndSend);
onFailure(exceptionToLogAndSend);
}
}
@Override
public void onFailure(Throwable ex) {
sendErrorMessage(requestMessage, ex);
}
});
} else {
((ReactiveStreamsSubscribableChannel) getOutputChannel()).subscribeTo(Flux.from((Publisher<?>) reply).map(result -> createOutputMessage(result, requestHeaders)));
}
} else {
sendOutput(createOutputMessage(reply, requestHeaders), replyChannel, false);
}
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class AbstractMessageProducingHandler method sendErrorMessage.
protected void sendErrorMessage(final Message<?> requestMessage, Throwable ex) {
Object errorChannel = resolveErrorChannel(requestMessage.getHeaders());
Throwable result = ex;
if (!(ex instanceof MessagingException)) {
result = new MessageHandlingException(requestMessage, ex);
}
if (errorChannel == null) {
logger.error("Async exception received and no 'errorChannel' header exists and no default " + "'errorChannel' found", result);
} else {
try {
sendOutput(new ErrorMessage(result), errorChannel, true);
} catch (Exception e) {
Exception exceptionToLog = e;
if (!(e instanceof MessagingException)) {
exceptionToLog = new MessageHandlingException(requestMessage, e);
}
logger.error("Failed to send async reply", exceptionToLog);
}
}
}
Aggregations