From 0f8ef53d82e347542675191adf5b004d0ced73e0 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sat, 11 May 2013 15:46:54 +0200 Subject: [PATCH] Waiters now deliver cooked orders --- .../tuwien/sbc/valesriegler/cook/JMSCook.java | 2 +- .../group/jms/JMSGroupConnector.java | 7 ++ .../sbc/valesriegler/waiter/Waiter.java | 5 ++ .../waiter/actions/DeliverOrder.java | 31 ++++++++ .../jms/messageListeners/CookedOrders.java | 78 +++++++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/actions/DeliverOrder.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java index f5b7ea4..408b9fe 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java @@ -39,7 +39,7 @@ public class JMSCook implements HasId { connection.start(); Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrderedPizzas")); + MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrdersToCook")); consWantToSit.setMessageListener(new OrdersToCook(this)); } catch (JMSException e) { log.error("EXCEPTION!", e); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java index 42a3505..b379ac6 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java @@ -25,6 +25,7 @@ import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; +import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; /** * This class handles the communication with other processes using JMS. @@ -70,6 +71,12 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(orderresponse.getGroupdata().getId()); g.orderReceived(orderresponse.getWaiterId()); return; + } else if (obj instanceof DeliverOrder) { + DeliverOrder deliverorder = (DeliverOrder) obj; + log.debug("Received: " + deliverorder); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(deliverorder.getGroupdata().getId()); + g.deliverPizzas(deliverorder.getGroupdata().getOrder(), deliverorder.getWaiterId()); + return; } else if (obj instanceof PayResponse) { PayResponse payresponse = (PayResponse) obj; log.debug("Received: " + payresponse); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java index 50cd859..76aaf6e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java @@ -10,6 +10,7 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.CookedOrders; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToOrder; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToPay; import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToSitAtTable; @@ -48,6 +49,10 @@ public class Waiter implements Serializable { MessageConsumer consWantToOrder = sessWantToOrder.createConsumer(sessWantToOrder.createQueue("WantToOrder")); consWantToOrder.setMessageListener(new WantToOrder(this)); + Session sessCookedOrders = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consCookedOrders = sessCookedOrders.createConsumer(sessCookedOrders.createQueue("CookedOrders")); + consCookedOrders.setMessageListener(new CookedOrders(this)); + Session sessWantToPay = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consWantToPay = sessWantToPay.createConsumer(sessWantToPay.createQueue("WantToPay")); consWantToPay.setMessageListener(new WantToPay(this)); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/actions/DeliverOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/actions/DeliverOrder.java new file mode 100644 index 0000000..5b9acd6 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/actions/DeliverOrder.java @@ -0,0 +1,31 @@ +package at.ac.tuwien.sbc.valesriegler.waiter.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +/** + * bring the order to the group. + * + * @author jan + * + */ +public class DeliverOrder extends AbstractAction implements Serializable { + private final int waiterId; + + public DeliverOrder(GroupData groupdata, int waiterId) { + super(groupdata); + this.waiterId = waiterId; + } + + public int getWaiterId() { + return waiterId; + } + + @Override + public String toString() { + return "DeliverOrder [waiterId=" + waiterId + "]"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java new file mode 100644 index 0000000..4ee0d58 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java @@ -0,0 +1,78 @@ +package at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; +import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; + +/** + * Listener listening on the CookedOrders MQ, handling all incomming messages. + * + * @author jan + * + */ +public class CookedOrders implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(CookedOrders.class); + private final Waiter waiter; + + public CookedOrders(Waiter waiter) { + this.waiter = waiter; + } + + @Override + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof OrderInfo) { + OrderInfo orderinfo = (OrderInfo) obj; + log.debug("Received: " + orderinfo); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // inform Group + Pizzeria + MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); + informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + DeliverOrder deliverorder = new DeliverOrder(orderinfo.getGroupdata(), waiter.getId()); + informGroup.send(session.createObjectMessage(deliverorder)); + + MessageProducer informPizzeria = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzeria.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzeria.send(session.createObjectMessage(deliverorder)); + + session.close(); + connection.close(); + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } +} -- 2.43.0