From 587673dccf5f3d1ca329622400df3fe3f53223c6 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sun, 5 May 2013 20:17:56 +0200 Subject: [PATCH] Implemented JMS OrderRequest sent by Group. --- .../tuwien/sbc/valesriegler/group/Group.java | 14 +-- .../group/actions/OrderRequest.java | 12 ++ .../group/jms/JMSGroupConnector.java | 40 ++++-- .../jms/messageListeners/WantToOrder.java | 114 ++++++++++-------- 4 files changed, 109 insertions(+), 71 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java index 20acf58..e7be93c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -3,6 +3,7 @@ package at.ac.tuwien.sbc.valesriegler.group; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; @@ -38,10 +39,7 @@ public class Group implements Runnable { GroupAgent.getInstance().getGroupcomm().send(new TableRequest(groupData)); break; case SITTING: - // MessageProducer wantToOrder = - // session.createProducer(session.createQueue("WantToOrder")); - // wantToOrder.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // wantToOrder.send(session.createObjectMessage(this)); + GroupAgent.getInstance().getGroupcomm().send(new OrderRequest(groupData)); break; case ORDERED: break; // do nothing. @@ -70,7 +68,7 @@ public class Group implements Runnable { // lets go to the pizzaria. public void goGrabSomeFood() { - log.debug("goGrabSomeFood():" + this); + log.debug("goGrabSomeFood(): " + this); groupData.setState(GroupState.WAITING); new Thread(this).start(); } @@ -81,7 +79,7 @@ public class Group implements Runnable { groupData.setTable(t); groupData.setTableWaiter(waiterID); new Thread(this).start(); - log.debug("assignTable():" + this); + log.debug("assignTable(): " + this); } // ask the group to send their order. @@ -89,7 +87,7 @@ public class Group implements Runnable { groupData.setState(GroupState.ORDERED); groupData.setOrderWaiter(waiter); new Thread(this).start(); - log.debug("askForOrder():" + this); + log.debug("askForOrder(): " + this); } // pizzas are here! nom pizzas! @@ -98,6 +96,6 @@ public class Group implements Runnable { groupData.setOrder(order); groupData.setPizzaWaiter(waiter); new Thread(this).start(); - log.debug("bringOrderedPizzas():" + this); + log.debug("bringOrderedPizzas(): " + this); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java new file mode 100644 index 0000000..c5f9f21 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderRequest.java @@ -0,0 +1,12 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.types.GroupData; + +public class OrderRequest extends AbstractAction implements Serializable { + public OrderRequest(GroupData groupdata) { + super(groupdata); + } + +} 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 812461a..afb2bb2 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 @@ -18,6 +18,8 @@ import at.ac.tuwien.sbc.valesriegler.group.AbstractGroupConnector; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.group.actions.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; /** @@ -31,6 +33,7 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message private Connection connection; + @Override public void init() { try { connection = new ActiveMQConnectionFactory("tcp://localhost:61616").createConnection(); @@ -66,19 +69,36 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message } } + @Override public void send(AbstractAction request) { try { - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); - wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - wantToSitAtTable.send(session.createObjectMessage(request)); - session.close(); + if (request instanceof TableRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToSitAtTable.send(session.createObjectMessage(request)); + session.close(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzariaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - session.close(); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzariaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + session.close(); + return; + } else if (request instanceof OrderRequest) { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToOrder")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToSitAtTable.send(session.createObjectMessage(request)); + session.close(); + + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzariaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + session.close(); + return; + } } catch (JMSException e) { log.error("EXCEPTION!", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java index 88a67d9..47a9e3c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java @@ -1,11 +1,22 @@ 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.group.actions.OrderRequest; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; +import at.ac.tuwien.sbc.valesriegler.types.Order; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; public class WantToOrder implements MessageListener { @@ -17,58 +28,55 @@ public class WantToOrder implements MessageListener { } @Override - public void onMessage(Message m) { - // try { - // m.acknowledge(); - // if (m instanceof ObjectMessage) { - // ObjectMessage pizzaMessage = (ObjectMessage) m; - // Object data = pizzaMessage.getObject(); - // - // if (data instanceof Group) { - // Group group = (Group) data; - log.debug("Received: " + m); - // - // // 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); - // - // MessageProducer prodOP = - // session.createProducer(session.createQueue("OrderedPizzas")); - // prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // - // // Create the queue for the cooks. - // Order o = group.askForOrder(waiter); - // for (PizzaType p : o.getOrdered()) { - // ObjectMessage message = session.createObjectMessage(p); - // prodOP.send(message); - // } - // - // // Also put the order in the Pending order queue for the waiter to - // // check. - // MessageProducer prodPending = - // session.createProducer(session.createQueue("PendingOrders")); - // prodPending.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - // ObjectMessage message = session.createObjectMessage(o); - // prodPending.send(message); - // - // session.close(); - // connection.close(); - // } else { - // log.warn("Received unknown Object: " + data); - // } - // } else { - // log.warn("Received unknown Message: " + m); - // } - // } catch (JMSException e) { - // log.error("EXCEPTION!", e); - // } catch (InterruptedException e) { - // log.error("EXCEPTION!", e); - // } + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof TableRequest) { + OrderRequest orderrequest = (OrderRequest) obj; + log.debug("Received: " + orderrequest); + + // 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); + + MessageProducer prodOP = session.createProducer(session.createQueue("OrderedPizzas")); + prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + // Create the queue for the cooks. + Order o = orderrequest.getGroupdata().getOrder(); + for (PizzaType p : o.getOrderedPizzas()) { + ObjectMessage message = session.createObjectMessage(p); + prodOP.send(message); + } + + // Also put the order in the Pending order queue for the waiter to + // check. + MessageProducer prodPending = session.createProducer(session.createQueue("PendingOrders")); + prodPending.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + ObjectMessage message = session.createObjectMessage(o); + prodPending.send(message); + + 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