use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.
the class IpIntegrationTests method testTcpGateways.
@Test
public void testTcpGateways() {
TestingUtilities.waitListening(this.server1, null);
IntegrationFlow flow = f -> f.handle(Tcp.outboundGateway(Tcp.netClient("localhost", this.server1.getPort()).serializer(TcpCodecs.crlf()).deserializer(TcpCodecs.lengthHeader1()).id("client1")).remoteTimeout(m -> 5000)).transform(Transformers.objectToString());
IntegrationFlowRegistration theFlow = this.flowContext.registration(flow).register();
assertThat(theFlow.getMessagingTemplate().convertSendAndReceive("foo", String.class), equalTo("FOO"));
}
use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.
the class FtpTests method testFtpOutboundFlow.
@Test
public void testFtpOutboundFlow() {
IntegrationFlow flow = f -> f.handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL).useTemporaryFileName(false).fileNameExpression("headers['" + FileHeaders.FILENAME + "']").remoteDirectory("ftpTarget"));
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
String fileName = "foo.file";
Message<ByteArrayInputStream> message = MessageBuilder.withPayload(new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8))).setHeader(FileHeaders.FILENAME, fileName).build();
registration.getInputChannel().send(message);
RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(sessionFactory());
FTPFile[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
assertEquals(1, files.length);
assertEquals(3, files[0].getSize());
registration.destroy();
}
use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.
the class FtpTests method testFtpMgetFlow.
@Test
@SuppressWarnings("unchecked")
public void testFtpMgetFlow() {
QueueChannel out = new QueueChannel();
IntegrationFlow flow = f -> f.handle(Ftp.outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET, "payload").options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE).fileExistsMode(FileExistsMode.IGNORE).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();
}
use of org.springframework.integration.dsl.IntegrationFlow 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.IntegrationFlow 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);
}
Aggregations