Search in sources :

Example 1 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-integration by spring-projects.

the class GatewayParserTests method testAsyncCompletableNoAsyncMessage.

@Test
public void testAsyncCompletableNoAsyncMessage() 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);
    CompletableFuture<Message<?>> result = service.completableReturnsMessage("flowCompletableM");
    Message<?> reply = result.get(10, TimeUnit.SECONDS);
    assertEquals("flowCompletableM", reply.getPayload());
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}
Also used : 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 ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter 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 3 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-integration by spring-projects.

the class GatewayParserTests method testAsyncCompletableNoAsync.

@Test
public void testAsyncCompletableNoAsync() 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);
    CompletableFuture<String> result = service.completable("flowCompletable");
    String reply = result.get(10, TimeUnit.SECONDS);
    assertEquals("SYNC_COMPLETABLE", reply);
    assertEquals(Thread.currentThread(), thread.get());
    assertNull(TestUtils.getPropertyValue(context.getBean("&completableNoAsync"), "asyncExecutor"));
}
Also used : 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 4 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-integration by spring-projects.

the class ChannelInterceptorTests method testPostSendInterceptorWithSentMessage.

@Test
public void testPostSendInterceptorWithSentMessage() {
    final AtomicBoolean invoked = new AtomicBoolean(false);
    channel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
            assertNotNull(message);
            assertNotNull(channel);
            assertSame(ChannelInterceptorTests.this.channel, channel);
            assertTrue(sent);
            invoked.set(true);
        }
    });
    channel.send(new GenericMessage<String>("test"));
    assertTrue(invoked.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 5 with ChannelInterceptorAdapter

use of org.springframework.messaging.support.ChannelInterceptorAdapter in project spring-integration by spring-projects.

the class ChannelInterceptorTests method testPostReceiveInterceptor.

@Test
public void testPostReceiveInterceptor() {
    final AtomicInteger invokedCount = new AtomicInteger();
    final AtomicInteger messageCount = new AtomicInteger();
    channel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> postReceive(Message<?> message, MessageChannel channel) {
            assertNotNull(channel);
            assertSame(ChannelInterceptorTests.this.channel, channel);
            if (message != null) {
                messageCount.incrementAndGet();
            }
            invokedCount.incrementAndGet();
            return message;
        }
    });
    channel.receive(0);
    assertEquals(1, invokedCount.get());
    assertEquals(0, messageCount.get());
    channel.send(new GenericMessage<String>("test"));
    Message<?> result = channel.receive(0);
    assertNotNull(result);
    assertEquals(2, invokedCount.get());
    assertEquals(1, messageCount.get());
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

MessageChannel (org.springframework.messaging.MessageChannel)16 ChannelInterceptorAdapter (org.springframework.messaging.support.ChannelInterceptorAdapter)16 Test (org.junit.Test)15 Message (org.springframework.messaging.Message)13 GenericMessage (org.springframework.messaging.support.GenericMessage)12 QueueChannel (org.springframework.integration.channel.QueueChannel)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestService (org.springframework.integration.gateway.TestService)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)4 MessageConverterConfigurer (org.springframework.cloud.stream.binding.MessageConverterConfigurer)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)3 DirectChannel (org.springframework.integration.channel.DirectChannel)2 MyCompletableFuture (org.springframework.integration.gateway.TestService.MyCompletableFuture)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 ParseException (java.text.ParseException)1 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Log (org.apache.commons.logging.Log)1