@Nullable protected final Message<?> doReceive(MessageChannel channel, long timeout) { Assert.notNull(channel, "MessageChannel is required"); Assert.state(channel instanceof PollableChannel, "A PollableChannel is required to receive messages"); Message<?> message = (timeout >= 0 ? ((PollableChannel) channel).receive(timeout) : ((PollableChannel) channel).receive()); if (message == null && logger.isTraceEnabled()) { logger.trace("Failed to receive message from channel '" + channel + "' within timeout: " + timeout); } return message; }
@Test public void testChannelInteceptorInnerBean() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("channelInterceptorParserTests.xml", getClass()); PollableChannel channel = (PollableChannel) context.getBean("channelWithInterceptorInnerBean"); channel.send(new GenericMessage<String>("test")); Message<?> transformed = channel.receive(1000); assertEquals("TEST", transformed.getPayload()); context.close(); }
@Test public void testSolicitResponse() { PollableChannel channel = (PollableChannel) context.getBean("replyChannel"); channel.send(new GenericMessage<String>("foo")); TestService service = (TestService) context.getBean("solicitResponse"); String result = service.solicitResponse(); assertEquals("foo", result); }
@Test public void testInterceptor() { int before = interceptor.getSendCount(); channelWithInterceptor.send(new GenericMessage<String>("test")); assertEquals(before + 1, interceptor.getSendCount()); }
/** * */ @SuppressWarnings("unchecked") @Override public <T> SpringResponse<T> receive(long timeout) { if (this.fromSpringChannel != null) { final Message<T> message = (Message<T>) this.fromSpringChannel.receive(timeout); if (message != null) { if (!(message.getPayload() instanceof byte[]) && !(message.getPayload() instanceof String)) { throw new IllegalStateException("Failed while receiving message from Spring due to the " + "payload type being other then byte[] or String which are the only types that are supported. Please " + "apply transformation/conversion on Spring side when sending message back to NiFi"); } return new SpringResponse<T>(message.getPayload(), message.getHeaders()); } } return null; }
@Test public void testBarrier() { Message<?> releasing = MessageBuilder.withPayload("bar").setHeader(BARRIER, "foo").build(); this.releaseChannel.send(releasing); Message<?> suspending = MessageBuilder.withPayload("foo").setHeader(BARRIER, "foo").build(); this.barrierFlowInput.send(suspending); Message<?> out = this.barrierResults.receive(10000); assertNotNull(out); assertEquals("bar", out.getPayload()); }
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class); MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class); PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class); inputChannel.send(new GenericMessage<String>("World")); logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload()); context.close(); }
@Before public void setUp() { Object message = ""; while (message!=null) { message = responseChannel.receive(10L); } }
@Before public void setUp() { Object message = ""; while (message!=null) { message = responseChannel.receive(10L); } }
@After public void tearDown() { while (replies.receive(10L) != null) { } }
@Test public void demoControlBus(){ ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext( "/META-INF/spring/integration/ControlBusDemo-context.xml"); MessageChannel controlChannel = ac.getBean("controlChannel", MessageChannel.class); PollableChannel adapterOutputChanel = ac.getBean("adapterOutputChanel", PollableChannel.class); logger.info("Received before adapter started: " + adapterOutputChanel.receive(1000)); controlChannel.send(new GenericMessage<String>("@inboundAdapter.start()")); logger.info("Received before adapter started: " + adapterOutputChanel.receive(1000)); controlChannel.send(new GenericMessage<String>("@inboundAdapter.stop()")); logger.info("Received after adapter stopped: " + adapterOutputChanel.receive(1000)); ac.close(); } }
@Test public void runDemo() throws Exception{ ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/spring/integration/FtpInboundChannelAdapterSample-context.xml"); PollableChannel ftpChannel = ctx.getBean("ftpChannel", PollableChannel.class); Message<?> message1 = ftpChannel.receive(10000); Message<?> message2 = ftpChannel.receive(10000); Message<?> message3 = ftpChannel.receive(1000); LOGGER.info(String.format("Received first file message: %s.", message1)); LOGGER.info(String.format("Received second file message: %s.", message2)); LOGGER.info(String.format("Received nothing else: %s.", message3)); assertNotNull(message1); assertNotNull(message2); assertNull("Was NOT expecting a third message.", message3); ctx.close(); }
@Test public void testConcurrentFileProcessing() throws Exception { logger.info("\n\n#### Starting Concurrent processing test #### "); logger.info("Populating directory with files"); for (int i = 0; i < fileCount; i++) { File file = new File("input/file_" + i + ".txt"); BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("hello " + i); out.close(); } logger.info("Populated directory with files"); Thread.sleep(2000); logger.info("Starting Spring Integration Sequential File processing"); ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/concurrentFileProcessing-config.xml"); PollableChannel filesOutChannel = ac.getBean("filesOutChannel", PollableChannel.class); for (int i = 0; i < fileCount; i++) { logger.info("Finished processing " + filesOutChannel.receive(10000).getPayload()); } ac.close(); } }
@Test public void testSequentialFileProcessing() throws Exception { logger.info("\n\n#### Starting Sequential processing test ####"); logger.info("Populating directory with files"); for (int i = 0; i < fileCount; i++) { File file = new File("input/file_" + i + ".txt"); BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("hello " + i); out.close(); } logger.info("Populated directory with files"); Thread.sleep(2000); logger.info("Starting Spring Integration Sequential File processing"); ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/spring/integration/sequentialFileProcessing-config.xml"); PollableChannel filesOutChannel = ac.getBean("filesOutChannel", PollableChannel.class); for (int i = 0; i < fileCount; i++) { logger.info("Finished processing " + filesOutChannel.receive(10000).getPayload()); } ac.stop(); } @Test
@SuppressWarnings("unchecked") @Test public void runDemo(){ ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("META-INF/spring/integration/FeedInboundChannelAdapterSample-context.xml"); PollableChannel feedChannel = ac.getBean("feedChannel", PollableChannel.class); for (int i = 0; i < 10; i++) { Message<SyndEntry> message = (Message<SyndEntry>) feedChannel.receive(1000); if (message != null){ SyndEntry entry = message.getPayload(); System.out.println(entry.getPublishedDate() + " - " + entry.getTitle()); } else { break; } } ac.close(); }
@SuppressWarnings("unchecked") @Test @Ignore //FIXME // This broke with Integration 2.0 in a milestone, so watch out when upgrading... public void testVanillaConversion() throws Exception { resources.send(new GenericMessage<>("classpath:*-context.xml")); Message<Resource> message = (Message<Resource>) requests.receive(200L); assertNotNull(message); message = (Message<Resource>) requests.receive(100L); assertNotNull(message); }
@Test public void testVanillaSendAndReceive() throws Exception { smokein.send(new GenericMessage<>("foo")); @SuppressWarnings("unchecked") Message<String> message = (Message<String>) smokeout.receive(100); String result = message == null ? null : message.getPayload(); assertEquals("foo: 1", result); assertEquals(1, AnnotatedEndpoint.count); }