From c06828558609d2da58fa9f45dff1f1095b93778c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 18 Apr 2016 14:51:33 +0100 Subject: [PATCH] Streamline Integration Starter and add Java DSL to it This commit streamlines the Integration Starter by removing the file http, ip, and stream modules as they are not always used by a majority of apps that use Spring Integration and can also pull in other, unwanted dependencies. Additionally, a dependency on spring-integration-java-dsl has been added. This makes it easy for users to configure Spring Integration using Java configuration (the recommended approach), rather than via XML. The Integration sample has been updated to use the DSL. Further improvements could be made once the sample is using Java 8. Closes gh-5528 --- spring-boot-dependencies/pom.xml | 6 ++ .../spring-boot-sample-integration/pom.xml | 4 ++ .../sample/integration/HelloWorldService.java | 14 +++-- .../sample/integration/SampleEndpoint.java | 10 ++-- .../SampleIntegrationApplication.java | 57 ++++++++++++++++++- .../src/main/resources/application.properties | 2 - .../main/resources/integration-context.xml | 20 ------- .../spring-boot-starter-integration/pom.xml | 14 +---- 8 files changed, 79 insertions(+), 48 deletions(-) delete mode 100644 spring-boot-samples/spring-boot-sample-integration/src/main/resources/integration-context.xml diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 3fc7178295d..e4bf7284c32 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -147,6 +147,7 @@ Hopper-SR1 0.19.0.RELEASE 4.3.0.M1 + 1.1.2.RELEASE 1.2.5.RELEASE 1.1.5.RELEASE 1.2.0.RELEASE @@ -2064,6 +2065,11 @@ + + org.springframework.integration + spring-integration-java-dsl + ${spring-integration-java-dsl.version} + org.springframework.mobile spring-mobile-device diff --git a/spring-boot-samples/spring-boot-sample-integration/pom.xml b/spring-boot-samples/spring-boot-sample-integration/pom.xml index 46d5a5a7926..37f9d1a99db 100644 --- a/spring-boot-samples/spring-boot-sample-integration/pom.xml +++ b/spring-boot-samples/spring-boot-sample-integration/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.integration + spring-integration-file + org.springframework.integration spring-integration-jmx diff --git a/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/HelloWorldService.java b/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/HelloWorldService.java index f904d07dab0..e68197c39aa 100644 --- a/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/HelloWorldService.java +++ b/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/HelloWorldService.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,16 @@ package sample.integration; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; -@Component +@Service public class HelloWorldService { - @Autowired - private ServiceProperties configuration; + private final ServiceProperties configuration; + + public HelloWorldService(ServiceProperties configuration) { + this.configuration = configuration; + } public String getHelloMessage(String name) { return this.configuration.getGreeting() + " " + name; diff --git a/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleEndpoint.java b/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleEndpoint.java index 852d9797b6f..a478666b685 100644 --- a/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleEndpoint.java +++ b/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ package sample.integration; import java.io.File; import java.io.FileInputStream; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.annotation.MessageEndpoint; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.util.StreamUtils; @@ -27,8 +26,11 @@ import org.springframework.util.StreamUtils; @MessageEndpoint public class SampleEndpoint { - @Autowired - private HelloWorldService helloWorldService; + private final HelloWorldService helloWorldService; + + public SampleEndpoint(HelloWorldService helloWorldService) { + this.helloWorldService = helloWorldService; + } @ServiceActivator public String hello(File input) throws Exception { diff --git a/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleIntegrationApplication.java b/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleIntegrationApplication.java index 96fec479235..a893f386d5a 100644 --- a/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleIntegrationApplication.java +++ b/spring-boot-samples/spring-boot-sample-integration/src/main/java/sample/integration/SampleIntegrationApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,69 @@ package sample.integration; +import java.io.File; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.ImportResource; +import org.springframework.context.annotation.Bean; +import org.springframework.integration.channel.DirectChannel; +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.support.Consumer; +import org.springframework.integration.file.FileReadingMessageSource; +import org.springframework.integration.file.FileWritingMessageHandler; @SpringBootApplication @EnableConfigurationProperties(ServiceProperties.class) -@ImportResource("integration-context.xml") public class SampleIntegrationApplication { + @Bean + public FileReadingMessageSource fileReader() { + FileReadingMessageSource reader = new FileReadingMessageSource(); + reader.setDirectory(new File("target/input")); + return reader; + } + + @Bean + public DirectChannel inputChannel() { + return new DirectChannel(); + } + + @Bean + public DirectChannel outputChannel() { + return new DirectChannel(); + } + + @Bean + public FileWritingMessageHandler fileWriter() { + FileWritingMessageHandler writer = new FileWritingMessageHandler( + new File("target/output")); + writer.setExpectReply(false); + return writer; + } + + @Bean + public IntegrationFlow integrationFlow(SampleEndpoint endpoint) { + return IntegrationFlows.from(fileReader(), new FixedRatePoller()) + .channel(inputChannel()).handle(endpoint).channel(outputChannel()) + .handle(fileWriter()).get(); + } + public static void main(String[] args) throws Exception { SpringApplication.run(SampleIntegrationApplication.class, args); } + private static class FixedRatePoller + implements Consumer { + + @Override + public void accept(SourcePollingChannelAdapterSpec spec) { + spec.poller(Pollers.fixedRate(500)); + } + + } + } diff --git a/spring-boot-samples/spring-boot-sample-integration/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-integration/src/main/resources/application.properties index 92dc00252be..1a341165477 100644 --- a/spring-boot-samples/spring-boot-sample-integration/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-integration/src/main/resources/application.properties @@ -1,4 +1,2 @@ -logging.file: /tmp/logs/app.log logging.level.org.springframework.integration.file: DEBUG service.greeting: Hello -debug: true \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-integration/src/main/resources/integration-context.xml b/spring-boot-samples/spring-boot-sample-integration/src/main/resources/integration-context.xml deleted file mode 100644 index 1372fe393a7..00000000000 --- a/spring-boot-samples/spring-boot-sample-integration/src/main/resources/integration-context.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - diff --git a/spring-boot-starters/spring-boot-starter-integration/pom.xml b/spring-boot-starters/spring-boot-starter-integration/pom.xml index 19ea166498c..62efa190ae3 100644 --- a/spring-boot-starters/spring-boot-starter-integration/pom.xml +++ b/spring-boot-starters/spring-boot-starter-integration/pom.xml @@ -32,19 +32,7 @@ org.springframework.integration - spring-integration-file - - - org.springframework.integration - spring-integration-http - - - org.springframework.integration - spring-integration-ip - - - org.springframework.integration - spring-integration-stream + spring-integration-java-dsl