Search in sources :

Example 16 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class DeferredResultReturnValueHandlerTests method listenableFuture.

@Test
public void listenableFuture() throws Exception {
    MethodParameter returnType = returnType("handleListenableFuture");
    SettableListenableFuture<String> future = new SettableListenableFuture<>();
    handleReturnValue(future, returnType);
    assertTrue(this.request.isAsyncStarted());
    assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
    future.set("foo");
    assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
    assertEquals("foo", WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 17 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-integration by spring-projects.

the class AsyncAmqpGatewayTests method testConfirmsAndReturns.

@Test
public void testConfirmsAndReturns() throws Exception {
    CachingConnectionFactory ccf = new CachingConnectionFactory("localhost");
    ccf.setPublisherConfirms(true);
    ccf.setPublisherReturns(true);
    RabbitTemplate template = new RabbitTemplate(ccf);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(ccf);
    container.setBeanName("replyContainer");
    container.setQueueNames("asyncRQ1");
    container.afterPropertiesSet();
    container.start();
    AsyncRabbitTemplate asyncTemplate = new AsyncRabbitTemplate(template, container);
    asyncTemplate.setEnableConfirms(true);
    asyncTemplate.setMandatory(true);
    SimpleMessageListenerContainer receiver = new SimpleMessageListenerContainer(ccf);
    receiver.setBeanName("receiver");
    receiver.setQueueNames("asyncQ1");
    final CountDownLatch waitForAckBeforeReplying = new CountDownLatch(1);
    MessageListenerAdapter messageListener = new MessageListenerAdapter((ReplyingMessageListener<String, String>) foo -> {
        try {
            waitForAckBeforeReplying.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return foo.toUpperCase();
    });
    receiver.setMessageListener(messageListener);
    receiver.afterPropertiesSet();
    receiver.start();
    AsyncAmqpOutboundGateway gateway = new AsyncAmqpOutboundGateway(asyncTemplate);
    Log logger = spy(TestUtils.getPropertyValue(gateway, "logger", Log.class));
    given(logger.isDebugEnabled()).willReturn(true);
    final CountDownLatch replyTimeoutLatch = new CountDownLatch(1);
    willAnswer(invocation -> {
        invocation.callRealMethod();
        replyTimeoutLatch.countDown();
        return null;
    }).given(logger).debug(startsWith("Reply not required and async timeout for"));
    new DirectFieldAccessor(gateway).setPropertyValue("logger", logger);
    QueueChannel outputChannel = new QueueChannel();
    outputChannel.setBeanName("output");
    QueueChannel returnChannel = new QueueChannel();
    returnChannel.setBeanName("returns");
    QueueChannel ackChannel = new QueueChannel();
    ackChannel.setBeanName("acks");
    QueueChannel errorChannel = new QueueChannel();
    errorChannel.setBeanName("errors");
    gateway.setOutputChannel(outputChannel);
    gateway.setReturnChannel(returnChannel);
    gateway.setConfirmAckChannel(ackChannel);
    gateway.setConfirmNackChannel(ackChannel);
    gateway.setConfirmCorrelationExpressionString("#this");
    gateway.setExchangeName("");
    gateway.setRoutingKey("asyncQ1");
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    gateway.start();
    Message<?> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    gateway.handleMessage(message);
    Message<?> ack = ackChannel.receive(10000);
    assertNotNull(ack);
    assertEquals("foo", ack.getPayload());
    assertEquals(true, ack.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM));
    waitForAckBeforeReplying.countDown();
    Message<?> received = outputChannel.receive(10000);
    assertNotNull(received);
    assertEquals("FOO", received.getPayload());
    // timeout tests
    asyncTemplate.setReceiveTimeout(10);
    receiver.setMessageListener(message1 -> {
    });
    // reply timeout with no requiresReply
    message = MessageBuilder.withPayload("bar").setErrorChannel(errorChannel).build();
    gateway.handleMessage(message);
    assertTrue(replyTimeoutLatch.await(10, TimeUnit.SECONDS));
    // reply timeout with requiresReply
    gateway.setRequiresReply(true);
    message = MessageBuilder.withPayload("baz").setErrorChannel(errorChannel).build();
    gateway.handleMessage(message);
    received = errorChannel.receive(10000);
    assertThat(received, instanceOf(ErrorMessage.class));
    ErrorMessage error = (ErrorMessage) received;
    assertThat(error.getPayload(), instanceOf(MessagingException.class));
    assertThat(error.getPayload().getCause(), instanceOf(AmqpReplyTimeoutException.class));
    asyncTemplate.setReceiveTimeout(30000);
    receiver.setMessageListener(messageListener);
    // error on sending result
    DirectChannel errorForce = new DirectChannel();
    errorForce.setBeanName("errorForce");
    errorForce.subscribe(message1 -> {
        throw new RuntimeException("intentional");
    });
    gateway.setOutputChannel(errorForce);
    message = MessageBuilder.withPayload("qux").setErrorChannel(errorChannel).build();
    gateway.handleMessage(message);
    received = errorChannel.receive(10000);
    assertThat(received, instanceOf(ErrorMessage.class));
    error = (ErrorMessage) received;
    assertThat(error.getPayload(), instanceOf(MessagingException.class));
    assertEquals("QUX", ((MessagingException) error.getPayload()).getFailedMessage().getPayload());
    gateway.setRoutingKey(UUID.randomUUID().toString());
    message = MessageBuilder.withPayload("fiz").setErrorChannel(errorChannel).build();
    gateway.handleMessage(message);
    Message<?> returned = returnChannel.receive(10000);
    assertNotNull(returned);
    assertThat(returned, instanceOf(ErrorMessage.class));
    assertThat(returned.getPayload(), instanceOf(ReturnedAmqpMessageException.class));
    ReturnedAmqpMessageException payload = (ReturnedAmqpMessageException) returned.getPayload();
    assertEquals("fiz", payload.getFailedMessage().getPayload());
    ackChannel.receive(10000);
    ackChannel.purge(null);
    asyncTemplate = mock(AsyncRabbitTemplate.class);
    RabbitMessageFuture future = asyncTemplate.new RabbitMessageFuture(null, null);
    willReturn(future).given(asyncTemplate).sendAndReceive(anyString(), anyString(), any(org.springframework.amqp.core.Message.class));
    DirectFieldAccessor dfa = new DirectFieldAccessor(future);
    dfa.setPropertyValue("nackCause", "nacknack");
    SettableListenableFuture<Boolean> confirmFuture = new SettableListenableFuture<Boolean>();
    confirmFuture.set(false);
    dfa.setPropertyValue("confirm", confirmFuture);
    new DirectFieldAccessor(gateway).setPropertyValue("template", asyncTemplate);
    message = MessageBuilder.withPayload("buz").setErrorChannel(errorChannel).build();
    gateway.handleMessage(message);
    ack = ackChannel.receive(10000);
    assertNotNull(ack);
    assertThat(returned, instanceOf(ErrorMessage.class));
    assertThat(returned.getPayload(), instanceOf(ReturnedAmqpMessageException.class));
    NackedAmqpMessageException nack = (NackedAmqpMessageException) ack.getPayload();
    assertEquals("buz", nack.getFailedMessage().getPayload());
    assertEquals("nacknack", nack.getNackReason());
    asyncTemplate.stop();
    receiver.stop();
    ccf.destroy();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Mockito.spy(org.mockito.Mockito.spy) ReturnedAmqpMessageException(org.springframework.integration.amqp.support.ReturnedAmqpMessageException) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) BDDMockito.willReturn(org.mockito.BDDMockito.willReturn) AmqpReplyTimeoutException(org.springframework.amqp.core.AmqpReplyTimeoutException) RabbitMessageFuture(org.springframework.amqp.rabbit.AsyncRabbitTemplate.RabbitMessageFuture) BDDMockito.given(org.mockito.BDDMockito.given) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) BrokerRunning(org.springframework.amqp.rabbit.junit.BrokerRunning) MessageListenerAdapter(org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter) ReplyingMessageListener(org.springframework.amqp.rabbit.listener.adapter.ReplyingMessageListener) Message(org.springframework.messaging.Message) ClassRule(org.junit.ClassRule) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) AfterClass(org.junit.AfterClass) ArgumentMatchers.startsWith(org.mockito.ArgumentMatchers.startsWith) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUID(java.util.UUID) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtils(org.springframework.amqp.utils.test.TestUtils) NackedAmqpMessageException(org.springframework.integration.amqp.support.NackedAmqpMessageException) Rule(org.junit.Rule) Log4j2LevelAdjuster(org.springframework.integration.test.rule.Log4j2LevelAdjuster) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) AmqpHeaders(org.springframework.amqp.support.AmqpHeaders) DirectChannel(org.springframework.integration.channel.DirectChannel) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmqpReplyTimeoutException(org.springframework.amqp.core.AmqpReplyTimeoutException) ReturnedAmqpMessageException(org.springframework.integration.amqp.support.ReturnedAmqpMessageException) BeanFactory(org.springframework.beans.factory.BeanFactory) NackedAmqpMessageException(org.springframework.integration.amqp.support.NackedAmqpMessageException) AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) Log(org.apache.commons.logging.Log) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) CountDownLatch(java.util.concurrent.CountDownLatch) MessageListenerAdapter(org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter) RabbitMessageFuture(org.springframework.amqp.rabbit.AsyncRabbitTemplate.RabbitMessageFuture) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 18 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture 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);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) Arrays(java.util.Arrays) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) HashMap(java.util.HashMap) ErrorMessage(org.springframework.messaging.support.ErrorMessage) ListenableFutureCallback(org.springframework.util.concurrent.ListenableFutureCallback) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Map(java.util.Map) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) RoutingSlipRouteStrategy(org.springframework.integration.routingslip.RoutingSlipRouteStrategy) Message(org.springframework.messaging.Message) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) ObjectUtils(org.springframework.util.ObjectUtils) PatternMatchUtils(org.springframework.util.PatternMatchUtils) Set(java.util.Set) Mono(reactor.core.publisher.Mono) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) Flux(reactor.core.publisher.Flux) List(java.util.List) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) Publisher(org.reactivestreams.Publisher) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) List(java.util.List) MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 19 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class WebSocketTransport method connect.

@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
    final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<>();
    WebSocketClientSockJsSession session = new WebSocketClientSockJsSession(request, handler, future);
    handler = new ClientSockJsWebSocketHandler(session);
    request.addTimeoutTask(session.getTimeoutTask());
    URI url = request.getTransportUrl();
    WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHandshakeHeaders());
    if (logger.isDebugEnabled()) {
        logger.debug("Starting WebSocket session on " + url);
    }
    this.webSocketClient.doHandshake(handler, headers, url).addCallback(new ListenableFutureCallback<WebSocketSession>() {

        @Override
        public void onSuccess(@Nullable WebSocketSession webSocketSession) {
        // WebSocket session ready, SockJS Session not yet
        }

        @Override
        public void onFailure(Throwable ex) {
            future.setException(ex);
        }
    });
    return future;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) WebSocketHttpHeaders(org.springframework.web.socket.WebSocketHttpHeaders) URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 20 with SettableListenableFuture

use of org.springframework.util.concurrent.SettableListenableFuture in project spring-framework by spring-projects.

the class AbstractXhrTransport method connect.

// Transport methods
@Override
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
    SettableListenableFuture<WebSocketSession> connectFuture = new SettableListenableFuture<>();
    XhrClientSockJsSession session = new XhrClientSockJsSession(request, handler, this, connectFuture);
    request.addTimeoutTask(session.getTimeoutTask());
    URI receiveUrl = request.getTransportUrl();
    if (logger.isDebugEnabled()) {
        logger.debug("Starting XHR " + (isXhrStreamingDisabled() ? "Polling" : "Streaming") + "session url=" + receiveUrl);
    }
    HttpHeaders handshakeHeaders = new HttpHeaders();
    handshakeHeaders.putAll(request.getHandshakeHeaders());
    connectInternal(request, handler, receiveUrl, handshakeHeaders, session, connectFuture);
    return connectFuture;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) URI(java.net.URI) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Aggregations

SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)24 Test (org.junit.Test)8 List (java.util.List)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 MessagingException (org.springframework.messaging.MessagingException)3 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 URI (java.net.URI)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Transformer (javax.xml.transform.Transformer)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 Log (org.apache.commons.logging.Log)2 AcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)2 BasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage)2 ConvertedAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage)2 ConvertedBasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)2 MethodParameter (org.springframework.core.MethodParameter)2 Message (org.springframework.messaging.Message)2 MessageHandlingException (org.springframework.messaging.MessageHandlingException)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2