@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; }
/** * */ @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; }
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 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); }
@Test @DirtiesContext @SuppressWarnings("unchecked") public void testWrongPayload() { final Message<String> stringMessage = MessageBuilder.withPayload("just text").build(); try { requestChannel.send(stringMessage); fail(); } catch (MessageHandlingException e) { String message = e.getCause().getMessage(); assertTrue("Wrong message: " + message, message.contains("The payload must be of type JobLaunchRequest.")); } Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNull("JobExecution message received when no return address set", executionMessage); }
@SuppressWarnings("unchecked") @Test @DirtiesContext public void testReply() { JobParametersBuilder builder = new JobParametersBuilder(); builder.addString("dontclash", "12"); Map<String, Object> map = new HashMap<>(); map.put(MessageHeaders.REPLY_CHANNEL, "response"); MessageHeaders headers = new MessageHeaders(map); GenericMessage<JobLaunchRequest> trigger = new GenericMessage<>(new JobLaunchRequest(job, builder.toJobParameters()), headers); requestChannel.send(trigger); Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNotNull("No response received", executionMessage); JobExecution execution = executionMessage.getPayload(); assertNotNull("JobExecution not returned", execution); }
@SuppressWarnings("unchecked") @Test @DirtiesContext public void testReply() { JobParametersBuilder builder = new JobParametersBuilder(); builder.addString("dontclash", "12"); Map<String, Object> map = new HashMap<>(); map.put(MessageHeaders.REPLY_CHANNEL, "response"); MessageHeaders headers = new MessageHeaders(map); GenericMessage<JobLaunchRequest> trigger = new GenericMessage<>(new JobLaunchRequest(job, builder.toJobParameters()), headers); requestChannel.send(trigger); Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNotNull("No response received", executionMessage); JobExecution execution = executionMessage.getPayload(); assertNotNull("JobExecution not returned", execution); }
@Test @DirtiesContext @SuppressWarnings("unchecked") public void testNoReply() { GenericMessage<JobLaunchRequest> trigger = new GenericMessage<>(new JobLaunchRequest(job, new JobParameters())); try { requestChannel.send(trigger); } catch (MessagingException e) { String message = e.getMessage(); assertTrue("Wrong message: " + message, message.contains("replyChannel")); } Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNull("JobExecution message received when no return address set", executionMessage); }
@Test @DirtiesContext @SuppressWarnings("unchecked") public void testExceptionRaised() throws InterruptedException { this.tasklet.setFail(true); JobParametersBuilder builder = new JobParametersBuilder(); builder.addString("dontclash", "12"); Map<String, Object> map = new HashMap<>(); map.put(MessageHeaders.REPLY_CHANNEL, "response"); MessageHeaders headers = new MessageHeaders(map); GenericMessage<JobLaunchRequest> trigger = new GenericMessage<>(new JobLaunchRequest(testJob, builder.toJobParameters()), headers); requestChannel.send(trigger); Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNotNull("No response received", executionMessage); JobExecution execution = executionMessage.getPayload(); assertNotNull("JobExecution not returned", execution); assertEquals(ExitStatus.FAILED.getExitCode(), execution.getExitStatus().getExitCode()); } }
@Test @DirtiesContext @SuppressWarnings("unchecked") public void testNoReply() { GenericMessage<JobLaunchRequest> trigger = new GenericMessage<>(new JobLaunchRequest(job, new JobParameters())); try { requestChannel.send(trigger); fail(); } catch (MessagingException e) { String message = e.getMessage(); assertTrue("Wrong message: " + message, message.contains("replyChannel")); } Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000); assertNull("JobExecution message received when no return address set", executionMessage); }