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