@Bean public DirectChannel requests() { return new DirectChannel(); }
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); } }); } }
@Test public void testWebSockets() throws InterruptedException { System.setProperty("local.server.port", this.port); ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("client-context.xml", org.springframework.integration.samples.websocket.standard.client.Application.class); DirectChannel webSocketInputChannel = ctx.getBean("webSocketInputChannel", DirectChannel.class); final CountDownLatch stopLatch = new CountDownLatch(2); webSocketInputChannel.addInterceptor(new ChannelInterceptor() { @Override public void postSend(Message<?> message, MessageChannel channel, boolean sent) { Object payload = message.getPayload(); assertThat(payload, instanceOf(String.class)); Date date = null; try { date = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.DEFAULT).parse((String) payload); } catch (ParseException e) { fail("fail to parse date"); } assertThat(new Date().compareTo(date), greaterThanOrEqualTo(0)); stopLatch.countDown(); } }); assertTrue(stopLatch.await(10, TimeUnit.SECONDS)); ctx.close(); }
@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 testDirectChannelLoggingWithMoreThenOneSubscriber() { final DirectChannel channel = new DirectChannel(); channel.setBeanName("directChannel"); verifySubscriptions(channel); }
@Test public void filterAcceptsWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> true); filter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertEquals(message.getPayload(), reply.getPayload()); }
@Bean public MessageChannel jmsInboundGatewayInputChannel() { DirectChannel directChannel = new DirectChannel(); directChannel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { jmsInboundGatewayChannelCalled().set(true); return message; } }); return directChannel; }
@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 withNoContext() { DirectChannel channel = new DirectChannel(); channel.setBeanName("bar"); try { channel.send(new GenericMessage<String>("Hello, world!")); fail("Exception expected"); } catch (MessagingException e) { assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for channel 'bar'.")); } }
@Bean public MessageChannel loggingOffChannel() { DirectChannel directChannel = new DirectChannel(); directChannel.setLoggingEnabled(false); return directChannel; } }
@Test public void testGetWithRemove() { String dir = "ftpSource/"; this.getGw.setOption(Option.DELETE); this.inboundGet.send(new GenericMessage<Object>(dir + "ftpSource2.txt")); Message<?> result = this.output.receive(1000); assertNotNull(result); File localFile = (File) result.getPayload(); assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), containsString(dir.toUpperCase())); assertThat(new File(getSourceRemoteDirectory(), "ftpSource2.txt").exists(), equalTo(false)); }
@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(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 testFileReaderClosedOnException() throws Exception { DirectChannel outputChannel = new DirectChannel(); outputChannel.subscribe(message -> { throw new RuntimeException(); }); FileSplitter splitter = new FileSplitter(true, true); splitter.setOutputChannel(outputChannel); FileReader fileReader = Mockito.spy(new FileReader(file)); try { splitter.handleMessage(new GenericMessage<Reader>(fileReader)); } catch (RuntimeException e) { // ignore } Mockito.verify(fileReader).close(); }
@Test @Transactional public void testWithMessageHistory() throws Exception { Message<?> message = new GenericMessage<String>("Hello"); DirectChannel fooChannel = new DirectChannel(); fooChannel.setBeanName("fooChannel"); DirectChannel barChannel = new DirectChannel(); barChannel.setBeanName("barChannel"); message = MessageHistory.write(message, fooChannel); message = MessageHistory.write(message, barChannel); messageStore.addMessage(message); message = messageStore.getMessage(message.getHeaders().getId()); MessageHistory messageHistory = MessageHistory.read(message); assertNotNull(messageHistory); assertEquals(2, messageHistory.size()); Properties fooChannelHistory = messageHistory.get(0); assertEquals("fooChannel", fooChannelHistory.get("name")); assertEquals("channel", fooChannelHistory.get("type")); }
@Test(expected = MessageRejectedException.class) public void filterThrowsExceptionWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); filter.setThrowExceptionOnRejection(true); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); }
@Bean public MessageChannel jmsMessageDrivenInputChannel() { DirectChannel directChannel = new DirectChannel(); directChannel.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { jmsMessageDrivenChannelCalled().set(true); return message; } }); return directChannel; }
@Test(expected = ReplyRequiredException.class) public void splitterParserTestWithRequiresReply() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "splitterParserTests.xml", this.getClass()); context.start(); DirectChannel inputChannel = context.getBean("requiresReplyInput", DirectChannel.class); inputChannel.send(MessageBuilder.withPayload(Collections.emptyList()).build()); context.close(); }
@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()); }