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
This commit is contained in:
Artem Bilan
2014-09-08 15:03:05 +03:00
parent b20a275a6a
commit 8de0f77321
5 changed files with 243 additions and 5 deletions

View File

@@ -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'

17
dsl/cafe-dsl/README.md Normal file
View File

@@ -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

View File

@@ -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")
.<Order>split(Order::getItems, null)
.channel(MessageChannels.executor(this.taskExecutor()))
.<OrderItem, String>route(orderItem -> orderItem.isIced() ? "coldDrinks" : "hotDrinks")
.get();
}
@Bean
public IntegrationFlow coldDrinksFlow() {
return IntegrationFlows.from(MessageChannels.queue("coldDrinks", 10))
.<OrderItem>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))
.<OrderItem>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")
.<OrderItem, Drink>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();
}
}

View File

@@ -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() {
}
}

View File

@@ -1,2 +1,2 @@
version=3.0.0.BUILD-SNAPSHOT
springBootVersion=1.1.0.RELEASE
springBootVersion=1.1.6.RELEASE