Search in sources :

Example 16 with ChannelInterceptorAdapter

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

the class ChannelInterceptorTests method testPostSendInterceptorWithUnsentMessage.

@Test
public void testPostSendInterceptorWithUnsentMessage() {
    final AtomicInteger invokedCounter = new AtomicInteger(0);
    final AtomicInteger sentCounter = new AtomicInteger(0);
    final QueueChannel singleItemChannel = new QueueChannel(1);
    singleItemChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
            assertNotNull(message);
            assertNotNull(channel);
            assertSame(singleItemChannel, channel);
            if (sent) {
                sentCounter.incrementAndGet();
            }
            invokedCounter.incrementAndGet();
        }
    });
    assertEquals(0, invokedCounter.get());
    assertEquals(0, sentCounter.get());
    singleItemChannel.send(new GenericMessage<String>("test1"));
    assertEquals(1, invokedCounter.get());
    assertEquals(1, sentCounter.get());
    singleItemChannel.send(new GenericMessage<String>("test2"), 0);
    assertEquals(2, invokedCounter.get());
    assertEquals(1, sentCounter.get());
    assertNotNull(singleItemChannel.removeInterceptor(0));
    singleItemChannel.send(new GenericMessage<String>("test2"), 0);
    assertEquals(2, invokedCounter.get());
    assertEquals(1, sentCounter.get());
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) QueueChannel(org.springframework.integration.channel.QueueChannel) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) 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