Search in sources :

Example 1 with MyCompletableFuture

use of org.springframework.integration.gateway.TestService.MyCompletableFuture in project spring-integration by spring-projects.

the class GatewayParserTests method testCustomCompletableNoAsync.

@Test
public void testCustomCompletableNoAsync() throws Exception {
    QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel");
    final AtomicReference<Thread> thread = new AtomicReference<>();
    requestChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            thread.set(Thread.currentThread());
            return super.preSend(message, channel);
        }
    });
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("completableNoAsync", TestService.class);
    MyCompletableFuture result = service.customCompletable("flowCustomCompletable");
    String reply = result.get(10, TimeUnit.SECONDS);
    assertEquals("SYNC_CUSTOM_COMPLETABLE", reply);
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}
Also used : MyCompletableFuture(org.springframework.integration.gateway.TestService.MyCompletableFuture) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) TestService(org.springframework.integration.gateway.TestService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with MyCompletableFuture

use of org.springframework.integration.gateway.TestService.MyCompletableFuture in project spring-integration by spring-projects.

the class GatewayParserTests method startResponder.

private void startResponder(final PollableChannel requestChannel, final MessageChannel replyChannel) {
    Executors.newSingleThreadExecutor().execute(() -> {
        Message<?> request = requestChannel.receive(60000);
        assertNotNull("Request not received", request);
        Message<?> reply = MessageBuilder.fromMessage(request).setCorrelationId(request.getHeaders().getId()).build();
        Object payload = null;
        if (request.getPayload().equals("futureSync")) {
            payload = new AsyncResult<Message<?>>(reply);
        } else if (request.getPayload().equals("flowCompletable")) {
            payload = CompletableFuture.<String>completedFuture("SYNC_COMPLETABLE");
        } else if (request.getPayload().equals("flowCustomCompletable")) {
            MyCompletableFuture myCompletableFuture1 = new MyCompletableFuture();
            myCompletableFuture1.complete("SYNC_CUSTOM_COMPLETABLE");
            payload = myCompletableFuture1;
        } else if (request.getPayload().equals("flowCompletableM")) {
            payload = CompletableFuture.<Message<?>>completedFuture(reply);
        } else if (request.getPayload().equals("flowCustomCompletableM")) {
            MyCompletableMessageFuture myCompletableFuture2 = new MyCompletableMessageFuture();
            myCompletableFuture2.complete(reply);
            payload = myCompletableFuture2;
        }
        if (payload != null) {
            reply = MessageBuilder.withPayload(payload).copyHeaders(reply.getHeaders()).build();
        }
        replyChannel.send(reply);
    });
}
Also used : MyCompletableFuture(org.springframework.integration.gateway.TestService.MyCompletableFuture) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MyCompletableMessageFuture(org.springframework.integration.gateway.TestService.MyCompletableMessageFuture)

Example 3 with MyCompletableFuture

use of org.springframework.integration.gateway.TestService.MyCompletableFuture in project spring-integration by spring-projects.

the class GatewayParserTests method testCustomCompletableNoAsyncAttemptAsync.

@Test
public void testCustomCompletableNoAsyncAttemptAsync() throws Exception {
    Object gateway = context.getBean("&customCompletableAttemptAsync");
    Log logger = spy(TestUtils.getPropertyValue(gateway, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    new DirectFieldAccessor(gateway).setPropertyValue("logger", logger);
    QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel");
    final AtomicReference<Thread> thread = new AtomicReference<>();
    requestChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            thread.set(Thread.currentThread());
            return super.preSend(message, channel);
        }
    });
    MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
    this.startResponder(requestChannel, replyChannel);
    TestService service = context.getBean("customCompletableAttemptAsync", TestService.class);
    MyCompletableFuture result = service.customCompletable("flowCustomCompletable");
    String reply = result.get(10, TimeUnit.SECONDS);
    assertEquals("SYNC_CUSTOM_COMPLETABLE", reply);
    assertEquals(Thread.currentThread(), thread.get());
    assertNotNull(TestUtils.getPropertyValue(gateway, "asyncExecutor"));
    verify(logger).debug("AsyncTaskExecutor submit*() return types are incompatible with the method return type; " + "running on calling thread; the downstream flow must return the required Future: " + "MyCompletableFuture");
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Log(org.apache.commons.logging.Log) TestService(org.springframework.integration.gateway.TestService) AtomicReference(java.util.concurrent.atomic.AtomicReference) MyCompletableFuture(org.springframework.integration.gateway.TestService.MyCompletableFuture) MessageChannel(org.springframework.messaging.MessageChannel) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Aggregations

MyCompletableFuture (org.springframework.integration.gateway.TestService.MyCompletableFuture)3 Message (org.springframework.messaging.Message)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 TestService (org.springframework.integration.gateway.TestService)2 MessageChannel (org.springframework.messaging.MessageChannel)2 ChannelInterceptorAdapter (org.springframework.messaging.support.ChannelInterceptorAdapter)2 Log (org.apache.commons.logging.Log)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MyCompletableMessageFuture (org.springframework.integration.gateway.TestService.MyCompletableMessageFuture)1