SpringOne 2014 XML to @Config (A-E)

AXML thru EDSL.

FMail is the same as EDSL with placeholders

GIMAP is empty
This commit is contained in:
Gary Russell
2014-09-07 11:10:36 -05:00
parent 8de0f77321
commit a97acde67b
14 changed files with 663 additions and 8 deletions

View File

@@ -520,13 +520,21 @@ project('si4demo') {
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-integration'
compile "org.springframework.integration:spring-integration-twitter:$springIntegrationVersion"
compile "org.springframework.integration:spring-integration-mail:$springIntegration41Version"
compile "org.springframework.integration:spring-integration-twitter:$springIntegration41Version"
compile "org.springframework.integration:spring-integration-java-dsl:$springIntegrationDslVersion"
compile "javax.mail:javax.mail-api:$javaxMailVersion"
compile "com.sun.mail:javax.mail:$javaxMailVersion"
compile "com.sun.mail:smtp:$javaxMailVersion"
compile "com.sun.mail:pop3:$javaxMailVersion"
compile "com.sun.mail:imap:$javaxMailVersion"
compile "com.rometools:rome:1.5.0"
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
mainClassName = 'org.springframework.integration.samples.si4demo.dsl.Application'
}
project('cafe-dsl') {

View File

@@ -1,9 +1,7 @@
#Spring Integration 4.0 Java Config/DSL Demo
#Spring Integration 4.0/4.1 Java Config/DSL Demo
This sample is the demo used in the [Spring Integration 4.0 Webinar](https://spring.io/blog/2014/05/15/webinar-replay-spring-integration-4-0-the-new-frontier)
It's currently using the spring-boot 1.1.0.M1 milestone so you may have to add the __repo.spring.io/repo__ repository to your settings.xml.
There are two demo applications:
__org.springframework.integration.samples.javaconfig.annotations.Application__ is a Spring Boot application using
@@ -37,3 +35,45 @@ Twitter now requires authentication to perform searches; visit the [twitter deve
accessTokenSecret:
Additional examples were added to this project at __SpringOne2GX 2014__ - see the __springone__ package.
This demonstrates moving from completely XML configuration, through Java Configuration, and ultimately to the DSL.
##AXML
This example is pure XML - it's a simple flow...
gateway -> transformer -> service-activator
The transformer concatentates the payload to itself (String), the service upper cases the payload. The result is returned to the gateway. "foo" becomes "FOOFOO".
##BXMLAndPojo
This takes the same example and shows how to configure it using "classic" Spring Integration annotations, available since 2.0.
##CNoXML
This takes the same example and configures it using standard Java Configuration (available since 4.0). Many of the standard annotations are now available on __@Bean__ definitions; note that the output channel must be configured on the handler, not the annotation. __@IntegrationComponentScan__ detects __@MessagingGateway__ interfaces and creates gateways.
##DBoot
This takes the same example as __C...__ and configures it using Spring Boot. The configuration is slightly more concise.
Note: Other applications in this project use the embedded web server (for http adapters). The boot apps in this section disable the embedded server by using the __SpringApplicationBuilder__ ...
new SpringApplicationBuilder(DBoot.class)
.web(false)
.run(args);
##EDSL
This takes the same example and configures it using the Spring Integration Java DSL. It requires Spring Integration 4.1.0.M1 and DSL 1.0.0.M3.
##FMail
This adds a recipient list router and sends a copy of the payload to an SMTP Email server (add credentials to __application.yml__).
##GIMAP
This is a separate application that demonstrates the DSL configuring an IMAP idle channel adapter, to receive emails sent by __FEmail__.

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author Gary Russell
*
*/
@Component
@ConfigurationProperties(prefix="gmail")
public class GMailProperties {
private String user;
private String password;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.a;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author Gary Russell
*
*/
public class AXML {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
new ClassPathXmlApplicationContext("AXML-context.xml");
System.out.println(ctx.getBean(FooService.class).foo("foo"));
ctx.close();
}
public static interface FooService {
String foo(String request);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.b;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
/**
*
* @author Gary Russell
*
*/
public class BXMLAndPojo {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
new ClassPathXmlApplicationContext("BXMLAndPojo-context.xml");
System.out.println(ctx.getBean(FooService.class).foo("foo"));
ctx.close();
}
public static interface FooService {
String foo(String request);
}
@MessageEndpoint
public static class MyComponents {
@Transformer(inputChannel="foo", outputChannel="bar")
public String transform(String foo) {
return foo + foo;
}
@ServiceActivator(inputChannel="bar")
public String service(String foo) {
return foo.toUpperCase();
}
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.c;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.transformer.MessageTransformingHandler;
import org.springframework.integration.transformer.MethodInvokingTransformer;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
/**
*
* @author Gary Russell
*
*/
public class CNoXML {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
new AnnotationConfigApplicationContext(CConfig.class);
System.out.println(ctx.getBean(FooService.class).foo("foo"));
ctx.close();
}
@MessagingGateway(defaultRequestChannel="foo")
public static interface FooService {
String foo(String request);
}
@Configuration
@EnableIntegration
@IntegrationComponentScan
public static class CConfig {
@Bean
public MessageChannel foo() {
return new DirectChannel();
}
@Transformer(inputChannel="foo")
@Bean
public MessageHandler transform() {
MessageTransformingHandler transformingHandler = new MessageTransformingHandler(new MethodInvokingTransformer(helpers(), "duplicate"));
transformingHandler.setOutputChannel(bar());
return transformingHandler;
}
@Bean
public MessageChannel bar() {
return new DirectChannel();
}
@ServiceActivator(inputChannel="bar")
@Bean
public MessageHandler service() {
return new ServiceActivatingHandler(new MethodInvokingTransformer(helpers(), "upper"));
}
@Bean
public MyHelpers helpers() {
return new MyHelpers();
}
}
public static class MyHelpers {
public String duplicate(String foo) {
return foo + foo;
}
public String upper(String foo) {
return foo.toUpperCase();
}
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.d;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.transformer.MessageTransformingHandler;
import org.springframework.integration.transformer.MethodInvokingTransformer;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
/**
*
* @author Gary Russell
*
*/
@Configuration
@EnableAutoConfiguration
@IntegrationComponentScan
public class DBoot {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
new SpringApplicationBuilder(DBoot.class)
.web(false)
.run(args);
System.out.println(ctx.getBean(FooService.class).foo("foo"));
ctx.close();
}
@MessagingGateway(defaultRequestChannel="foo")
public static interface FooService {
String foo(String request);
}
@Bean
public MessageChannel foo() {
return new DirectChannel();
}
@Transformer(inputChannel="foo")
@Bean
public MessageHandler transform() {
MessageTransformingHandler transformingHandler = new MessageTransformingHandler(new MethodInvokingTransformer(helpers(), "duplicate"));
transformingHandler.setOutputChannel(bar());
return transformingHandler;
}
@Bean
public MessageChannel bar() {
return new DirectChannel();
}
@ServiceActivator(inputChannel="bar")
@Bean
public MessageHandler service() {
return new ServiceActivatingHandler(new MethodInvokingTransformer(helpers(), "upper"));
}
@Bean
public MyHelpers helpers() {
return new MyHelpers();
}
public static class MyHelpers {
public String duplicate(String foo) {
return foo + foo;
}
public String upper(String foo) {
return foo.toUpperCase();
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.e;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.messaging.MessageChannel;
/**
*
* @author Gary Russell
*
*/
@Configuration
@EnableAutoConfiguration
@IntegrationComponentScan
public class EDSL {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
new SpringApplicationBuilder(EDSL.class)
.web(false)
.run(args);
System.out.println(ctx.getBean(FooService.class).foo("foo"));
ctx.close();
}
@MessagingGateway(defaultRequestChannel="foo")
public static interface FooService {
String foo(String request);
}
@Bean
public MessageChannel foo() {
return new DirectChannel();
}
@Bean
IntegrationFlow flow() {
return IntegrationFlows.from(foo())
.transform("payload + payload")
.handle(String.class, (p, h) -> p.toUpperCase())
.get();
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.f;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.samples.si4demo.springone.GMailProperties;
/**
*
* @author Gary Russell
*
*/
@Configuration
@EnableConfigurationProperties(GMailProperties.class)
@EnableAutoConfiguration
@IntegrationComponentScan
public class FMail {
@Autowired
GMailProperties gmail;
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
SpringApplication.run(FMail.class);
System.out.println(ctx.getBean(FooService.class).foo("foo"));
ctx.close();
}
@MessagingGateway(defaultRequestChannel="foo.input")
public static interface FooService {
String foo(String request);
}
@Bean
IntegrationFlow foo() {
return f -> f
.transform("payload + payload")
.handle(String.class, (p, h) -> p.toUpperCase())
//RLR
;
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.si4demo.springone.g;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.samples.si4demo.springone.GMailProperties;
/**
*
* @author Gary Russell
*
*/
@Configuration
@EnableConfigurationProperties(GMailProperties.class)
@EnableAutoConfiguration
public class GIMAP {
@Autowired
GMailProperties gmail;
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx =
SpringApplication.run(GIMAP.class);
System.out.println("Hit Enter to terminate");
System.in.read();
ctx.close();
}
@Bean
IntegrationFlow imapIdle() {
return null;
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:gateway
service-interface="org.springframework.integration.samples.si4demo.springone.a.AXML$FooService"
default-request-channel="foo" />
<int:channel id="foo" />
<int:transformer input-channel="foo" output-channel="bar"
expression="payload + payload" />
<int:channel id="bar" />
<int:service-activator input-channel="bar"
expression="payload.toUpperCase()" />
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<int:annotation-config />
<int:gateway
service-interface="org.springframework.integration.samples.si4demo.springone.b.BXMLAndPojo$FooService"
default-request-channel="foo" />
<int:channel id="foo" />
<int:channel id="bar" />
<bean class="org.springframework.integration.samples.si4demo.springone.b.BXMLAndPojo$MyComponents"/>
</beans>

View File

@@ -1,6 +1,10 @@
twitter:
oauth:
consumerKey:
consumerSecret:
accessToken:
accessTokenSecret:
consumerKey:
consumerSecret:
accessToken:
accessTokenSecret:
gmail:
user:
password:

View File

@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>