use of org.springframework.integration.MessageTimeoutException in project spring-integration by spring-projects.
the class MessagingGatewaySupport method doSendAndReceive.
@SuppressWarnings("unchecked")
private Object doSendAndReceive(Object object, boolean shouldConvert) {
this.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();
Object reply = null;
Throwable error = null;
Message<?> requestMessage = null;
try {
if (this.countsEnabled) {
this.messageCount.incrementAndGet();
}
if (shouldConvert) {
reply = this.messagingTemplate.convertSendAndReceive(requestChannel, object, Object.class, this.historyWritingPostProcessor);
if (reply instanceof Throwable) {
error = (Throwable) reply;
}
} else {
requestMessage = (object instanceof Message<?>) ? (Message<?>) object : this.requestMapper.toMessage(object);
requestMessage = this.historyWritingPostProcessor.postProcessMessage(requestMessage);
reply = this.messagingTemplate.sendAndReceive(requestChannel, requestMessage);
if (reply instanceof ErrorMessage) {
error = ((ErrorMessage) reply).getPayload();
}
}
if (reply == null && this.errorOnTimeout) {
if (object instanceof Message) {
error = new MessageTimeoutException((Message<?>) object, "No reply received within timeout");
} else {
error = new MessageTimeoutException("No reply received within timeout");
}
}
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("failure occurred in gateway sendAndReceive: " + e.getMessage());
}
error = e;
}
if (error != null) {
MessageChannel errorChannel = getErrorChannel();
if (errorChannel != null) {
ErrorMessage errorMessage = buildErrorMessage(requestMessage, error);
Message<?> errorFlowReply = null;
try {
errorFlowReply = this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
} catch (Exception errorFlowFailure) {
throw new MessagingException(errorMessage, "failure occurred in error-handling flow", errorFlowFailure);
}
if (shouldConvert) {
Object result = (errorFlowReply != null) ? errorFlowReply.getPayload() : null;
if (result instanceof Throwable) {
this.rethrow((Throwable) result, "error flow returned Exception");
}
return result;
}
if (errorFlowReply != null && errorFlowReply.getPayload() instanceof Throwable) {
this.rethrow((Throwable) errorFlowReply.getPayload(), "error flow returned an Error Message");
}
if (errorFlowReply == null && this.errorOnTimeout) {
if (object instanceof Message) {
throw new MessageTimeoutException((Message<?>) object, "No reply received from error channel within timeout");
} else {
throw new MessageTimeoutException("No reply received from error channel within timeout");
}
}
return errorFlowReply;
} else {
// no errorChannel so we'll propagate
this.rethrow(error, "gateway received checked Exception");
}
}
return reply;
}
use of org.springframework.integration.MessageTimeoutException in project spring-integration by spring-projects.
the class HttpRequestHandlingEndpointSupport method actualDoHandleRequest.
@SuppressWarnings({ "rawtypes", "unchecked" })
private Message<?> actualDoHandleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
this.activeCount.incrementAndGet();
try {
ServletServerHttpRequest request = this.prepareRequest(servletRequest);
Object requestBody = null;
if (isReadable(request)) {
requestBody = extractRequestBody(request);
}
HttpEntity httpEntity = new HttpEntity(requestBody, request.getHeaders());
StandardEvaluationContext evaluationContext = this.createEvaluationContext();
evaluationContext.setRootObject(httpEntity);
evaluationContext.setVariable("requestAttributes", RequestContextHolder.currentRequestAttributes());
MultiValueMap<String, String> requestParams = this.convertParameterMap(servletRequest.getParameterMap());
evaluationContext.setVariable("requestParams", requestParams);
evaluationContext.setVariable("requestHeaders", new ServletServerHttpRequest(servletRequest).getHeaders());
Cookie[] requestCookies = servletRequest.getCookies();
if (!ObjectUtils.isEmpty(requestCookies)) {
Map<String, Cookie> cookies = new HashMap<String, Cookie>(requestCookies.length);
for (Cookie requestCookie : requestCookies) {
cookies.put(requestCookie.getName(), requestCookie);
}
evaluationContext.setVariable("cookies", cookies);
}
Map<String, String> pathVariables = (Map<String, String>) servletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (!CollectionUtils.isEmpty(pathVariables)) {
if (logger.isDebugEnabled()) {
logger.debug("Mapped path variables: " + pathVariables);
}
evaluationContext.setVariable("pathVariables", pathVariables);
}
Map<String, MultiValueMap<String, String>> matrixVariables = (Map<String, MultiValueMap<String, String>>) servletRequest.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
if (!CollectionUtils.isEmpty(matrixVariables)) {
if (logger.isDebugEnabled()) {
logger.debug("Mapped matrix variables: " + matrixVariables);
}
evaluationContext.setVariable("matrixVariables", matrixVariables);
}
Map<String, Object> headers = getHeaderMapper().toHeaders(request.getHeaders());
Object payload = null;
if (getPayloadExpression() != null) {
// create payload based on SpEL
payload = getPayloadExpression().getValue(evaluationContext);
}
if (!CollectionUtils.isEmpty(getHeaderExpressions())) {
for (Entry<String, Expression> entry : getHeaderExpressions().entrySet()) {
String headerName = entry.getKey();
Expression headerExpression = entry.getValue();
Object headerValue = headerExpression.getValue(evaluationContext);
if (headerValue != null) {
headers.put(headerName, headerValue);
}
}
}
if (payload == null) {
if (requestBody != null) {
payload = requestBody;
} else {
payload = requestParams;
}
}
AbstractIntegrationMessageBuilder<?> messageBuilder = null;
if (payload instanceof Message<?>) {
messageBuilder = this.getMessageBuilderFactory().fromMessage((Message<?>) payload).copyHeadersIfAbsent(headers);
} else {
messageBuilder = this.getMessageBuilderFactory().withPayload(payload).copyHeaders(headers);
}
Message<?> message = messageBuilder.setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL, request.getURI().toString()).setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD, request.getMethod().toString()).setHeader(org.springframework.integration.http.HttpHeaders.USER_PRINCIPAL, servletRequest.getUserPrincipal()).build();
Message<?> reply = null;
if (this.expectReply) {
try {
reply = this.sendAndReceiveMessage(message);
} catch (MessageTimeoutException e) {
if (getStatusCodeExpression() != null) {
reply = getMessageBuilderFactory().withPayload(e.getMessage()).setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, evaluateHttpStatus()).build();
} else {
reply = getMessageBuilderFactory().withPayload(e.getMessage()).setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
} else {
this.send(message);
}
return reply;
} finally {
this.postProcessRequest(servletRequest);
this.activeCount.decrementAndGet();
}
}
use of org.springframework.integration.MessageTimeoutException in project spring-integration by spring-projects.
the class TcpOutboundGatewayTests method testGoodNetTimeout.
@Test
public void testGoodNetTimeout() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
serverSocket.set(server);
latch.countDown();
int i = 0;
Socket socket = server.accept();
while (true) {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ois.readObject();
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Thread.sleep(1000);
oos.writeObject("Reply" + (i++));
}
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
ccf.setSerializer(new DefaultSerializer());
ccf.setDeserializer(new DefaultDeserializer());
ccf.setSoTimeout(10000);
ccf.setSingleUse(false);
ccf.start();
final TcpOutboundGateway gateway = new TcpOutboundGateway();
gateway.setConnectionFactory(ccf);
gateway.setRequestTimeout(1);
QueueChannel replyChannel = new QueueChannel();
gateway.setRequiresReply(true);
gateway.setOutputChannel(replyChannel);
@SuppressWarnings("unchecked") Future<Integer>[] results = (Future<Integer>[]) new Future<?>[2];
for (int i = 0; i < 2; i++) {
final int j = i;
results[j] = (this.executor.submit(() -> {
gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build());
return 0;
}));
}
Set<String> replies = new HashSet<>();
int timeouts = 0;
for (int i = 0; i < 2; i++) {
try {
results[i].get();
} catch (ExecutionException e) {
if (timeouts > 0) {
fail("Unexpected " + e.getMessage());
} else {
assertNotNull(e.getCause());
assertTrue(e.getCause() instanceof MessageTimeoutException);
}
timeouts++;
continue;
}
Message<?> m = replyChannel.receive(10000);
assertNotNull(m);
replies.add((String) m.getPayload());
}
if (timeouts < 1) {
fail("Expected ExecutionException");
}
for (int i = 0; i < 1; i++) {
assertTrue(replies.remove("Reply" + i));
}
done.set(true);
gateway.stop();
ccf.stop();
serverSocket.get().close();
}
use of org.springframework.integration.MessageTimeoutException in project spring-integration by spring-projects.
the class PipelineJmsTests method test.
public void test(String contextConfig) throws Exception {
ActiveMqTestUtils.prepare();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(contextConfig, this.getClass());
final RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
final CountDownLatch latch = new CountDownLatch(requests);
final AtomicInteger successCounter = new AtomicInteger();
final AtomicInteger timeoutCounter = new AtomicInteger();
final AtomicInteger failureCounter = new AtomicInteger();
try {
for (int i = 0; i < requests; i++) {
final int y = i;
executor.execute(() -> {
try {
assertEquals(y, gateway.exchange(new GenericMessage<Integer>(y)).getPayload());
successCounter.incrementAndGet();
} catch (MessageTimeoutException e) {
timeoutCounter.incrementAndGet();
} catch (Throwable t) {
failureCounter.incrementAndGet();
} finally {
latch.countDown();
}
});
}
latch.await();
} finally {
logger.info("Test config: " + contextConfig);
logger.info("Success: " + successCounter.get());
logger.info("Timeout: " + timeoutCounter.get());
logger.info("Failure: " + failureCounter.get());
// technically all we care that its > 0,
// but reality of this test it has to be something more then 0
assertTrue(successCounter.get() > 1);
assertEquals(0, failureCounter.get());
assertEquals(requests, successCounter.get() + timeoutCounter.get());
context.close();
}
}
use of org.springframework.integration.MessageTimeoutException in project spring-integration by spring-projects.
the class TcpOutboundGateway method handleRequestMessage.
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
Assert.notNull(this.connectionFactory, this.getClass().getName() + " requires a client connection factory");
boolean haveSemaphore = false;
TcpConnection connection = null;
String connectionId = null;
try {
if (!this.isSingleUse) {
logger.debug("trying semaphore");
if (!this.semaphore.tryAcquire(this.requestTimeout, TimeUnit.MILLISECONDS)) {
throw new MessageTimeoutException(requestMessage, "Timed out waiting for connection");
}
haveSemaphore = true;
if (logger.isDebugEnabled()) {
logger.debug("got semaphore");
}
}
connection = this.connectionFactory.getConnection();
AsyncReply reply = new AsyncReply(this.remoteTimeoutExpression.getValue(this.evaluationContext, requestMessage, Long.class));
connectionId = connection.getConnectionId();
this.pendingReplies.put(connectionId, reply);
if (logger.isDebugEnabled()) {
logger.debug("Added pending reply " + connectionId);
}
connection.send(requestMessage);
Message<?> replyMessage = reply.getReply();
if (replyMessage == null) {
if (logger.isDebugEnabled()) {
logger.debug("Remote Timeout on " + connectionId);
}
// The connection is dirty - force it closed.
this.connectionFactory.forceClose(connection);
throw new MessageTimeoutException(requestMessage, "Timed out waiting for response");
}
if (logger.isDebugEnabled()) {
logger.debug("Response " + replyMessage);
}
return replyMessage;
} catch (Exception e) {
logger.error("Tcp Gateway exception", e);
if (e instanceof MessagingException) {
throw (MessagingException) e;
}
throw new MessagingException("Failed to send or receive", e);
} finally {
if (connectionId != null) {
this.pendingReplies.remove(connectionId);
if (logger.isDebugEnabled()) {
logger.debug("Removed pending reply " + connectionId);
}
if (this.isSingleUse) {
connection.close();
}
}
if (haveSemaphore) {
this.semaphore.release();
if (logger.isDebugEnabled()) {
logger.debug("released semaphore");
}
}
}
}
Aggregations