public static void main (String[] args) throws Exception { @SuppressWarnings("resource") ApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/gmail-pop3-config.xml"); DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class); inputChannel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { logger.info("Message: " + message); } }); } }
public static void main (String[] args) throws Exception { @SuppressWarnings("resource") ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext( "/META-INF/spring/integration/gmail-imap-idle-config.xml"); DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class); inputChannel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { logger.info("Message: " + message); } }); } }
@Test public void testWithServiceToString() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelBaz", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Bar bar = ac.getBean(Bar.class); bar.toString(); verify(handler, times(0)).handleMessage(Mockito.any(Message.class)); ac.close(); }
@Test public void testWithServiceGetClass() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelBaz", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Bar bar = ac.getBean(Bar.class); bar.getClass(); verify(handler, times(0)).handleMessage(Mockito.any(Message.class)); ac.close(); }
@Test public void testWithServiceCastAsSuperclassUnAnnotatedMethod() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelBaz", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Foo foo = ac.getBean(Foo.class); foo.baz("hello"); verify(handler, times(1)).handleMessage(Mockito.any(Message.class)); ac.close(); }
@Test public void testWithServiceHashcode() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelBaz", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Bar bar = ac.getBean(Bar.class); assertEquals(bar.hashCode(), ac.getBean(Bar.class).hashCode()); verify(handler, times(0)).handleMessage(Mockito.any(Message.class)); ac.close(); }
@Test public void testWithServiceCastAsSuperclassAnnotatedMethod() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelFoo", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Foo foo = ac.getBean(Foo.class); foo.foo("hello"); verify(handler, times(1)).handleMessage(Mockito.any(Message.class)); ac.close(); }
@Test public void testWithServiceAnnotatedMethod() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelBar", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Bar bar = ac.getBean(Bar.class); bar.bar("hello"); verify(handler, times(1)).handleMessage(Mockito.any(Message.class)); ac.close(); }
@Test public void testSendInSeparateThread() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); final DirectChannel channel = new DirectChannel(); ThreadNameExtractingTestTarget target = new ThreadNameExtractingTestTarget(latch); channel.subscribe(target); final GenericMessage<String> message = new GenericMessage<String>("test"); new Thread((Runnable) () -> channel.send(message), "test-thread").start(); latch.await(1000, TimeUnit.MILLISECONDS); assertEquals("test-thread", target.threadName); }
@Test public void testAsyncWithDirectReply() { DirectChannel replyChannel = new DirectChannel(); final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>(); replyChannel.subscribe(reply::set); Message<?> message = MessageBuilder.withPayload("testing").setReplyChannel(replyChannel).build(); this.asyncIn.send(message); assertNull(reply.get()); this.asyncService.future.set(this.asyncService.payload.toUpperCase()); assertNotNull(reply.get()); assertEquals("TESTING", reply.get().getPayload()); }
@Test public void testProxyDirect() { assertTrue(AopUtils.isCglibProxy(this.directChannel)); final AtomicReference<Message<?>> message = new AtomicReference<>(); this.directChannel.subscribe(m -> message.set(m)); this.directChannel.send(new GenericMessage<>("foo")); assertNotNull(message.get()); }
@Test public void asyncConvertSendAndReceiveWithExplicitChannel() throws Exception { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); long start = System.currentTimeMillis(); Future<String> result = template.asyncConvertSendAndReceive(channel, "test"); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST", result.get()); }
@Before public void setup() { input.setBeanName("input"); output.setBeanName("output"); taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.afterPropertiesSet(); delayHandler = new DelayHandler(DELAYER_MESSAGE_GROUP_ID, taskScheduler); delayHandler.setOutputChannel(output); delayHandler.setBeanFactory(mock(BeanFactory.class)); input.subscribe(delayHandler); output.subscribe(resultHandler); }
@Test public void asyncSendAndReceiveWithExplicitChannel() throws Exception { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); long start = System.currentTimeMillis(); Future<Message<?>> result = template.asyncSendAndReceive(channel, MessageBuilder.withPayload("test").build()); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST", result.get().getPayload()); }
@Before public void setUp() { queryListenerContainer = mock(ContinuousQueryListenerContainer.class); cqMessageProducer = new ContinuousQueryMessageProducer(queryListenerContainer, "foo"); DirectChannel outputChannel = new DirectChannel(); cqMessageProducer.setOutputChannel(outputChannel); cqMessageProducer.setBeanFactory(mock(BeanFactory.class)); handler = new CqMessageHandler(); outputChannel.subscribe(handler); }
@Test public void asyncConvertSendAndReceiveWithExplicitChannelAndMessagePostProcessor() throws Exception { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); long start = System.currentTimeMillis(); Future<String> result = template.asyncConvertSendAndReceive(channel, "test", new TestMessagePostProcessor()); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST-bar", result.get()); }
@Test public void asyncConvertSendAndReceiveWithDefaultChannelAndMessagePostProcessor() throws Exception { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setDefaultDestination(channel); long start = System.currentTimeMillis(); Future<String> result = template.asyncConvertSendAndReceive(123, new TestMessagePostProcessor()); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("123-bar", result.get()); }
@Test(expected = MessageDeliveryException.class) public void handlerThrowsExceptionWithNoDelay() { startDelayerHandler(); output.unsubscribe(resultHandler); output.subscribe(message -> { throw new UnsupportedOperationException("intentional test failure"); }); Message<?> message = MessageBuilder.withPayload("test").build(); input.send(message); }
@Test public void asyncSendAndReceiveWithDefaultChannel() throws Exception { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setDefaultDestination(channel); long start = System.currentTimeMillis(); Future<Message<?>> result = template.asyncSendAndReceive(MessageBuilder.withPayload("test").build()); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); }
@Test(expected = TimeoutException.class) public void timeoutException() throws Exception { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(10000)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setDefaultDestination(channel); Future<Message<?>> result = template.asyncSendAndReceive(MessageBuilder.withPayload("test").build()); result.get(10, TimeUnit.MILLISECONDS); }