diff --git a/annotations/basic/helloworld/README.md b/annotations/basic/helloworld/README.md new file mode 100644 index 000000000..15ea72dc7 --- /dev/null +++ b/annotations/basic/helloworld/README.md @@ -0,0 +1,39 @@ +Hello World Sample +================== + +This is an obvious place to get started. This sample project contains 2 basic sample applications: + +* Hello World +* Poller Application + +## Hello World + +The Hello World application demonstrates a simple message flow represented by the diagram below: + + Message -> Channel -> ServiceActivator -> QueueChannel + +To run the sample simply execute **HelloWorldApp** in package **org.springframework.integration.samples.helloworld**. +You can also execute that class using the [Gradle](http://www.gradle.org): + + $ gradlew :helloworld:runHelloWorldApp + +You should see the following output: + + INFO : org.springframework.integration.samples.helloworld.HelloWorldApp - ==> HelloWorldDemo: Hello World + +## Poller Application + +This simple application will print out the current system time twice every 20 seconds. + +More specifically, an **Inbound Channel Adapter** polls for the current system time 2 times every 20 seconds (20000 milliseconds). The resulting message contains as payload the time in milliseconds and the message is sent to a **Logging Channel Adapter**, which will print the time to the command prompt. + +To run the sample simply execute **PollerApp** in package **org.springframework.integration.samples.helloworld**. +You can also execute that class using the [Gradle](http://www.gradle.org): + + $ gradlew :helloworld:runPollerApp + +You should see output like the following: + + INFO : org.springframework.integration.samples.helloworld - 1328892135471 + INFO : org.springframework.integration.samples.helloworld - 1328892135524 + diff --git a/annotations/basic/helloworld/pom.xml b/annotations/basic/helloworld/pom.xml new file mode 100644 index 000000000..dd545c90e --- /dev/null +++ b/annotations/basic/helloworld/pom.xml @@ -0,0 +1,164 @@ + + + 4.0.0 + org.springframework.integration.samples + helloworld + 5.0.0.BUILD-SNAPSHOT + Hello World Sample + Hello World Sample + http://projects.spring.io/spring-integration + + SpringIO + https://spring.io + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + garyrussell + Gary Russell + grussell@pivotal.io + + project lead + + + + markfisher + Mark Fisher + mfisher@pivotal.io + + project founder and lead emeritus + + + + ghillert + Gunnar Hillert + ghillert@pivotal.io + + + abilan + Artem Bilan + abilan@pivotal.io + + + + scm:git:scm:git:git://github.com/spring-projects/spring-integration-samples.git + scm:git:scm:git:ssh://git@github.com:spring-projects/spring-integration-samples.git + https://github.com/spring-projects/spring-integration-samples + + + + org.springframework.integration + spring-integration-core + compile + + + jackson-module-kotlin + com.fasterxml.jackson.module + + + + + org.apache.logging.log4j + log4j-core + 2.7 + compile + + + jackson-module-kotlin + com.fasterxml.jackson.module + + + + + junit + junit + 4.12 + test + + + jackson-module-kotlin + com.fasterxml.jackson.module + + + * + org.hamcrest + + + + + org.hamcrest + hamcrest-all + 1.3 + test + + + jackson-module-kotlin + com.fasterxml.jackson.module + + + + + org.mockito + mockito-core + 2.10.0 + test + + + jackson-module-kotlin + com.fasterxml.jackson.module + + + * + org.hamcrest + + + + + org.springframework + spring-test + test + + + jackson-module-kotlin + com.fasterxml.jackson.module + + + + + + + repo.spring.io.milestone + Spring Framework Maven Milestone Repository + https://repo.spring.io/libs-milestone + + + repo.spring.io.snapshot + Spring Framework Maven Snapshot Repository + https://repo.spring.io/libs-snapshot + + + + + + org.springframework.integration + spring-integration-bom + 5.0.0.M7 + import + pom + + + org.springframework + spring-framework-bom + 5.0.0.RC4 + import + pom + + + + diff --git a/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloConfig.java b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloConfig.java new file mode 100644 index 000000000..c7433009d --- /dev/null +++ b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloConfig.java @@ -0,0 +1,42 @@ +package org.springframework.integration.samples.helloworld; + +import java.util.Map; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.IntegrationComponentScan; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.dsl.IntegrationFlow; +import org.springframework.integration.dsl.IntegrationFlows; +import org.springframework.integration.dsl.channel.MessageChannels; +import org.springframework.integration.handler.GenericHandler; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.PollableChannel; + +@Configuration +@EnableIntegration +@IntegrationComponentScan +@ComponentScan +public class HelloConfig { + + @Bean + public PollableChannel outputChannel() { + return MessageChannels.queue(10).get(); + } + + @Bean + public MessageChannel inputChannel() { + return MessageChannels.direct().get(); + } + + @Bean + public IntegrationFlow helloFlow(final HelloService helloService) { + return IntegrationFlows.from(inputChannel()).handle(new GenericHandler() { + public Object handle(String arg0, Map arg1) { + return helloService.sayHello(arg0); + } + }).channel(outputChannel()).get(); + } + +} diff --git a/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloService.java b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloService.java new file mode 100644 index 000000000..27078b85a --- /dev/null +++ b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloService.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2012 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.helloworld; + +import org.springframework.stereotype.Service; + +/** + * Simple POJO to be referenced from a Service Activator. + * + * @author Mark Fisher + */ +@Service +public class HelloService { + + public String sayHello(String name) { + return "Hello " + name; + } + +} diff --git a/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloWorldApp.java b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloWorldApp.java new file mode 100644 index 000000000..5752253ee --- /dev/null +++ b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/helloworld/HelloWorldApp.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2017 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.helloworld; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.PollableChannel; +import org.springframework.messaging.support.GenericMessage; + +/** + * Demonstrates a basic Message Endpoint that simply prepends a greeting + * ("Hello ") to an inbound String payload from a Message. This is a very + * low-level example, using Message Channels directly for both input and + * output. Notice that the output channel has a queue sub-element. It is + * therefore a PollableChannel and its consumers must invoke receive() as + * demonstrated below. + *

+ * View the configuration of the channels and the endpoint (a <service-activator/> + * element) in 'helloWorldDemo.xml' within this same package. + * + * @author Mark Fisher + * @author Oleg Zhurakousky + * @author Gary Russell + */ +public class HelloWorldApp { + + private static Log logger = LogFactory.getLog(HelloWorldApp.class); + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class); + MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class); + PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class); + inputChannel.send(new GenericMessage("World")); + logger.info("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload()); + context.close(); + } + +} diff --git a/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/systemtime/PollerApp.java b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/systemtime/PollerApp.java new file mode 100644 index 000000000..52a2e4a4a --- /dev/null +++ b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/systemtime/PollerApp.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2017 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.systemtime; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class PollerApp { + + /** + * Simple application that polls the current system time 2 times every + * 20 seconds (20000 milliseconds). + * + * The resulting message contains the time in milliseconds and the message + * is routed to a Logging Channel Adapter which will print the time to the + * command prompt. + * + * @param args Not used. + */ + @SuppressWarnings("resource") + public static void main(String[] args) throws Exception{ + new AnnotationConfigApplicationContext(PollerConfig.class); + } + +} diff --git a/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/systemtime/PollerConfig.java b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/systemtime/PollerConfig.java new file mode 100644 index 000000000..822dfcbf4 --- /dev/null +++ b/annotations/basic/helloworld/src/main/java/org/springframework/integration/samples/systemtime/PollerConfig.java @@ -0,0 +1,72 @@ +package org.springframework.integration.samples.systemtime; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.InboundChannelAdapter; +import org.springframework.integration.annotation.IntegrationComponentScan; +import org.springframework.integration.annotation.Poller; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.core.MessageSource; +import org.springframework.integration.dsl.IntegrationFlow; +import org.springframework.integration.dsl.IntegrationFlows; +import org.springframework.integration.dsl.channel.MessageChannels; +import org.springframework.integration.handler.LoggingHandler; +import org.springframework.integration.handler.LoggingHandler.Level; +import org.springframework.integration.scheduling.PollerMetadata; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; +import org.springframework.scheduling.support.PeriodicTrigger; + +/** + * + * + * + * + * + * + * + * + */ +@Configuration +@EnableIntegration +@IntegrationComponentScan +public class PollerConfig { + + @Bean + public MessageChannel logger() { + return MessageChannels.direct().get(); + } + + @Bean(name = PollerMetadata.DEFAULT_POLLER) + public PollerMetadata defaultPoller() { + PollerMetadata pollerMetadata = new PollerMetadata(); + pollerMetadata.setTrigger(new PeriodicTrigger(20000)); + pollerMetadata.setMaxMessagesPerPoll(2); + return pollerMetadata; + } + + @Bean + @InboundChannelAdapter(channel="logger", poller = @Poller(PollerMetadata.DEFAULT_POLLER)) + public MessageSource inbound() { + MessageSource source = new MessageSource() { + public Message receive() { + return MessageBuilder.withPayload(System.currentTimeMillis()).build(); + } + }; + return source; + } + + @Bean + public IntegrationFlow systemTimeFlow(MessageChannel logger) { + LoggingHandler loggingHandler = new LoggingHandler(Level.INFO); + loggingHandler.setLoggerName("org.springframework.integration.samples.systemtime"); + return IntegrationFlows + .from(logger) + .handle(loggingHandler) + .get(); + } + +} diff --git a/annotations/basic/helloworld/src/main/resources/log4j2.xml b/annotations/basic/helloworld/src/main/resources/log4j2.xml new file mode 100644 index 000000000..cfeafeb3e --- /dev/null +++ b/annotations/basic/helloworld/src/main/resources/log4j2.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + +