From 93aedfb6d1319c91b1dfa17d3827211c03b9b041 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sun, 12 May 2013 04:31:10 +0200 Subject: [PATCH] fixed some synchronisation problems in jms. --- .../group/actions/OrderRequest.java | 2 +- .../group/jms/JMSGroupConnector.java | 10 --- .../valesriegler/pizzeria/PizzeriaAgent.java | 52 +++++++-------- .../gui/tablemodels/TablesOverviewModel.java | 18 ++++- .../pizzeria/jms/JMSPizzeriaConnector.java | 17 ++--- .../messageListeners/WantToSitAtTable.java | 65 ++++++++++--------- 6 files changed, 85 insertions(+), 79 deletions(-) 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 index e067674..886ab92 100644 --- 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 @@ -18,7 +18,7 @@ public class OrderRequest extends AbstractAction implements Serializable { @Override public String toString() { - return "OrderRequest []"; + return "OrderRequest [getGroupdata()=" + getGroupdata() + "]"; } } 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 b379ac6..a062fec 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 @@ -101,11 +101,6 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToSitAtTable.send(session.createObjectMessage(request)); - - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - session.close(); return; } else if (request instanceof OrderRequest) { @@ -113,11 +108,6 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message MessageProducer wantToOrder = session.createProducer(session.createQueue("WantToOrder")); wantToOrder.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToOrder.send(session.createObjectMessage(request)); - - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - session.close(); return; } else if (request instanceof PayRequest) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java index 6b1f146..9115ac6 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java @@ -1,27 +1,21 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; -import java.io.Serializable; -import java.util.ArrayList; import java.util.List; import javax.swing.SwingUtilities; -import org.mozartspaces.core.Entry; -import org.mozartspaces.core.MzsConstants.RequestTimeout; -import org.mozartspaces.notifications.Notification; -import org.mozartspaces.notifications.NotificationListener; -import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.JMSPizzeriaConnector; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; @@ -47,13 +41,14 @@ public class PizzeriaAgent { private PizzasOfOrderModel pizzasModel; private PizzeriaAgentXVSM xvsm; + private JMSPizzeriaConnector jmspc; public static void main(String[] args) {// TODO: remove hardcoding of JMS. // if (args.length != 1) { // throw new IllegalArgumentException(USAGE); // } else { -// String mom = "XVSM";// args[0]; - String mom = Util.useJMS ? "JMS" : "XVSM"; + // String mom = "XVSM";// args[0]; + String mom = Util.useJMS ? "JMS" : "XVSM"; log.info(mom); pizzeriaAgent = new PizzeriaAgent(); switch (mom) { @@ -75,20 +70,21 @@ public class PizzeriaAgent { } private void initJMS() { - // do what you have to do + jmspc = new JMSPizzeriaConnector(); + jmspc.init(); } private void initXVSM() { - xvsm = new PizzeriaAgentXVSM(); - - xvsm.listenForFreeTables(); - xvsm.listenForTakenOrders(); - xvsm.listenForWaitingGroups(); - xvsm.listenForPizzasInPreparation(); - xvsm.listenForDeliveredOrders(); - xvsm.listenForPreparedPizzas(); - xvsm.listenForPayment(); - xvsm.listenForOccupiedTables(); + xvsm = new PizzeriaAgentXVSM(); + + xvsm.listenForFreeTables(); + xvsm.listenForTakenOrders(); + xvsm.listenForWaitingGroups(); + xvsm.listenForPizzasInPreparation(); + xvsm.listenForDeliveredOrders(); + xvsm.listenForPreparedPizzas(); + xvsm.listenForPayment(); + xvsm.listenForOccupiedTables(); } private void initGUI() { @@ -104,9 +100,13 @@ public class PizzeriaAgent { @Override public void freeTablesCreated(List tables) { - if(!Util.useJMS) { - xvsm.sendFreeTablesToContainer(tables); - } + if (!Util.useJMS) { + xvsm.sendFreeTablesToContainer(tables); + } else { + for (Table t : tables) { + jmspc.send(new TableNew(t)); + } + } } }); frame.start(); @@ -122,7 +122,7 @@ public class PizzeriaAgent { waitersModel = new WaitersOfOrderModel(); pizzasModel = new PizzasOfOrderModel(); } - + public static PizzeriaAgent getInstance() { return pizzeriaAgent; } @@ -146,7 +146,7 @@ public class PizzeriaAgent { public PizzasOfOrderModel getPizzasModel() { return pizzasModel; } - + public interface TablesCreatedHandler { public void freeTablesCreated(List
tables); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java index c85e0ca..1abb283 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java @@ -61,7 +61,8 @@ public class TablesOverviewModel extends TableModel
{ tables.add(table); } - if(Util.useJMS) setItems(tables); + if (Util.useJMS) + setItems(tables); return tables; } @@ -73,7 +74,7 @@ public class TablesOverviewModel extends TableModel
{ public int getTableIdOfGroup(GroupData group) { synchronized (items) { for (Table table : items.values()) { - if(table.getGroupId() == group.getId()) { + if (table.getGroupId() == group.getId()) { return table.getId(); } } @@ -81,9 +82,20 @@ public class TablesOverviewModel extends TableModel
{ throw new RuntimeException("Could not find table with the wanted group"); } + public Table getTableById(int tableId) { + synchronized (items) { + for (Table table : items.values()) { + if (table.getId() == tableId) { + return table; + } + } + } + throw new RuntimeException("Could not find table with the wanted group"); + } + public void makeTableFree(int tableId) { items.put(tableId, new Table(tableId)); - + fireTableDataChanged(); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java index 132c5a2..e7d91ce 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java @@ -15,12 +15,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; -import at.ac.tuwien.sbc.valesriegler.group.Group; -import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; -import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; import at.ac.tuwien.sbc.valesriegler.pizzeria.AbstractPizzeriaConnector; +import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; +import at.ac.tuwien.sbc.valesriegler.types.Table; /** * This class handles the communication with other processes using JMS. @@ -39,7 +38,7 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M connection = new ActiveMQConnectionFactory("tcp://localhost:61616").createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageConsumer consumer = session.createConsumer(session.createQueue("PizzariaConnector")); + MessageConsumer consumer = session.createConsumer(session.createQueue("PizzeriaConnector")); consumer.setMessageListener(this); } catch (JMSException e) { log.error("EXCEPTION!", e); @@ -57,12 +56,14 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M if (obj instanceof TableResponse) { TableResponse tablerresponse = (TableResponse) obj; log.debug("Received: " + tablerresponse); - Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(tablerresponse.getGroupdata().getId()); - g.assignTable(tablerresponse.getTable(), tablerresponse.getWaiterId()); + Table t = PizzeriaAgent.getInstance().getTablesModel().getTableById(tablerresponse.getTable().getId()); + t.setGroupId(tablerresponse.getGroupdata().getId()); return; - } else if (obj instanceof OrderResponse) { - } + // else if (obj instanceof OrderResponse) { + // + // } + log.warn("Unknown message received!" + obj); } log.warn("Unknown messagetype received!"); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java index f5f6dad..774cafa 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java @@ -37,55 +37,58 @@ public class WantToSitAtTable implements MessageListener { @Override public void onMessage(Message msg) { try { - synchronized (waiter) { - msg.acknowledge(); - if (msg instanceof ObjectMessage) { - ObjectMessage objMsg = (ObjectMessage) msg; - Object obj = objMsg.getObject(); + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); - if (obj instanceof TableRequest) { - TableRequest tablerequest = (TableRequest) obj; - log.debug("Received: " + tablerequest); + if (obj instanceof TableRequest) { + TableRequest tablerequest = (TableRequest) obj; + log.debug("Received: " + tablerequest); - // generate random delay - Thread.sleep((long) (Math.random() * 10000)); + // 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); + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - MessageConsumer consumer = session.createConsumer(session.createQueue("TablesFree")); - Message trm = consumer.receive(); - if (trm instanceof ObjectMessage) { - ObjectMessage tom = (ObjectMessage) trm; - Object tabledata = tom.getObject(); - - if (tabledata instanceof Table) { - session.close(); - Table table = (Table) tabledata; - System.out.println("Received: " + table); + MessageConsumer consumer = session.createConsumer(session.createQueue("TablesFree")); + Message trm = consumer.receive(); + if (trm instanceof ObjectMessage) { + ObjectMessage tom = (ObjectMessage) trm; + Object tabledata = tom.getObject(); + if (tabledata instanceof Table) { + session.close(); + Table table = (Table) tabledata; + System.out.println("Received: " + table); + synchronized (waiter) { TableResponse tr = new TableResponse(tablerequest.getGroupdata(), table, waiter.getId()); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer ret = session.createProducer(session.createQueue("GroupConnector")); ret.setDeliveryMode(DeliveryMode.NON_PERSISTENT); ret.send(session.createObjectMessage(tr)); - } else { - log.warn("Received unknown Object: " + tabledata); + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(tr)); + session.close(); } } else { - log.warn("Received unknown Message: " + trm); + log.warn("Received unknown Object: " + tabledata); } - session.close(); - connection.close(); } else { - log.warn("Received unknown Object: " + obj); + log.warn("Received unknown Message: " + trm); } + session.close(); + connection.close(); } else { - log.warn("Received unknown Message: " + msg); + log.warn("Received unknown Object: " + obj); } + } else { + log.warn("Received unknown Message: " + msg); } } catch (JMSException e) { log.error("EXCEPTION!", e); -- 2.43.0