protected void addThreadEnricher(QueueChannel requestChannel) { requestChannel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { return MessageBuilder.fromMessage(message) .setHeader("thread", Thread.currentThread()) .build(); } }); }
@Bean public PollableChannel output() { QueueChannel queueChannel = new QueueChannel(); queueChannel.addInterceptor(wireTapFromOutputInterceptor()); return queueChannel; }
@Test public void testPostSendInterceptorWithSentMessage() { final AtomicBoolean invoked = new AtomicBoolean(false); channel.addInterceptor(new ChannelInterceptor() { @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()); }
@Test public void testAsyncCompletable() throws Exception { QueueChannel requestChannel = (QueueChannel) context.getBean("requestChannel"); final AtomicReference<Thread> thread = new AtomicReference<>(); requestChannel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { thread.set(Thread.currentThread()); return message; } }); 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")); }
@Test(expected = MessageDeliveryException.class) public void testSingleSelectorRejects() { final AtomicInteger counter = new AtomicInteger(); MessageSelector selector = new TestMessageSelector(false, counter); MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector); QueueChannel channel = new QueueChannel(); channel.addInterceptor(interceptor); channel.send(new GenericMessage<>("test1")); }
@Test public void afterCompletionWithReceiveException() { PreReceiveReturnsTrueInterceptor interceptor1 = new PreReceiveReturnsTrueInterceptor(); PreReceiveReturnsTrueInterceptor interceptor2 = new PreReceiveReturnsTrueInterceptor(); interceptor2.setExceptionToRaise(new RuntimeException("Simulated exception")); channel.addInterceptor(interceptor1); channel.addInterceptor(interceptor2); try { channel.receive(0); } catch (Exception ex) { assertEquals("Simulated exception", ex.getMessage()); } assertTrue(interceptor1.wasAfterCompletionInvoked()); assertFalse(interceptor2.wasAfterCompletionInvoked()); }
@Test public void afterCompletionWithPreSendException() { AfterCompletionTestInterceptor interceptor1 = new AfterCompletionTestInterceptor(); AfterCompletionTestInterceptor interceptor2 = new AfterCompletionTestInterceptor(); interceptor2.setExceptionToRaise(new RuntimeException("Simulated exception")); this.channel.addInterceptor(interceptor1); this.channel.addInterceptor(interceptor2); try { this.channel.send(MessageBuilder.withPayload("test").build()); } catch (Exception ex) { assertEquals("Simulated exception", ex.getCause().getMessage()); } assertTrue(interceptor1.wasAfterCompletionInvoked()); assertFalse(interceptor2.wasAfterCompletionInvoked()); }
@Test public void testPostReceiveInterceptor() { final AtomicInteger messageCount = new AtomicInteger(); channel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> postReceive(Message<?> message, MessageChannel channel) { assertNotNull(channel); assertSame(ChannelInterceptorTests.this.channel, channel); messageCount.incrementAndGet(); return message; } }); channel.receive(0); assertEquals(0, messageCount.get()); channel.send(new GenericMessage<String>("test")); Message<?> result = channel.receive(0); assertNotNull(result); assertEquals(1, messageCount.get()); }
@Test public void testPreSendInterceptorReturnsMessage() { PreSendReturnsMessageInterceptor interceptor = new PreSendReturnsMessageInterceptor(); channel.addInterceptor(interceptor); channel.send(new GenericMessage<String>("test")); Message<?> result = channel.receive(0); assertNotNull(result); assertEquals("test", result.getPayload()); assertEquals(1, result.getHeaders().get(PreSendReturnsMessageInterceptor.class.getSimpleName())); assertTrue(interceptor.wasAfterCompletionInvoked()); }
@Test public void testPreReceiveInterceptorReturnsFalse() { channel.addInterceptor(new PreReceiveReturnsFalseInterceptor()); Message<?> message = new GenericMessage<String>("test"); channel.send(message); Message<?> result = channel.receive(0); assertEquals(1, PreReceiveReturnsFalseInterceptor.counter.get()); assertNull(result); }
@Test public void testSingleSelectorAccepts() { final AtomicInteger counter = new AtomicInteger(); MessageSelector selector = new TestMessageSelector(true, counter); MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector); QueueChannel channel = new QueueChannel(); channel.addInterceptor(interceptor); assertTrue(channel.send(new GenericMessage<>("test1"))); }
@Test public void testMultipleSelectorsAccept() { final AtomicInteger counter = new AtomicInteger(); MessageSelector selector1 = new TestMessageSelector(true, counter); MessageSelector selector2 = new TestMessageSelector(true, counter); MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2); QueueChannel channel = new QueueChannel(); channel.addInterceptor(interceptor); assertTrue(channel.send(new GenericMessage<>("test1"))); assertEquals(2, counter.get()); }
@Test public void wireTapDoesNotInterceptItsOwnChannel() { QueueChannel wireTapChannel = new QueueChannel(); wireTapChannel.addInterceptor(new WireTap(wireTapChannel)); // would throw a StackOverflowException if not working: wireTapChannel.send(MessageBuilder.withPayload("test").build()); }
@Test public void testPreReceiveInterceptorReturnsTrue() { PreReceiveReturnsTrueInterceptor interceptor = new PreReceiveReturnsTrueInterceptor(); channel.addInterceptor(interceptor); Message<?> message = new GenericMessage<String>("test"); channel.send(message); Message<?> result = channel.receive(0); assertEquals(1, interceptor.getCounter().get()); assertNotNull(result); assertTrue(interceptor.wasAfterCompletionInvoked()); }
@Test public void interceptedMessageContainsHeaderValue() { QueueChannel mainChannel = new QueueChannel(); QueueChannel secondaryChannel = new QueueChannel(); mainChannel.addInterceptor(new WireTap(secondaryChannel)); String headerName = "testAttribute"; Message<String> message = MessageBuilder.withPayload("testing") .setHeader(headerName, 123).build(); mainChannel.send(message); Message<?> original = mainChannel.receive(0); Message<?> intercepted = secondaryChannel.receive(0); Object originalAttribute = original.getHeaders().get(headerName); Object interceptedAttribute = intercepted.getHeaders().get(headerName); assertNotNull(originalAttribute); assertNotNull(interceptedAttribute); assertEquals(originalAttribute, interceptedAttribute); }
@Test public void wireTapWithNoSelector() { QueueChannel mainChannel = new QueueChannel(); QueueChannel secondaryChannel = new QueueChannel(); mainChannel.addInterceptor(new WireTap(secondaryChannel)); mainChannel.send(new GenericMessage<>("testing")); Message<?> original = mainChannel.receive(0); assertNotNull(original); Message<?> intercepted = secondaryChannel.receive(0); assertNotNull(intercepted); assertEquals(original, intercepted); }
@Test public void testPreSendInterceptorReturnsNull() { PreSendReturnsNullInterceptor interceptor = new PreSendReturnsNullInterceptor(); channel.addInterceptor(interceptor); Message<?> message = new GenericMessage<String>("test"); channel.send(message); assertEquals(1, interceptor.getCount()); assertTrue(channel.removeInterceptor(interceptor)); channel.send(new GenericMessage<String>("TEST")); assertEquals(1, interceptor.getCount()); Message<?> result = channel.receive(0); assertNotNull(result); assertEquals("TEST", result.getPayload()); }
@Test public void wireTapWithRejectingSelector() { QueueChannel mainChannel = new QueueChannel(); QueueChannel secondaryChannel = new QueueChannel(); mainChannel.addInterceptor(new WireTap(secondaryChannel, new TestSelector(false))); mainChannel.send(new GenericMessage<>("testing")); Message<?> original = mainChannel.receive(0); assertNotNull(original); Message<?> intercepted = secondaryChannel.receive(0); assertNull(intercepted); }
@Test public void wireTapWithAcceptingSelector() { QueueChannel mainChannel = new QueueChannel(); QueueChannel secondaryChannel = new QueueChannel(); mainChannel.addInterceptor(new WireTap(secondaryChannel, new TestSelector(true))); mainChannel.send(new GenericMessage<>("testing")); Message<?> original = mainChannel.receive(0); assertNotNull(original); Message<?> intercepted = secondaryChannel.receive(0); assertNotNull(intercepted); assertEquals(original, intercepted); }
@Test public void simpleTargetWireTap() { QueueChannel mainChannel = new QueueChannel(); QueueChannel secondaryChannel = new QueueChannel(); mainChannel.addInterceptor(new WireTap(secondaryChannel)); assertNull(secondaryChannel.receive(0)); Message<?> message = new GenericMessage<>("testing"); mainChannel.send(message); Message<?> original = mainChannel.receive(0); Message<?> intercepted = secondaryChannel.receive(0); assertNotNull(original); assertNotNull(intercepted); assertEquals(original, intercepted); }