From 8de0f77321884bf80c3ef2ee7dd485b03612700c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 8 Sep 2014 15:03:05 +0300 Subject: [PATCH] INTSAMPLES-135: DSL Cafe Demo Sample JIRA: https://jira.spring.io/browse/INTSAMPLES-135 Conflicts: gradle.properties Upgrade version: Boot 1.1.5, SI 4.0.4, DSL 1.0.0.M3, SF 4.0.7 --- build.gradle | 27 ++- dsl/cafe-dsl/README.md | 17 ++ .../samples/dsl/cafe/Application.java | 165 ++++++++++++++++++ .../samples/dsl/cafe/ApplicationTests.java | 37 ++++ gradle.properties | 2 +- 5 files changed, 243 insertions(+), 5 deletions(-) create mode 100644 dsl/cafe-dsl/README.md create mode 100644 dsl/cafe-dsl/src/main/java/org/springframework/integration/samples/dsl/cafe/Application.java create mode 100644 dsl/cafe-dsl/src/test/java/org/springframework/integration/samples/dsl/cafe/ApplicationTests.java diff --git a/build.gradle b/build.gradle index b5757782..a1d7fb04 100644 --- a/build.gradle +++ b/build.gradle @@ -191,10 +191,10 @@ subprojects { subproject -> postgresVersion = '9.1-901-1.jdbc4' subethasmtpVersion = '1.2' slf4jVersion = '1.7.6' - springIntegrationVersion = '4.0.3.RELEASE' - springIntegration41Version = '4.1.0.BUILD-SNAPSHOT' - springIntegrationDslVersion = '1.0.0.M2' - springVersion = '4.0.5.RELEASE' + springIntegrationVersion = '4.0.4.RELEASE' + springIntegration41Version = '4.1.0.M1' + springIntegrationDslVersion = '1.0.0.M3' + springVersion = '4.0.7.RELEASE' springSecurityVersion = '3.2.4.RELEASE' springWebFlowVersion = '2.3.3.RELEASE' tilesJspVersion = '2.2.1' @@ -529,6 +529,25 @@ project('si4demo') { mainClassName = 'org.springframework.integration.samples.si4demo.dsl.Application' } +project('cafe-dsl') { + description = 'Java DSL Cafe Sample' + + apply plugin: 'spring-boot' + + sourceCompatibility = 1.8 + + dependencies { + compile project(":cafe-si") + compile 'org.springframework.boot:spring-boot-starter-integration' + compile "org.springframework.integration:spring-integration-java-dsl:$springIntegrationDslVersion" + + testCompile 'org.springframework.boot:spring-boot-starter-test' + } + + mainClassName = 'org.springframework.integration.samples.dsl.cafe.Application' +} + + project('jdbc') { description = 'JDBC Basic Sample' diff --git a/dsl/cafe-dsl/README.md b/dsl/cafe-dsl/README.md new file mode 100644 index 00000000..c6ee239f --- /dev/null +++ b/dsl/cafe-dsl/README.md @@ -0,0 +1,17 @@ +#Cafe Demo: Spring Integration Java DSL + +This sample demonstrates the classical Cafe Demo, but it is based on [Spring Integration Java DSL](https://github.com/spring-projects/spring-integration-extensions/wiki/Spring-Integration-Java-DSL-Reference) + and [Spring Boot](http://projects.spring.io/spring-boot). + +See the `cafe` project **README.md** for more details about the domain and the Cafe algorithm. + +## Run the Sample + +* You need Java 8 to run this sample, because it is based on Lambdas. +* running the `org.springframework.integration.samples.dsl.cafe.Application` class from within STS (Right-click on +Main class --> Run As --> Java Application) +* or from the command line: + + $ gradlew :cafe-dsl:run + + diff --git a/dsl/cafe-dsl/src/main/java/org/springframework/integration/samples/dsl/cafe/Application.java b/dsl/cafe-dsl/src/main/java/org/springframework/integration/samples/dsl/cafe/Application.java new file mode 100644 index 00000000..55aed541 --- /dev/null +++ b/dsl/cafe-dsl/src/main/java/org/springframework/integration/samples/dsl/cafe/Application.java @@ -0,0 +1,165 @@ +/* + * 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.dsl.cafe; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.Gateway; +import org.springframework.integration.annotation.IntegrationComponentScan; +import org.springframework.integration.annotation.MessagingGateway; +import org.springframework.integration.dsl.IntegrationFlow; +import org.springframework.integration.dsl.IntegrationFlows; +import org.springframework.integration.dsl.channel.MessageChannels; +import org.springframework.integration.dsl.support.Pollers; +import org.springframework.integration.samples.cafe.Delivery; +import org.springframework.integration.samples.cafe.Drink; +import org.springframework.integration.samples.cafe.DrinkType; +import org.springframework.integration.samples.cafe.Order; +import org.springframework.integration.samples.cafe.OrderItem; +import org.springframework.integration.scheduling.PollerMetadata; +import org.springframework.integration.stream.CharacterStreamWritingMessageHandler; + +/** + * @author Artem Bilan + * @since 3.0 + */ +@Configuration +@EnableAutoConfiguration +@IntegrationComponentScan +public class Application { + + public static void main(String[] args) throws InterruptedException { + ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args); + + Cafe cafe = ctx.getBean(Cafe.class); + for (int i = 1; i <= 100; i++) { + Order order = new Order(i); + order.addItem(DrinkType.LATTE, 2, false); + order.addItem(DrinkType.MOCHA, 3, true); + cafe.placeOrder(order); + } + + Thread.sleep(60000); + + ctx.close(); + } + + @MessagingGateway + public interface Cafe { + + @Gateway(requestChannel = "orders") + void placeOrder(Order order); + + } + + private AtomicInteger hotDrinkCounter = new AtomicInteger(); + + private AtomicInteger coldDrinkCounter = new AtomicInteger(); + + @Bean + public Executor taskExecutor() { + return Executors.newCachedThreadPool(); + } + + @Bean(name = PollerMetadata.DEFAULT_POLLER) + public PollerMetadata poller() { + return Pollers.fixedDelay(1000).get(); + } + + @Bean + public IntegrationFlow ordersFlow() { + return IntegrationFlows.from("orders") + .split(Order::getItems, null) + .channel(MessageChannels.executor(this.taskExecutor())) + .route(orderItem -> orderItem.isIced() ? "coldDrinks" : "hotDrinks") + .get(); + } + + @Bean + public IntegrationFlow coldDrinksFlow() { + return IntegrationFlows.from(MessageChannels.queue("coldDrinks", 10)) + .handle((orderItem, h) -> { + try { + Thread.sleep(1000); + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } + System.out.println(Thread.currentThread().getName() + + " prepared cold drink #" + this.coldDrinkCounter.incrementAndGet() + " for order #" + + orderItem.getOrderNumber() + ": " + orderItem); + + return orderItem; + }) + .channel("preparedDrinks") + .get(); + } + + @Bean + public IntegrationFlow hotDrinksFlow() { + return IntegrationFlows.from(MessageChannels.queue("hotDrinks", 10)) + .handle((orderItem, h) -> { + try { + Thread.sleep(5000); + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } + System.out.println(Thread.currentThread().getName() + + " prepared hot drink #" + this.hotDrinkCounter.incrementAndGet() + " for order #" + + orderItem.getOrderNumber() + ": " + orderItem); + + return orderItem; + }) + .channel("preparedDrinks") + .get(); + } + + + @Bean + public IntegrationFlow preparedDrinksFlow() { + return IntegrationFlows.from("preparedDrinks") + .transform(orderItem -> + new Drink(orderItem.getOrderNumber(), + orderItem.getDrinkType(), + orderItem.isIced(), + orderItem.getShots()) + ) + .aggregate(aggregator -> + aggregator.outputProcessor(g -> + new Delivery(g.getMessages() + .stream() + .map(message -> (Drink) message.getPayload()) + .collect(Collectors.toList())) + ) + .correlationStrategy(m -> ((Drink) m.getPayload()).getOrderNumber()) + , null) + .handle(CharacterStreamWritingMessageHandler.stdout()) + .get(); + } + +} diff --git a/dsl/cafe-dsl/src/test/java/org/springframework/integration/samples/dsl/cafe/ApplicationTests.java b/dsl/cafe-dsl/src/test/java/org/springframework/integration/samples/dsl/cafe/ApplicationTests.java new file mode 100644 index 00000000..a7de4b5d --- /dev/null +++ b/dsl/cafe-dsl/src/test/java/org/springframework/integration/samples/dsl/cafe/ApplicationTests.java @@ -0,0 +1,37 @@ +/* + * 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.dsl.cafe; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Artem Bilan + * @since 3.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/gradle.properties b/gradle.properties index 4e776fbd..03ec998d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ version=3.0.0.BUILD-SNAPSHOT -springBootVersion=1.1.0.RELEASE +springBootVersion=1.1.6.RELEASE