From eba716e65b6f6d70a187a429a8fbbf4b6811ea3d Mon Sep 17 00:00:00 2001 From: Marius Bogoevici Date: Sat, 23 Aug 2008 16:44:24 +0000 Subject: [PATCH] Adding aggregation capabilities to the CafeDemo sample, renaming a few domain objects --- .../integration/samples/cafe/Barista.java | 21 +++++-- .../integration/samples/cafe/Cafe.java | 8 +-- .../integration/samples/cafe/CafeDemo.java | 11 ++-- .../integration/samples/cafe/Delivery.java | 61 ++++++++++++++++++ .../samples/cafe/DeliveryAssembler.java | 35 +++++++++++ .../integration/samples/cafe/Drink.java | 39 ++++++------ .../integration/samples/cafe/DrinkRouter.java | 4 +- .../integration/samples/cafe/Order.java | 48 ++++++++++++++ .../integration/samples/cafe/OrderItem.java | 62 +++++++++++++++++++ .../samples/cafe/OrderSplitter.java | 2 +- .../cafe/{DrinkOrder.java => Waiter.java} | 20 +++--- .../integration/samples/cafe/cafeDemo.xml | 14 ++++- 12 files changed, 272 insertions(+), 53 deletions(-) create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Delivery.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DeliveryAssembler.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Order.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java rename org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/{DrinkOrder.java => Waiter.java} (65%) diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java index e369771215..d542affd15 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Barista.java @@ -22,16 +22,17 @@ import org.springframework.stereotype.Component; /** * @author Mark Fisher + * @author Marius Bogoevici */ @Component public class Barista { private long hotDrinkDelay = 5000; - private long coldDrinkDelay = 1000; + private long coldDrinkDelay = 1000; private AtomicInteger hotDrinkCounter = new AtomicInteger(); - + private AtomicInteger coldDrinkCounter = new AtomicInteger(); @@ -43,24 +44,32 @@ public class Barista { this.coldDrinkDelay = coldDrinkDelay; } - public void prepareHotDrink(Drink drink) { + public Drink prepareHotDrink(OrderItem orderItem) { try { Thread.sleep(this.hotDrinkDelay); System.out.println(Thread.currentThread().getName() - + " prepared hot drink #" + hotDrinkCounter.incrementAndGet() + ": " + drink); + + " 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 void prepareColdDrink(Drink drink) { + public Drink prepareColdDrink(OrderItem orderItem) { try { Thread.sleep(this.coldDrinkDelay); System.out.println(Thread.currentThread().getName() - + " prepared cold drink #" + coldDrinkCounter.incrementAndGet() + ": " + drink); + + " 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/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java index 337a46268c..8093d8aa43 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Cafe.java @@ -17,11 +17,11 @@ package org.springframework.integration.samples.cafe; import org.springframework.integration.channel.MessageChannel; -import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.MessageBuilder; /** * The entry point for {@link CafeDemo}. When the 'placeOrder' - * method is invoked, it passes the {@link DrinkOrder} as the payload of a + * method is invoked, it passes the {@link Order} 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'. @@ -37,8 +37,8 @@ public class Cafe { this.orderChannel = orderChannel; } - public void placeOrder(DrinkOrder order) { - this.orderChannel.send(new GenericMessage(order)); + public void placeOrder(Order order) { + this.orderChannel.send(MessageBuilder.fromPayload(order).build()); } } diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java index bc8558f366..52b31b0939 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/CafeDemo.java @@ -28,6 +28,7 @@ import org.springframework.context.support.FileSystemXmlApplicationContext; * OrderSplitter, DrinkRouter, and Barista classes). * * @author Mark Fisher + * @author Marius Bogoevici */ public class CafeDemo { @@ -40,12 +41,10 @@ public class CafeDemo { context = new ClassPathXmlApplicationContext("cafeDemo.xml", CafeDemo.class); } 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 < 100; i++) { + 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); } } diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Delivery.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Delivery.java new file mode 100644 index 0000000000..d32574bccc --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Delivery.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2008 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/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DeliveryAssembler.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DeliveryAssembler.java new file mode 100644 index 0000000000..3e83668188 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DeliveryAssembler.java @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2008 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.Aggregator; +import org.springframework.integration.annotation.MessageEndpoint; + +/** + * @author Marius Bogoevici + */ +@MessageEndpoint(input = "preparedDrinks",output = "deliveries") +public class DeliveryAssembler { + + @Aggregator + public Delivery prepareDelivery(List drinks) { + return new Delivery(drinks); + } + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java index 27d69a8c5c..5cd79c2ea0 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Drink.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 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. @@ -17,31 +17,34 @@ package org.springframework.integration.samples.cafe; /** - * @author Mark Fisher + * @author Marius Bogoevici */ public class Drink { - private DrinkType type; + private boolean iced; - private int shots = 1; + private int shots; - private boolean iced = false; + 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 Drink(DrinkType type, int shots, boolean iced) { - this.type = type; - this.shots = shots; - this.iced = iced; + public int getOrderNumber() { + return orderNumber; } - - public boolean isIced() { - return this.iced; - } - - public String toString() { - return ((this.iced) ? "iced " : "hot " ) + - this.shots + " shot " + this.type; - } + @Override + public String toString() { + return (iced?"Iced":"Hot") + " " + drinkType.toString() + ", " + shots + " shots."; + } } diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java index 9634d2e20e..2a04b1bec5 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkRouter.java @@ -26,8 +26,8 @@ import org.springframework.integration.annotation.Router; public class DrinkRouter { @Router - public String resolveDrinkChannel(Drink drink) { - return (drink.isIced()) ? "coldDrinks" : "hotDrinks"; + public String resolveOrderItemChannel(OrderItem orderItem) { + return (orderItem.isIced()) ? "coldDrinks" : "hotDrinks"; } } diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Order.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Order.java new file mode 100644 index 0000000000..2e308984db --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Order.java @@ -0,0 +1,48 @@ +/* + * 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 + * @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 getDrinks() { + return this.orderItems; + } + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java new file mode 100644 index 0000000000..0a48f70a98 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderItem.java @@ -0,0 +1,62 @@ +/* + * 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 + * @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/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java index c0f105db39..6e3ed9f4bd 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/OrderSplitter.java @@ -28,7 +28,7 @@ import org.springframework.integration.annotation.Splitter; public class OrderSplitter { @Splitter - public List split(DrinkOrder order) { + public List split(Order order) { return order.getDrinks(); } diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Waiter.java similarity index 65% rename from org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java rename to org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Waiter.java index c29a1a7c5c..eec89af3c8 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/DrinkOrder.java +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/Waiter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 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. @@ -16,22 +16,16 @@ package org.springframework.integration.samples.cafe; -import java.util.ArrayList; -import java.util.List; +import org.springframework.stereotype.Component; /** - * @author Mark Fisher + * @author Marius Bogoevici */ -public class DrinkOrder { +@Component +public class Waiter { - private List drinks = new ArrayList(); - - public void addDrink(Drink drink) { - this.drinks.add(drink); - } - - public List getDrinks() { - return this.drinks; + public void serve(Delivery delivery) { + System.out.println("\nDelivering: \n" + delivery); } } diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml index c37d23538d..97c93f15fd 100644 --- a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/cafe/cafeDemo.xml @@ -1,5 +1,5 @@ - + + - + - + + + + + + +