From b258239e08d9f6fc2d79080ae50d5a2ce85d0f50 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 23 Jan 2008 04:13:39 +0000 Subject: [PATCH] Added Cafe demo to spring-integration-samples --- .../integration/samples/cafe/Barista.java | 64 +++++++++++++++++++ .../integration/samples/cafe/Cafe.java | 44 +++++++++++++ .../integration/samples/cafe/CafeDemo.java | 46 +++++++++++++ .../integration/samples/cafe/Drink.java | 47 ++++++++++++++ .../integration/samples/cafe/DrinkOrder.java | 37 +++++++++++ .../integration/samples/cafe/DrinkRouter.java | 38 +++++++++++ .../integration/samples/cafe/DrinkType.java | 13 ++-- .../samples/cafe/OrderSplitter.java | 35 ++++++++++ .../integration/samples/cafe/cafeDemo.xml | 30 +++++++++ 9 files changed, 347 insertions(+), 7 deletions(-) create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java rename spring-integration-core/src/main/java/org/springframework/integration/router/Splitter.java => spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java (79%) create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java create mode 100644 spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java new file mode 100644 index 0000000000..89919ece3a --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2007 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.cafe; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.springframework.stereotype.Component; + +/** + * @author Mark Fisher + */ +@Component +public class Barista { + + private long hotDrinkDelay = 1000; + + private long coldDrinkDelay = 700; + + private AtomicInteger hotDrinkCounter = new AtomicInteger(); + + private AtomicInteger coldDrinkCounter = new AtomicInteger(); + + + public void setHotDrinkDelay(long hotDrinkDelay) { + this.hotDrinkDelay = hotDrinkDelay; + } + + public void setColdDrinkDelay(long coldDrinkDelay) { + this.coldDrinkDelay = coldDrinkDelay; + } + + public void prepareHotDrink(Drink drink) { + try { + Thread.sleep(this.hotDrinkDelay); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + System.out.println("prepared hot drink #" + hotDrinkCounter.incrementAndGet() + ": " + drink); + } + + public void prepareColdDrink(Drink drink) { + try { + Thread.sleep(this.coldDrinkDelay); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + System.out.println("prepared cold drink #" + coldDrinkCounter.incrementAndGet() + ": " + drink); + } + +} diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java new file mode 100644 index 0000000000..337a46268c --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2007 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.cafe; + +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.message.GenericMessage; + +/** + * The entry point for {@link CafeDemo}. When the 'placeOrder' + * method is invoked, it passes the {@link DrinkOrder} as the payload of a + * {@link org.springframework.integration.message.Message} to the + * 'orderChannel'. The channel reference is configured in the "cafe" bean + * definition within 'cafeDemo.xml'. + * + * @author Mark Fisher + */ +public class Cafe { + + private MessageChannel orderChannel; + + + public void setOrderChannel(MessageChannel orderChannel) { + this.orderChannel = orderChannel; + } + + public void placeOrder(DrinkOrder order) { + this.orderChannel.send(new GenericMessage(order)); + } + +} diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java new file mode 100644 index 0000000000..5ea23c9e47 --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2007 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.cafe; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Provides the 'main' method for running the Cafe Demo application. When an + * order is placed, the Cafe will send that order to the "orders" channel. + * The relevant components are defined within the configuration file + * ("cafeDemo.xml") or configured with annotations (such as the + * OrderSplitter, DrinkRouter, and Barista classes). + * + * @author Mark Fisher + */ +public class CafeDemo { + + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("cafeDemo.xml", CafeDemo.class); + context.start(); + Cafe cafe = (Cafe) context.getBean("cafe"); + DrinkOrder order = new DrinkOrder(); + Drink hotDoubleLatte = new Drink(DrinkType.LATTE, 2, false); + Drink icedTripleMocha = new Drink(DrinkType.MOCHA, 3, true); + order.addDrink(hotDoubleLatte); + order.addDrink(icedTripleMocha); + for (int i = 0; i < 10; i++) { + cafe.placeOrder(order); + } + } + +} diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java new file mode 100644 index 0000000000..27d69a8c5c --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2007 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.cafe; + +/** + * @author Mark Fisher + */ +public class Drink { + + private DrinkType type; + + private int shots = 1; + + private boolean iced = false; + + + public Drink(DrinkType type, int shots, boolean iced) { + this.type = type; + this.shots = shots; + this.iced = iced; + } + + + public boolean isIced() { + return this.iced; + } + + public String toString() { + return ((this.iced) ? "iced " : "hot " ) + + this.shots + " shot " + this.type; + } + +} diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java new file mode 100644 index 0000000000..c29a1a7c5c --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2007 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.cafe; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Mark Fisher + */ +public class DrinkOrder { + + private List drinks = new ArrayList(); + + public void addDrink(Drink drink) { + this.drinks.add(drink); + } + + public List getDrinks() { + return this.drinks; + } + +} diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java new file mode 100644 index 0000000000..f917f9a5a3 --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2007 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.cafe; + +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.annotation.Router; + +/** + * @author Mark Fisher + */ +@MessageEndpoint(input="drinks") +public class DrinkRouter { + + @Router + public String resolveDrinkChannel(Drink drink) { + if (drink.isIced()) { + return "coldDrinks"; + } + else { + return "hotDrinks"; + } + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/router/Splitter.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java similarity index 79% rename from spring-integration-core/src/main/java/org/springframework/integration/router/Splitter.java rename to spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java index 2d8510b766..b495eab0c4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/router/Splitter.java +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java @@ -14,17 +14,16 @@ * limitations under the License. */ -package org.springframework.integration.router; - -import java.util.Collection; +package org.springframework.integration.samples.cafe; /** - * Base interface for splitters. - * * @author Mark Fisher */ -public interface Splitter { +public enum DrinkType { - Collection split(T t); + ESPRESSO, + LATTE, + CAPPUCCINO, + MOCHA } diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java new file mode 100644 index 0000000000..ae7068d92b --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2007 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.cafe; + +import java.util.List; + +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.annotation.Splitter; + +/** + * @author Mark Fisher + */ +@MessageEndpoint(input="orders") +public class OrderSplitter { + + @Splitter(channel="drinks") + public List split(DrinkOrder order) { + return order.getDrinks(); + } + +} diff --git a/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml new file mode 100644 index 0000000000..eed7e735dc --- /dev/null +++ b/spring-integration-samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + +