Example usage for org.springframework.integration.channel QueueChannel addInterceptor

List of usage examples for org.springframework.integration.channel QueueChannel addInterceptor

Introduction

In this page you can find the example usage for org.springframework.integration.channel QueueChannel addInterceptor.

Prototype

@Override
    public void addInterceptor(ChannelInterceptor interceptor) 

Source Link

Usage

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

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

        @Override/*from  w ww  . j  ava  2  s.com*/
        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("asyncCompletable", TestService.class);
    CompletableFuture<String> result = service.completable("foo").thenApply(String::toUpperCase);
    String reply = result.get(10, TimeUnit.SECONDS);
    assertEquals("FOO", reply);
    assertThat(thread.get().getName(), startsWith("testExec-"));
    assertNotNull(TestUtils.getPropertyValue(context.getBean("&asyncCompletable"), "asyncExecutor"));
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

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

        @Override//from  w ww .  j  a v  a  2  s . c om
        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);
    CompletableFuture<String> result = service.completable("flowCompletable");
    String reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("SYNC_COMPLETABLE", reply);
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

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

        @Override/*from w  w w.j a  v  a 2  s  .  c  om*/
        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(1, TimeUnit.SECONDS);
    assertEquals("SYNC_CUSTOM_COMPLETABLE", reply);
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

@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/*from w w  w.j  a v  a2  s  .  com*/
        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(1, 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");
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

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

        @Override// w  ww . j a  va2  s  . c om
        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("asyncCompletable", TestService.class);
    CompletableFuture<Message<?>> result = service.completableReturnsMessage("foo");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("foo", reply.getPayload());
    assertThat(thread.get().getName(), startsWith("testExec-"));
    assertNotNull(TestUtils.getPropertyValue(context.getBean("&asyncCompletable"), "asyncExecutor"));
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

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

        @Override//from  w  w  w  . j a  v a2  s. c  o  m
        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);
    CompletableFuture<Message<?>> result = service.completableReturnsMessage("flowCompletableM");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("flowCompletableM", reply.getPayload());
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}

From source file:org.springframework.integration.config.xml.GatewayParserTests.java

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

        @Override/*from   w  ww. j a  va  2 s  .c o m*/
        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);
    MyCompletableMessageFuture result = service.customCompletableReturnsMessage("flowCustomCompletableM");
    Message<?> reply = result.get(1, TimeUnit.SECONDS);
    assertEquals("flowCustomCompletableM", reply.getPayload());
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}