From 8ec1d3557b2d3dec64e7c59d2729ede0dc78478d Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 16 Sep 2010 14:48:11 -0400 Subject: [PATCH] INT-1380 added cafe sample --- case-studies/cafe/.classpath | 10 +++ case-studies/cafe/.project | 23 ++++++ .../cafe/.settings/org.eclipse.jdt.core.prefs | 6 ++ case-studies/cafe/pom.xml | 52 ++++++++++++ case-studies/cafe/readme.txt | 60 ++++++++++++++ .../integration/samples/cafe/Cafe.java | 36 +++++++++ .../integration/samples/cafe/Delivery.java | 61 ++++++++++++++ .../integration/samples/cafe/Drink.java | 50 ++++++++++++ .../integration/samples/cafe/DrinkType.java | 29 +++++++ .../integration/samples/cafe/Order.java | 48 +++++++++++ .../integration/samples/cafe/OrderItem.java | 62 ++++++++++++++ .../samples/cafe/annotation/Barista.java | 81 +++++++++++++++++++ .../samples/cafe/annotation/DrinkRouter.java | 34 ++++++++ .../cafe/annotation/OrderSplitter.java | 37 +++++++++ .../samples/cafe/annotation/Waiter.java | 43 ++++++++++ .../integration/samples/cafe/xml/Barista.java | 76 +++++++++++++++++ .../integration/samples/cafe/xml/Waiter.java | 33 ++++++++ .../integration/cafeDemo-annotation.xml | 40 +++++++++ .../spring/integration/cafeDemo-xml.xml | 42 ++++++++++ .../annotation/CafeDemoTestAnnotation.java | 53 ++++++++++++ .../samples/cafe/xml/CafeDemoTestXml.java | 55 +++++++++++++ .../cafe/src/test/resources/log4j.xml | 28 +++++++ 22 files changed, 959 insertions(+) create mode 100644 case-studies/cafe/.classpath create mode 100644 case-studies/cafe/.project create mode 100644 case-studies/cafe/.settings/org.eclipse.jdt.core.prefs create mode 100644 case-studies/cafe/pom.xml create mode 100644 case-studies/cafe/readme.txt create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Cafe.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Delivery.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Drink.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Order.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Barista.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/DrinkRouter.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/OrderSplitter.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Waiter.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Barista.java create mode 100644 case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Waiter.java create mode 100644 case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-annotation.xml create mode 100644 case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-xml.xml create mode 100644 case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/annotation/CafeDemoTestAnnotation.java create mode 100644 case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/xml/CafeDemoTestXml.java create mode 100644 case-studies/cafe/src/test/resources/log4j.xml diff --git a/case-studies/cafe/.classpath b/case-studies/cafe/.classpath new file mode 100644 index 00000000..1fd94e6d --- /dev/null +++ b/case-studies/cafe/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/case-studies/cafe/.project b/case-studies/cafe/.project new file mode 100644 index 00000000..06332d4f --- /dev/null +++ b/case-studies/cafe/.project @@ -0,0 +1,23 @@ + + + cafe + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + + org.maven.ide.eclipse.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/case-studies/cafe/.settings/org.eclipse.jdt.core.prefs b/case-studies/cafe/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..fa96d4c9 --- /dev/null +++ b/case-studies/cafe/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,6 @@ +#Wed Feb 24 19:23:36 PST 2010 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/case-studies/cafe/pom.xml b/case-studies/cafe/pom.xml new file mode 100644 index 00000000..a0f34e38 --- /dev/null +++ b/case-studies/cafe/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + org.springframework.integration.samples + cafe + 2.0.0 + Spring Integration Cafe Sample + + 2.0.0.M7 + 1.2.15 + 4.7 + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + org.springframework.integration + spring-integration-stream + ${spring.integration.version} + + + log4j + log4j + ${log4j.version} + + + + junit + junit + ${junit.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + -Xlint:all + true + false + + + + + diff --git a/case-studies/cafe/readme.txt b/case-studies/cafe/readme.txt new file mode 100644 index 00000000..8c9857d4 --- /dev/null +++ b/case-studies/cafe/readme.txt @@ -0,0 +1,60 @@ +The Cafe sample emulates a simple operation of the Coffee shop shop when modeled using EIP +It is inspired by one of the samples featured in Gregor Hohpe's Ramblings. +The domain is that of a Cafe, and the basic flow is depicted in the following diagram: + + + Barista + hotDrinks ____________________ + |==========| -->| | + orders drinks / | prepareHotDrink() | +Place Order ->Cafe->|======|->OrderSplitter->|======|->DrinkRouter | | + \ coldDeinks | prepareColdDrink() | + |==========| -->| | + |____________________| + + Legend: |====| - channels + + +The Order object may contain multiple OrderItems. Once the order is placed, a Splitter will break the +composite order message into a single message per drink. Each of these is then processed by a Router that +determines whether the drink is hot or cold (checking the OrderItem object's 'isIced' property). The Barista +prepares each drink, but hot and cold drink preparation are handled by two distinct methods: 'prepareHotDrink' +and 'prepareColdDrink'. The prepared drinks are then sent to the Waiter where they are aggregated into a +Delivery object. + +Instructions for running the CafeDemo sample +------------------------------------------------------------------------------- +1. See README.txt in the parent directory. + +2. To run this sample as a standalone application simply execute the CafeDemo + class in the org.springframework.integration.samples.cafe.xml package. + +3. This sample is also configured to be a valid OSGi bundle. This means you can + deploy the generated JAR into an OSGi environment providing that the Spring + Integration bundles as well as other prerequisites (e.g., Spring Framework) + were installed. For example: To deploy it on SpringSource dm Server simply + drop the cafe-[version].jar file, located in the 'target' directory, to the + 'pickup' directory of the dm Server instance. You should see output similar + to the following in the trace file: + +. . . . . . . . . . . . . + System.out I ----------------------- +[2010-07-11 01:43:04.614] task-scheduler-3 System.out I Order #1 +[2010-07-11 01:43:04.614] task-scheduler-3 System.out I Iced MOCHA, 3 shots. +[2010-07-11 01:43:04.614] task-scheduler-3 System.out I Hot LATTE, 2 shots. +[2010-07-11 01:43:04.614] task-scheduler-3 System.out I ----------------------- +[2010-07-11 01:43:04.618] task-scheduler-2 System.out I task-scheduler-2 prepared cold drink #5 for order #5: iced 3 shot MOCHA +[2010-07-11 01:43:05.618] task-scheduler-2 System.out I task-scheduler-2 prepared cold drink #6 for order #6: iced 3 shot MOCHA +[2010-07-11 01:43:06.618] task-scheduler-2 System.out I task-scheduler-2 prepared cold drink #7 for order #7: iced 3 shot MOCHA +[2010-07-11 01:43:07.619] task-scheduler-2 System.out I task-scheduler-2 prepared cold drink #8 for order #8: iced 3 shot MOCHA +[2010-07-11 01:43:08.619] task-scheduler-2 System.out I task-scheduler-2 prepared cold drink #9 for order #9: iced 3 shot MOCHA +[2010-07-11 01:43:09.614] task-scheduler-3 System.out I task-scheduler-3 prepared hot drink #2 for order #2: hot 2 shot LATTE +[2010-07-11 01:43:09.615] task-scheduler-3 System.out I ----------------------- +[2010-07-11 01:43:09.615] task-scheduler-3 System.out I Order #2 +[2010-07-11 01:43:09.615] task-scheduler-3 System.out I Iced MOCHA, 3 shots. +[2010-07-11 01:43:09.615] task-scheduler-3 System.out I Hot LATTE, 2 shots. +[2010-07-11 01:43:09.615] task-scheduler-3 System.out I ----------------------- +[2010-07-11 01:43:09.620] task-scheduler-2 System.out I task-scheduler-2 prepared cold +. . . . . . . . . . . . . + +Happy integration :-) \ No newline at end of file diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Cafe.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Cafe.java new file mode 100644 index 00000000..66363ca8 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Cafe.java @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2010 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.Gateway; + +/** + * The entry point for Cafe Demo. The demo's main() method invokes the + * 'placeOrder' method on a generated MessagingGateway proxy. + * The gateway then passes the {@link Order} as the payload of a + * {@link org.springframework.integration.Message} to the + * configured requestChannel. The channel ('orders') is + * defined in the 'cafeDemo.xml' file. + * + * @author Mark Fisher + */ +public interface Cafe { + + @Gateway(requestChannel="orders") + void placeOrder(Order order); + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Delivery.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Delivery.java new file mode 100644 index 00000000..a59fbd43 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Delivery.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2010 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; + +/** + * @author Marius Bogoevici + */ +public class Delivery { + + private static final String SEPARATOR = "-----------------------"; + + + private List deliveredDrinks; + + private int orderNumber; + + + public Delivery(List deliveredDrinks) { + assert(deliveredDrinks.size() > 0); + this.deliveredDrinks = deliveredDrinks; + this.orderNumber = deliveredDrinks.get(0).getOrderNumber(); + } + + + public int getOrderNumber() { + return orderNumber; + } + + public List getDeliveredDrinks() { + return deliveredDrinks; + } + + @Override + public String toString() { + StringBuffer buffer = new StringBuffer(SEPARATOR + "\n"); + buffer.append("Order #" + getOrderNumber() + "\n"); + for (Drink drink : getDeliveredDrinks()) { + buffer.append(drink); + buffer.append("\n"); + } + buffer.append(SEPARATOR + "\n"); + return buffer.toString(); + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Drink.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Drink.java new file mode 100644 index 00000000..504a8658 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Drink.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2010 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 Marius Bogoevici + */ +public class Drink { + + private boolean iced; + + private int shots; + + private DrinkType drinkType; + + private int orderNumber; + + + public Drink(int orderNumber, DrinkType drinkType, boolean hot, int shots) { + this.orderNumber = orderNumber; + this.drinkType = drinkType; + this.iced = hot; + this.shots = shots; + } + + + public int getOrderNumber() { + return orderNumber; + } + + @Override + public String toString() { + return (iced?"Iced":"Hot") + " " + drinkType.toString() + ", " + shots + " shots."; + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java new file mode 100644 index 00000000..7e438db4 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/DrinkType.java @@ -0,0 +1,29 @@ +/* + * Copyright 2002-2010 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 enum DrinkType { + + ESPRESSO, + LATTE, + CAPPUCCINO, + MOCHA + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Order.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Order.java new file mode 100644 index 00000000..883d9708 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/Order.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2010 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 + * @author Marius Bogoevici + */ +public class Order { + + private List orderItems = new ArrayList(); + + private int number; + + public Order(int number) { + this.number = number; + } + + public void addItem(DrinkType drinkType, int shots, boolean iced) { + this.orderItems.add(new OrderItem(this, drinkType, shots, iced)); + } + + public int getNumber() { + return number; + } + + public List getItems() { + return this.orderItems; + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java new file mode 100644 index 00000000..0215ff4c --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java @@ -0,0 +1,62 @@ +/* + * Copyright 2002-2010 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 + * @author Marius Bogoevici + */ +public class OrderItem { + + private DrinkType type; + + private int shots = 1; + + private boolean iced = false; + + private final Order order; + + + public OrderItem(Order order, DrinkType type, int shots, boolean iced) { + this.order = order; + this.type = type; + this.shots = shots; + this.iced = iced; + } + + + public Order getOrder() { + return this.order; + } + + public boolean isIced() { + return this.iced; + } + + public int getShots() { + return shots; + } + + public DrinkType getDrinkType() { + return this.type; + } + + public String toString() { + return ((this.iced) ? "iced " : "hot ") + this.shots + " shot " + this.type; + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Barista.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Barista.java new file mode 100644 index 00000000..a3ccc9e3 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Barista.java @@ -0,0 +1,81 @@ +/* + * Copyright 2002-2010 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.annotation; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.log4j.Logger; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.samples.cafe.Drink; +import org.springframework.integration.samples.cafe.OrderItem; +import org.springframework.stereotype.Component; + +/** + * @author Mark Fisher + * @author Marius Bogoevici + */ +@Component +public class Barista { + private static Logger logger = Logger.getLogger(Barista.class); + private long hotDrinkDelay = 5000; + + private long coldDrinkDelay = 1000; + + 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; + } + + @ServiceActivator(inputChannel="hotDrinkBarista", outputChannel="preparedDrinks") + public Drink prepareHotDrink(OrderItem orderItem) { + try { + Thread.sleep(this.hotDrinkDelay); + logger.info(Thread.currentThread().getName() + + " prepared hot drink #" + hotDrinkCounter.incrementAndGet() + " for order #" + + orderItem.getOrder().getNumber() + ": " + orderItem); + return new Drink(orderItem.getOrder().getNumber(), orderItem.getDrinkType(), orderItem.isIced(), + orderItem.getShots()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } + } + + @ServiceActivator(inputChannel="coldDrinkBarista", outputChannel="preparedDrinks") + public Drink prepareColdDrink(OrderItem orderItem) { + try { + Thread.sleep(this.coldDrinkDelay); + logger.info(Thread.currentThread().getName() + + " prepared cold drink #" + coldDrinkCounter.incrementAndGet() + " for order #" + + orderItem.getOrder().getNumber() + ": " + orderItem); + return new Drink(orderItem.getOrder().getNumber(), orderItem.getDrinkType(), orderItem.isIced(), + orderItem.getShots()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/DrinkRouter.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/DrinkRouter.java new file mode 100644 index 00000000..0dc31ff5 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/DrinkRouter.java @@ -0,0 +1,34 @@ +/* + * Copyright 2002-2010 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.annotation; + +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.annotation.Router; +import org.springframework.integration.samples.cafe.OrderItem; + +/** + * @author Mark Fisher + */ +@MessageEndpoint +public class DrinkRouter { + + @Router(inputChannel="drinks") + public String resolveOrderItemChannel(OrderItem orderItem) { + return (orderItem.isIced()) ? "coldDrinks" : "hotDrinks"; + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/OrderSplitter.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/OrderSplitter.java new file mode 100644 index 00000000..a2a6d756 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/OrderSplitter.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2010 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.annotation; + +import java.util.List; + +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.annotation.Splitter; +import org.springframework.integration.samples.cafe.Order; +import org.springframework.integration.samples.cafe.OrderItem; + +/** + * @author Mark Fisher + */ +@MessageEndpoint +public class OrderSplitter { + + @Splitter(inputChannel="orders", outputChannel="drinks") + public List split(Order order) { + return order.getItems(); + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Waiter.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Waiter.java new file mode 100644 index 00000000..d6c0b3f5 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/annotation/Waiter.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2010 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.annotation; + +import java.util.List; + +import org.springframework.integration.annotation.Aggregator; +import org.springframework.integration.annotation.CorrelationStrategy; +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.samples.cafe.Delivery; +import org.springframework.integration.samples.cafe.Drink; + +/** + * @author Marius Bogoevici + */ +@MessageEndpoint +public class Waiter { + + @Aggregator(inputChannel = "preparedDrinks", outputChannel = "deliveries") + public Delivery prepareDelivery(List drinks) { + return new Delivery(drinks); + } + + @CorrelationStrategy + public int correlateByOrderNumber(Drink drink) { + return drink.getOrderNumber(); + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Barista.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Barista.java new file mode 100644 index 00000000..4db79f81 --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Barista.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2010 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.xml; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.log4j.Logger; +import org.springframework.integration.samples.cafe.Drink; +import org.springframework.integration.samples.cafe.OrderItem; + +/** + * @author Mark Fisher + * @author Marius Bogoevici + */ +public class Barista { + private static Logger logger = Logger.getLogger(Barista.class); + private long hotDrinkDelay = 5000; + + private long coldDrinkDelay = 1000; + + 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 Drink prepareHotDrink(OrderItem orderItem) { + try { + Thread.sleep(this.hotDrinkDelay); + logger.info(Thread.currentThread().getName() + + " prepared hot drink #" + hotDrinkCounter.incrementAndGet() + " for order #" + + orderItem.getOrder().getNumber() + ": " + orderItem); + return new Drink(orderItem.getOrder().getNumber(), orderItem.getDrinkType(), orderItem.isIced(), + orderItem.getShots()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } + } + + public Drink prepareColdDrink(OrderItem orderItem) { + try { + Thread.sleep(this.coldDrinkDelay); + logger.info(Thread.currentThread().getName() + + " prepared cold drink #" + coldDrinkCounter.incrementAndGet() + " for order #" + + orderItem.getOrder().getNumber() + ": " + orderItem); + return new Drink(orderItem.getOrder().getNumber(), orderItem.getDrinkType(), orderItem.isIced(), + orderItem.getShots()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return null; + } + } + +} diff --git a/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Waiter.java b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Waiter.java new file mode 100644 index 00000000..e9de2d5a --- /dev/null +++ b/case-studies/cafe/src/main/java/org/springframework/integration/samples/cafe/xml/Waiter.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2010 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.xml; + +import java.util.List; + +import org.springframework.integration.samples.cafe.Delivery; +import org.springframework.integration.samples.cafe.Drink; + +/** + * @author Marius Bogoevici + */ +public class Waiter { + + public Delivery prepareDelivery(List drinks) { + return new Delivery(drinks); + } + +} diff --git a/case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-annotation.xml b/case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-annotation.xml new file mode 100644 index 00000000..103bd5d4 --- /dev/null +++ b/case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-annotation.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-xml.xml b/case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-xml.xml new file mode 100644 index 00000000..8c660018 --- /dev/null +++ b/case-studies/cafe/src/main/resources/META-INF/spring/integration/cafeDemo-xml.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/annotation/CafeDemoTestAnnotation.java b/case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/annotation/CafeDemoTestAnnotation.java new file mode 100644 index 00000000..a1c8b810 --- /dev/null +++ b/case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/annotation/CafeDemoTestAnnotation.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2010 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.annotation; + +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; +import org.springframework.integration.samples.cafe.Cafe; +import org.springframework.integration.samples.cafe.DrinkType; +import org.springframework.integration.samples.cafe.Order; + +/** + * 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 channels are defined within the configuration file ("cafeDemo.xml"), + * and the relevant components are configured with annotations (such as the + * OrderSplitter, DrinkRouter, and Barista classes). + * + * @author Mark Fisher + * @author Marius Bogoevici + */ +public class CafeDemoTestAnnotation { + + @Test + public void testCafeDemoWithAnnotationSupport() { + AbstractApplicationContext context = + new ClassPathXmlApplicationContext("/META-INF/spring/integration/cafeDemo-annotation.xml", CafeDemoTestAnnotation.class); + + Cafe cafe = (Cafe) context.getBean("cafe"); + 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); + } + context.close(); + } +} diff --git a/case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/xml/CafeDemoTestXml.java b/case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/xml/CafeDemoTestXml.java new file mode 100644 index 00000000..80f8b04c --- /dev/null +++ b/case-studies/cafe/src/test/java/org/springframework/integration/samples/cafe/xml/CafeDemoTestXml.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2010 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.xml; + +import org.junit.Test; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.samples.cafe.Cafe; +import org.springframework.integration.samples.cafe.DrinkType; +import org.springframework.integration.samples.cafe.Order; + +/** + * 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"). + *

+ * If deploying in SpringSource dmServer, the relevant ApplicationContext + * configuration is in the META-INF/spring directory instead. + * + * @author Mark Fisher + * @author Marius Bogoevici + * @author Oleg Zhurakousky + */ +public class CafeDemoTestXml { + + @Test + public void testCafeDemoWithXmlSupport() { + AbstractApplicationContext context = + new ClassPathXmlApplicationContext("/META-INF/spring/integration/cafeDemo-xml.xml", CafeDemoTestXml.class); + + Cafe cafe = (Cafe) context.getBean("cafe"); + 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); + } + context.close(); + } +} diff --git a/case-studies/cafe/src/test/resources/log4j.xml b/case-studies/cafe/src/test/resources/log4j.xml new file mode 100644 index 00000000..678b69b3 --- /dev/null +++ b/case-studies/cafe/src/test/resources/log4j.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file