List of usage examples for org.springframework.integration.channel QueueChannel receive
@Override @Nullable public Message<?> receive(long timeout)
From source file:org.springframework.integration.ftp.dsl.FtpTests.java
@Test public void testFtpInboundFlow() { QueueChannel out = new QueueChannel(); IntegrationFlow flow = IntegrationFlows.from( Ftp.inboundAdapter(sessionFactory()).preserveTimestamp(true).remoteDirectory("ftpSource") .regexFilter(".*\\.txt$").localFilename(f -> f.toUpperCase() + ".a") .localDirectory(getTargetLocalDirectory()), e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get(); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); Message<?> message = out.receive(10_000); assertNotNull(message);// w w w. jav a 2 s .co m Object payload = message.getPayload(); assertThat(payload, instanceOf(File.class)); File file = (File) payload; assertThat(file.getName(), isOneOf(" FTPSOURCE1.TXT.a", "FTPSOURCE2.TXT.a")); assertThat(file.getAbsolutePath(), containsString("localTarget")); message = out.receive(10_000); assertNotNull(message); file = (File) message.getPayload(); assertThat(file.getName(), isOneOf(" FTPSOURCE1.TXT.a", "FTPSOURCE2.TXT.a")); assertThat(file.getAbsolutePath(), containsString("localTarget")); assertNull(out.receive(10)); File remoteFile = new File(this.sourceRemoteDirectory, " " + prefix() + "Source1.txt"); remoteFile.setLastModified(System.currentTimeMillis() - 1000 * 60 * 60 * 24); message = out.receive(10_000); assertNotNull(message); payload = message.getPayload(); assertThat(payload, instanceOf(File.class)); file = (File) payload; assertEquals(" FTPSOURCE1.TXT.a", file.getName()); registration.destroy(); }
From source file:org.springframework.integration.ftp.dsl.FtpTests.java
@Test public void testFtpInboundStreamFlow() throws Exception { QueueChannel out = new QueueChannel(); StandardIntegrationFlow flow = IntegrationFlows.from( Ftp.inboundStreamingAdapter(new FtpRemoteFileTemplate(sessionFactory())) .remoteDirectory("ftpSource").regexFilter(".*\\.txt$"), e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get(); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); Message<?> message = out.receive(10_000); assertNotNull(message);// w w w. j a v a 2s . c o m assertThat(message.getPayload(), instanceOf(InputStream.class)); assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt")); new IntegrationMessageHeaderAccessor(message).getCloseableResource().close(); message = out.receive(10_000); assertNotNull(message); assertThat(message.getPayload(), instanceOf(InputStream.class)); assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt")); new IntegrationMessageHeaderAccessor(message).getCloseableResource().close(); registration.destroy(); }
From source file:org.springframework.integration.ftp.dsl.FtpTests.java
@Test @SuppressWarnings("unchecked") public void testFtpMgetFlow() { QueueChannel out = new QueueChannel(); IntegrationFlow flow = f -> f// w w w . ja va2 s . c o m .handle(Ftp .outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET, "payload") .options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE) .filterExpression("name matches 'subFtpSource|.*1.txt'") .localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory") .localFilenameExpression("#remoteFileName.replaceFirst('ftpSource', 'localTarget')")) .channel(out); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); String dir = "ftpSource/"; registration.getInputChannel().send(new GenericMessage<>(dir + "*")); Message<?> result = out.receive(10_000); assertNotNull(result); List<File> localFiles = (List<File>) result.getPayload(); // should have filtered ftpSource2.txt assertEquals(2, localFiles.size()); for (File file : localFiles) { assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir)); } assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir + "subFtpSource")); registration.destroy(); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void successFailureAdvice() { final AtomicBoolean doFail = new AtomicBoolean(); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override// ww w . j a v a 2 s . c o m protected Object handleRequestMessage(Message<?> requestMessage) { if (doFail.get()) { throw new RuntimeException("qux"); } return "baz"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); Message<String> message = new GenericMessage<String>("Hello, world!"); // no advice handler.handleMessage(message); Message<?> reply = replies.receive(1000); assertNotNull(reply); assertEquals("baz", reply.getPayload()); PollableChannel successChannel = new QueueChannel(); PollableChannel failureChannel = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); advice.setBeanFactory(mock(BeanFactory.class)); advice.setSuccessChannel(successChannel); advice.setFailureChannel(failureChannel); advice.setOnSuccessExpression("'foo'"); advice.setOnFailureExpression("'bar:' + #exception.message"); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); // advice with success handler.handleMessage(message); reply = replies.receive(1000); assertNotNull(reply); assertEquals("baz", reply.getPayload()); Message<?> success = successChannel.receive(1000); assertNotNull(success); assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload()); assertEquals("foo", success.getPayload()); // advice with failure, not trapped doFail.set(true); try { handler.handleMessage(message); fail("Expected exception"); } catch (Exception e) { assertEquals("qux", e.getCause().getMessage()); } Message<?> failure = failureChannel.receive(1000); assertNotNull(failure); assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult()); // advice with failure, trapped advice.setTrapException(true); handler.handleMessage(message); failure = failureChannel.receive(1000); assertNotNull(failure); assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult()); assertNull(replies.receive(1)); // advice with failure, eval is result advice.setReturnFailureExpressionResult(true); handler.handleMessage(message); failure = failureChannel.receive(1000); assertNotNull(failure); assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult()); reply = replies.receive(1000); assertNotNull(reply); assertEquals("bar:qux", reply.getPayload()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void propagateOnSuccessExpressionFailures() { final AtomicBoolean doFail = new AtomicBoolean(); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override//from ww w . j a v a 2 s.c o m protected Object handleRequestMessage(Message<?> requestMessage) { if (doFail.get()) { throw new RuntimeException("qux"); } return "baz"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); Message<String> message = new GenericMessage<String>("Hello, world!"); PollableChannel successChannel = new QueueChannel(); PollableChannel failureChannel = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); advice.setBeanFactory(mock(BeanFactory.class)); advice.setSuccessChannel(successChannel); advice.setFailureChannel(failureChannel); advice.setOnSuccessExpression("1/0"); advice.setOnFailureExpression("1/0"); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); // failing advice with success handler.handleMessage(message); Message<?> reply = replies.receive(1000); assertNotNull(reply); assertEquals("baz", reply.getPayload()); Message<?> success = successChannel.receive(1000); assertNotNull(success); assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload()); assertEquals(ArithmeticException.class, success.getPayload().getClass()); assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage()); // propagate failing advice with success advice.setPropagateEvaluationFailures(true); try { handler.handleMessage(message); fail("Expected Exception"); } catch (MessageHandlingException e) { assertEquals("/ by zero", e.getCause().getMessage()); } reply = replies.receive(1); assertNull(reply); success = successChannel.receive(1000); assertNotNull(success); assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload()); assertEquals(ArithmeticException.class, success.getPayload().getClass()); assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void propagateOnFailureExpressionFailures() { final AtomicBoolean doFail = new AtomicBoolean(true); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override//from w ww. ja va2 s .c o m protected Object handleRequestMessage(Message<?> requestMessage) { if (doFail.get()) { throw new RuntimeException("qux"); } return "baz"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); Message<String> message = new GenericMessage<String>("Hello, world!"); PollableChannel successChannel = new QueueChannel(); PollableChannel failureChannel = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); advice.setSuccessChannel(successChannel); advice.setFailureChannel(failureChannel); advice.setOnSuccessExpression("1/0"); advice.setOnFailureExpression("1/0"); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); // failing advice with failure try { handler.handleMessage(message); fail("Expected exception"); } catch (Exception e) { assertEquals("qux", e.getCause().getMessage()); } Message<?> reply = replies.receive(1); assertNull(reply); Message<?> failure = failureChannel.receive(1000); assertNotNull(failure); assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass()); assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage()); // propagate failing advice with failure; expect original exception advice.setPropagateEvaluationFailures(true); try { handler.handleMessage(message); fail("Expected Exception"); } catch (MessageHandlingException e) { assertEquals("qux", e.getCause().getMessage()); } reply = replies.receive(1); assertNull(reply); failure = failureChannel.receive(1000); assertNotNull(failure); assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass()); assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void defaultRetrySucceedOnThirdTry() { final AtomicInteger counter = new AtomicInteger(2); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override//from ww w . ja v a 2s. c om protected Object handleRequestMessage(Message<?> requestMessage) { if (counter.getAndDecrement() > 0) { throw new RuntimeException("foo"); } return "bar"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); handler.handleMessage(message); assertTrue(counter.get() == -1); Message<?> reply = replies.receive(1000); assertNotNull(reply); assertEquals("bar", reply.getPayload()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void defaultStatefulRetrySucceedOnThirdTry() { final AtomicInteger counter = new AtomicInteger(2); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/* ww w.j a v a2 s .c o m*/ protected Object handleRequestMessage(Message<?> requestMessage) { if (counter.getAndDecrement() > 0) { throw new RuntimeException("foo"); } return "bar"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); advice.setRetryStateGenerator(new RetryStateGenerator() { public RetryState determineRetryState(Message<?> message) { return new DefaultRetryState(message.getHeaders().getId()); } }); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); for (int i = 0; i < 3; i++) { try { handler.handleMessage(message); } catch (Exception e) { assertTrue(i < 2); } } assertTrue(counter.get() == -1); Message<?> reply = replies.receive(1000); assertNotNull(reply); assertEquals("bar", reply.getPayload()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
private void defaultStatefulRetryRecoverAfterThirdTryGuts(final AtomicInteger counter, AbstractReplyProducingMessageHandler handler, QueueChannel replies, RequestHandlerRetryAdvice advice) { advice.setRecoveryCallback(new RecoveryCallback<Object>() { public Object recover(RetryContext context) throws Exception { return "baz"; }/* ww w .j av a2s .c o m*/ }); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); for (int i = 0; i < 4; i++) { try { handler.handleMessage(message); } catch (Exception e) { } } assertTrue(counter.get() == 0); Message<?> reply = replies.receive(1000); assertNotNull(reply); assertEquals("baz", reply.getPayload()); }
From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java
@Test public void errorMessageSendingRecovererTests() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/*from w w w. j a v a 2 s .c o m*/ protected Object handleRequestMessage(Message<?> requestMessage) { throw new RuntimeException("fooException"); } }; QueueChannel errors = new QueueChannel(); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); ErrorMessageSendingRecoverer recoverer = new ErrorMessageSendingRecoverer(errors); advice.setRecoveryCallback(recoverer); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<String>("Hello, world!"); handler.handleMessage(message); Message<?> error = errors.receive(1000); assertNotNull(error); assertEquals("fooException", ((Exception) error.getPayload()).getCause().getMessage()); }