use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.
the class FtpTests method testFtpInboundFlow.
@Test
public void testFtpInboundFlow() throws IOException {
QueueChannel out = new QueueChannel();
IntegrationFlow flow = IntegrationFlows.from(Ftp.inboundAdapter(sessionFactory()).preserveTimestamp(true).remoteDirectory("ftpSource").maxFetchSize(10).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);
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");
FileOutputStream fos = new FileOutputStream(remoteFile);
fos.write("New content".getBytes());
fos.close();
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());
assertEquals("New content", FileCopyUtils.copyToString(new FileReader(file)));
MessageSource<?> source = context.getBean(FtpInboundFileSynchronizingMessageSource.class);
assertThat(TestUtils.getPropertyValue(source, "maxFetchSize"), equalTo(10));
registration.destroy();
}
use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.
the class ManualFlowTests method testManualFlowRegistration.
@Test
public void testManualFlowRegistration() throws InterruptedException {
IntegrationFlow myFlow = f -> f.<String, String>transform(String::toUpperCase).channel(MessageChannels.queue()).transform("Hello, "::concat, e -> e.poller(p -> p.fixedDelay(10).maxMessagesPerPoll(1).receiveTimeout(10))).handle(new BeanFactoryHandler());
BeanFactoryHandler additionalBean = new BeanFactoryHandler();
IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(myFlow).addBean(additionalBean).register();
BeanFactoryHandler bean = this.beanFactory.getBean(flowRegistration.getId() + BeanFactoryHandler.class.getName() + "#0", BeanFactoryHandler.class);
assertSame(additionalBean, bean);
assertSame(this.beanFactory, bean.beanFactory);
MessagingTemplate messagingTemplate = flowRegistration.getMessagingTemplate();
messagingTemplate.setReceiveTimeout(10000);
assertEquals("Hello, FOO", messagingTemplate.convertSendAndReceive("foo", String.class));
assertEquals("Hello, BAR", messagingTemplate.convertSendAndReceive("bar", String.class));
try {
messagingTemplate.receive();
fail("UnsupportedOperationException expected");
} catch (Exception e) {
assertThat(e, instanceOf(UnsupportedOperationException.class));
assertThat(e.getMessage(), containsString("The 'receive()/receiveAndConvert()' isn't supported"));
}
flowRegistration.destroy();
assertFalse(this.beanFactory.containsBean(flowRegistration.getId()));
assertFalse(this.beanFactory.containsBean(flowRegistration.getId() + ".input"));
assertFalse(this.beanFactory.containsBean(flowRegistration.getId() + BeanFactoryHandler.class.getName() + "#0"));
ThreadPoolTaskScheduler taskScheduler = this.beanFactory.getBean(ThreadPoolTaskScheduler.class);
Thread.sleep(100);
assertEquals(0, taskScheduler.getActiveCount());
assertTrue(additionalBean.destroyed);
}
use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.
the class ManualFlowTests method testMessageProducerForOutputChannel.
@Test
public void testMessageProducerForOutputChannel() {
class MessageProducingHandler implements MessageHandler, MessageProducer {
private MessageChannel outputChannel;
@Override
public void setOutputChannel(MessageChannel outputChannel) {
this.outputChannel = outputChannel;
}
@Override
public MessageChannel getOutputChannel() {
return this.outputChannel;
}
@Override
public void handleMessage(Message<?> message) throws MessagingException {
this.outputChannel.send(message);
}
}
PollableChannel resultChannel = new QueueChannel();
IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow -> flow.handle(new MessageProducingHandler()).channel(resultChannel)).register();
this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId()).send(new GenericMessage<>("test"));
Message<?> receive = resultChannel.receive(1000);
assertNotNull(receive);
assertEquals("test", receive.getPayload());
}
use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.
the class FtpTests method testFtpInboundStreamFlow.
@Test
public void testFtpInboundStreamFlow() throws Exception {
QueueChannel out = new QueueChannel();
StandardIntegrationFlow flow = IntegrationFlows.from(Ftp.inboundStreamingAdapter(new FtpRemoteFileTemplate(sessionFactory())).remoteDirectory("ftpSource").maxFetchSize(11).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);
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();
MessageSource<?> source = context.getBean(FtpStreamingMessageSource.class);
assertThat(TestUtils.getPropertyValue(source, "maxFetchSize"), equalTo(11));
registration.destroy();
}
use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.
the class SftpTests method testSftpMgetFlow.
@Test
@SuppressWarnings("unchecked")
public void testSftpMgetFlow() {
QueueChannel out = new QueueChannel();
IntegrationFlow flow = f -> f.handle(Sftp.outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET, "payload").options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE).regexFileNameFilter("(subSftpSource|.*1.txt)").localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory").localFilenameExpression("#remoteFileName.replaceFirst('sftpSource', 'localTarget')")).channel(out);
String dir = "sftpSource/";
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
registration.getInputChannel().send(new GenericMessage<>(dir + "*"));
Message<?> result = out.receive(10_000);
assertNotNull(result);
List<File> localFiles = (List<File>) result.getPayload();
// should have filtered sftpSource2.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 + "subSftpSource"));
registration.destroy();
}
Aggregations