@Test(expected = CancellationException.class) public void cancellationException() throws Throwable { DirectChannel channel = new DirectChannel(); EchoHandler handler = new EchoHandler(10000); channel.subscribe(handler); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setDefaultDestination(channel); Future<Message<?>> result = template.asyncSendAndReceive(MessageBuilder.withPayload("test").build()); try { Thread.sleep(200); result.cancel(true); result.get(); fail(); } catch (ExecutionException e) { Assert.isTrue(handler.interrupted, "handler should have been interrupted"); throw e.getCause(); } }
@Test public void asyncConvertSendAndReceiveWithResolvedChannel() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); context.registerSingleton("testChannel", DirectChannel.class); context.refresh(); DirectChannel channel = context.getBean("testChannel", DirectChannel.class); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setBeanFactory(context); long start = System.currentTimeMillis(); Future<String> result = template.asyncConvertSendAndReceive("testChannel", "test"); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST", result.get()); }
@Test public void asyncSendAndReceiveWithResolvedChannel() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); context.registerSingleton("testChannel", DirectChannel.class); context.refresh(); DirectChannel channel = context.getBean("testChannel", DirectChannel.class); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setBeanFactory(context); long start = System.currentTimeMillis(); Future<Message<?>> result = template.asyncSendAndReceive("testChannel", MessageBuilder.withPayload("test").build()); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST", result.get().getPayload()); }
@Test(expected = MessagingException.class) public void executionException() throws Throwable { DirectChannel channel = new DirectChannel(); channel.subscribe(new EchoHandler(-1)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setDefaultDestination(channel); Future<Message<?>> result = template.asyncSendAndReceive(MessageBuilder.withPayload("test").build()); try { result.get(10, TimeUnit.SECONDS); fail(); } catch (ExecutionException e) { throw e.getCause(); } }
@Test public void asyncConvertSendAndReceiveWithResolvedChannelAndMessagePostProcessor() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); context.registerSingleton("testChannel", DirectChannel.class); context.refresh(); DirectChannel channel = context.getBean("testChannel", DirectChannel.class); channel.subscribe(new EchoHandler(200)); AsyncMessagingTemplate template = new AsyncMessagingTemplate(); template.setBeanFactory(context); long start = System.currentTimeMillis(); Future<String> result = template.asyncConvertSendAndReceive("testChannel", "test", new TestMessagePostProcessor()); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST-bar", result.get()); }
@Test public void asyncConvertSendAndReceiveWithDefaultChannel() 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("test"); assertNotNull(result.get()); long elapsed = System.currentTimeMillis() - start; assertTrue(elapsed >= 200 - safety); assertEquals("TEST", result.get()); }
@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 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()); }
@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); }
@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()); }