Adding aggregation capabilities to the CafeDemo sample, renaming a few domain objects

This commit is contained in:
Marius Bogoevici
2008-08-23 16:44:24 +00:00
parent 8a5b0c8832
commit eba716e65b
12 changed files with 272 additions and 53 deletions

View File

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

View File

@@ -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 '<code>placeOrder</code>'
* 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<DrinkOrder>(order));
public void placeOrder(Order order) {
this.orderChannel.send(MessageBuilder.fromPayload(order).build());
}
}

View File

@@ -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);
}
}

View File

@@ -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<Drink> deliveredDrinks;
private int orderNumber;
public Delivery(List<Drink> deliveredDrinks) {
assert (deliveredDrinks.size() > 0);
this.deliveredDrinks = deliveredDrinks;
this.orderNumber = deliveredDrinks.get(0).getOrderNumber();
}
public int getOrderNumber() {
return orderNumber;
}
public List<Drink> 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();
}
}

View File

@@ -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<Drink> drinks) {
return new Delivery(drinks);
}
}

View File

@@ -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.";
}
}

View File

@@ -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";
}
}

View File

@@ -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<OrderItem> orderItems = new ArrayList<OrderItem>();
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<OrderItem> getDrinks() {
return this.orderItems;
}
}

View File

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

View File

@@ -28,7 +28,7 @@ import org.springframework.integration.annotation.Splitter;
public class OrderSplitter {
@Splitter
public List<Drink> split(DrinkOrder order) {
public List<OrderItem> split(Order order) {
return order.getDrinks();
}

View File

@@ -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<Drink> drinks = new ArrayList<Drink>();
public void addDrink(Drink drink) {
this.drinks.add(drink);
}
public List<Drink> getDrinks() {
return this.drinks;
public void serve(Delivery delivery) {
System.out.println("\nDelivering: \n" + delivery);
}
}

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
@@ -19,13 +19,21 @@
<channel id="drinks"/>
<channel id="coldDrinks"/>
<channel id="hotDrinks"/>
<channel id="preparedDrinks"/>
<channel id="deliveries"/>
<service-activator input-channel="coldDrinks" ref="barista" method="prepareColdDrink"/>
<service-activator input-channel="coldDrinks" ref="barista" method="prepareColdDrink" output-channel="preparedDrinks"/>
<service-activator input-channel="hotDrinks" ref="barista" method="prepareHotDrink"/>
<service-activator input-channel="hotDrinks" ref="barista" method="prepareHotDrink" output-channel="preparedDrinks"/>
<service-activator input-channel="deliveries" ref="waiter" method="serve"/>
<beans:bean id="cafe" class="org.springframework.integration.samples.cafe.Cafe">
<beans:property name="orderChannel" ref="orders"/>
</beans:bean>
<channel-adapter channel="deliveries" target="deliveryOutput"/>
<console-target id="deliveryOutput"/>
</beans:beans>