Search in sources :

Example 6 with IntegrationFlowRegistration

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();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) FileOutputStream(java.io.FileOutputStream) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FileReader(java.io.FileReader) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) Test(org.junit.Test)

Example 7 with IntegrationFlowRegistration

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);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Date(java.util.Date) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowAdapter(org.springframework.integration.dsl.IntegrationFlowAdapter) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) SpringRunner(org.springframework.test.context.junit4.SpringRunner) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) MessageProducerSpec(org.springframework.integration.dsl.MessageProducerSpec) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Objects(java.util.Objects) Configuration(org.springframework.context.annotation.Configuration) IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) DisposableBean(org.springframework.beans.factory.DisposableBean) Matchers.containsString(org.hamcrest.Matchers.containsString) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) MessageChannels(org.springframework.integration.dsl.MessageChannels) Scope(org.springframework.context.annotation.Scope) Assert.assertSame(org.junit.Assert.assertSame) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Flux(reactor.core.publisher.Flux) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 8 with IntegrationFlowRegistration

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());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Date(java.util.Date) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowAdapter(org.springframework.integration.dsl.IntegrationFlowAdapter) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) SpringRunner(org.springframework.test.context.junit4.SpringRunner) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) MessageProducerSpec(org.springframework.integration.dsl.MessageProducerSpec) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Objects(java.util.Objects) Configuration(org.springframework.context.annotation.Configuration) IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) DisposableBean(org.springframework.beans.factory.DisposableBean) Matchers.containsString(org.hamcrest.Matchers.containsString) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) MessageChannels(org.springframework.integration.dsl.MessageChannels) Scope(org.springframework.context.annotation.Scope) Assert.assertSame(org.junit.Assert.assertSame) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Flux(reactor.core.publisher.Flux) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) MessageProducer(org.springframework.integration.core.MessageProducer) Test(org.junit.Test)

Example 9 with IntegrationFlowRegistration

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();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test)

Example 10 with IntegrationFlowRegistration

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();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) RunWith(org.junit.runner.RunWith) FileHeaders(org.springframework.integration.file.FileHeaders) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Matcher(java.util.regex.Matcher) Pollers(org.springframework.integration.dsl.Pollers) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) ChannelSftp(com.jcraft.jsch.ChannelSftp) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) Test(org.junit.Test) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) File(java.io.File) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SftpTestSupport(org.springframework.integration.sftp.SftpTestSupport) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) List(java.util.List) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 QueueChannel (org.springframework.integration.channel.QueueChannel)14 IntegrationFlowRegistration (org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration)14 StandardIntegrationFlow (org.springframework.integration.dsl.StandardIntegrationFlow)13 IntegrationFlow (org.springframework.integration.dsl.IntegrationFlow)11 Matchers.containsString (org.hamcrest.Matchers.containsString)9 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 Assert.assertNotNull (org.junit.Assert.assertNotNull)9 Assert.assertThat (org.junit.Assert.assertThat)9 RunWith (org.junit.runner.RunWith)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 Configuration (org.springframework.context.annotation.Configuration)9 EnableIntegration (org.springframework.integration.config.EnableIntegration)9 IntegrationFlows (org.springframework.integration.dsl.IntegrationFlows)9 IntegrationFlowContext (org.springframework.integration.dsl.context.IntegrationFlowContext)9 Message (org.springframework.messaging.Message)9 GenericMessage (org.springframework.messaging.support.GenericMessage)9 DirtiesContext (org.springframework.test.annotation.DirtiesContext)9 SpringRunner (org.springframework.test.context.junit4.SpringRunner)9