Skip to content

Instantly share code, notes, and snippets.

@sumelio
Forked from elarif/Main.java
Created May 11, 2018 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumelio/0e12cf446ae04e8e5e8fb9f0ab344e00 to your computer and use it in GitHub Desktop.
Save sumelio/0e12cf446ae04e8e5e8fb9f0ab344e00 to your computer and use it in GitHub Desktop.
spring-integration-stdout
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;
@Configuration
@IntegrationComponentScan
@ComponentScan
@EnableIntegration
@PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = true)
public class Main {
private final static Logger LOGGER = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws IOException {
final ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Main.class);
LOGGER.info("Hit 'Enter' to terminate");
getReader().readLine();
ctx.close();
}
private static BufferedReader getReader() {
final BufferedReader result = new BufferedReader(new InputStreamReader(System.in));
return result;
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
final PropertySourcesPlaceholderConfigurer result = new PropertySourcesPlaceholderConfigurer();
return result;
}
}
package test.configs;
import java.io.File;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.SourcePollingChannelAdapterSpec;
import org.springframework.integration.dsl.core.Pollers;
import org.springframework.integration.dsl.file.Files;
import org.springframework.integration.dsl.support.Consumer;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.messaging.MessageHandler;
@Configuration
public class MyFlow {
private final static String DEFAULT_LOGGING_LEVEL = LoggingHandler.Level.INFO.name();
@Bean
public IntegrationFlow pdfs(@Value("${directory:./in}") File directory,
@Value("${delay:100}") final long delay) {
final IntegrationFlow result =
IntegrationFlows.from(inboundAdapter(directory), poller(delay)).handle(loggingHandler())
.get();
return result;
}
private Consumer<SourcePollingChannelAdapterSpec> poller(final long delay) {
final Consumer<SourcePollingChannelAdapterSpec> result =
new Consumer<SourcePollingChannelAdapterSpec>() {
@Override
public void accept(final SourcePollingChannelAdapterSpec t) {
t.poller(Pollers.fixedRate(delay).get());
}
};
return result;
}
private FileReadingMessageSource inboundAdapter(final File directory) {
final FileReadingMessageSource result =
Files.inboundAdapter(directory).autoCreateDirectory(true).preventDuplicates().get();
return result;
}
private MessageHandler loggingHandler() {
final LoggingHandler result = new LoggingHandler(DEFAULT_LOGGING_LEVEL);
return result;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-integration.version>4.2.2.RELEASE</spring-integration.version>
<spring-integration-java-dsl.version>1.1.3.RELEASE</spring-integration-java-dsl.version>
<slf4j.version>1.7.21</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
<version>${spring-integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<version> ${spring-integration-java-dsl.version}</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment