use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class NioFileLocker method lock.
@Override
public boolean lock(File fileToLock) {
FileLock lock = this.lockCache.get(fileToLock);
if (lock == null) {
FileLock newLock = null;
try {
newLock = FileChannelCache.tryLockFor(fileToLock);
} catch (IOException e) {
throw new MessagingException("Failed to lock file: " + fileToLock, e);
}
if (newLock != null) {
FileLock original = this.lockCache.putIfAbsent(fileToLock, newLock);
lock = original != null ? original : newLock;
}
}
return lock != null;
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class ExpressionEvaluatingRequestHandlerAdvice method evaluateFailureExpression.
private Object evaluateFailureExpression(Message<?> message, Exception exception) throws Exception {
Object evalResult;
try {
evalResult = this.onFailureExpression.getValue(this.prepareEvaluationContextToUse(exception), message);
} catch (Exception e) {
evalResult = e;
logger.error("Failure expression evaluation failed for " + message + ": " + e.getMessage());
}
if (this.failureChannel == null && this.failureChannelName != null && getChannelResolver() != null) {
this.failureChannel = getChannelResolver().resolveDestination(this.failureChannelName);
}
if (evalResult != null && this.failureChannel != null) {
MessagingException messagingException = new MessageHandlingExpressionEvaluatingAdviceException(message, "Handler Failed", this.unwrapThrowableIfNecessary(exception), evalResult);
ErrorMessage resultMessage = new ErrorMessage(messagingException);
this.messagingTemplate.send(this.failureChannel, resultMessage);
}
return evalResult;
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class MessagingGatewaySupport method sendAndReceiveMessageReactive.
protected Mono<Message<?>> sendAndReceiveMessageReactive(Object object) {
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();
return doSendAndReceiveMessageReactive(requestChannel, object, false);
}
use of org.springframework.messaging.MessagingException in project spring-integration by spring-projects.
the class AbstractMessageSource method buildMessage.
@SuppressWarnings("unchecked")
protected Message<T> buildMessage(Object result) {
Message<T> message = null;
Map<String, Object> headers = evaluateHeaders();
if (result instanceof AbstractIntegrationMessageBuilder) {
if (!CollectionUtils.isEmpty(headers)) {
((AbstractIntegrationMessageBuilder<T>) result).copyHeaders(headers);
}
message = ((AbstractIntegrationMessageBuilder<T>) result).build();
} else if (result instanceof Message<?>) {
try {
message = (Message<T>) result;
} catch (Exception e) {
throw new MessagingException("MessageSource returned unexpected type.", e);
}
if (!CollectionUtils.isEmpty(headers)) {
// create a new Message from this one in order to apply headers
message = getMessageBuilderFactory().fromMessage(message).copyHeaders(headers).build();
}
} else if (result != null) {
T payload;
try {
payload = (T) result;
} catch (Exception e) {
throw new MessagingException("MessageSource returned unexpected type.", e);
}
message = getMessageBuilderFactory().withPayload(payload).copyHeaders(headers).build();
}
if (this.countsEnabled && message != null) {
if (this.metricsCaptor != null) {
incrementReceiveCounter();
}
this.messageCount.incrementAndGet();
}
return message;
}
use of org.springframework.messaging.MessagingException 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);
}
}
Aggregations