From 0ed6bbb0d279ef9e77e0867ebc77b493cc4cee7d Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sun, 12 May 2013 23:17:59 +0200 Subject: [PATCH] added commandline argument parsing to allow custom IDs. --- .../sbc/valesriegler/cook/CookAgent.java | 37 +++++++++++++++++++ .../valesriegler/cook/{ => jms}/JMSCook.java | 13 ++----- .../jms/messageListeners/OrdersToCook.java | 4 +- .../tuwien/sbc/valesriegler/types/Pizza.java | 4 +- .../sbc/valesriegler/waiter/WaiterAgent.java | 37 +++++++++++++++++++ .../{Waiter.java => jms/JMSWaiter.java} | 13 ++----- .../jms/messageListeners/CookedOrders.java | 6 +-- .../jms/messageListeners/WantToOrder.java | 6 +-- .../jms/messageListeners/WantToPay.java | 6 +-- .../messageListeners/WantToSitAtTable.java | 6 +-- 10 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/CookAgent.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/cook/{ => jms}/JMSCook.java (80%) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/{Waiter.java => jms/JMSWaiter.java} (88%) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/CookAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/CookAgent.java new file mode 100644 index 0000000..0e5356e --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/CookAgent.java @@ -0,0 +1,37 @@ +package at.ac.tuwien.sbc.valesriegler.cook; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; + +/** + * CookAgent parses the arguments and runs the JMS/XVSMCook with the given ID. + * + * @author jan + * + */ +public class CookAgent { + private static final String USAGE = "This application needs exactly 2 parameters: <\"XVSM\"|\"JMS\"> "; + private static final Logger log = LoggerFactory.getLogger(CookAgent.class); + + public static void main(String[] args) throws Exception { + if (args.length != 2) { + throw new IllegalArgumentException(USAGE); + } + + String mw = args[0]; + int parsedId = 0; + try { + parsedId = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + log.error(USAGE); + return; + } + + if ("JMS".equalsIgnoreCase(mw)) { + new JMSCook(parsedId); + } + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java similarity index 80% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java index 408b9fe..32aa897 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.sbc.valesriegler.cook; +package at.ac.tuwien.sbc.valesriegler.cook.jms; import javax.jms.Connection; import javax.jms.JMSException; @@ -13,20 +13,15 @@ import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook; /** - * (JMS)Cook is far too primitive to require any abstraction. It would take more - * time to it, than to code it twice. + * JMSCook registers all the Listeners. * * @author jan * */ public class JMSCook implements HasId { private static final Logger log = LoggerFactory.getLogger(JMSCook.class); - private static int nextID = 0; - final private int id; - public static void main(String[] args) throws Exception { - new JMSCook(++nextID); - } + final private int id; public JMSCook(int id) { this.id = id; @@ -38,7 +33,7 @@ public class JMSCook implements HasId { Connection connection = connectionFactory.createConnection(); connection.start(); - Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + Session sessWantToSit = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrdersToCook")); consWantToSit.setMessageListener(new OrdersToCook(this)); } catch (JMSException e) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java index 31e4a68..042cef0 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/OrdersToCook.java @@ -13,8 +13,8 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.cook.JMSCook; import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.cook.jms.JMSCook; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Pizza; @@ -39,7 +39,6 @@ public class OrdersToCook implements MessageListener { public void onMessage(Message msg) { try { synchronized (cook) { - msg.acknowledge(); if (msg instanceof ObjectMessage) { ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); @@ -93,6 +92,7 @@ public class OrdersToCook implements MessageListener { } else { log.warn("Received unknown Message: " + msg); } + msg.acknowledge(); } } catch (JMSException e) { log.error("EXCEPTION!", e); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java index add3e5e..ef30948 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java @@ -3,7 +3,7 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** * this is a PIZZA! omnomnomnom @@ -12,7 +12,7 @@ import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; * */ public class Pizza extends PizzaOrder implements Serializable { - public Waiter deliveryAgent; + public JMSWaiter deliveryAgent; /** * Yes that's funny! the pizza has a field idOfOrder although PizzaOrder diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java new file mode 100644 index 0000000..6540656 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/WaiterAgent.java @@ -0,0 +1,37 @@ +package at.ac.tuwien.sbc.valesriegler.waiter; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; + +/** + * A waiter is a person who does all the stuff and gets lousy tips in return. + * The WaiterAgent runs the XVSM/JMSWaiter to do all the Waiter stuff. + * + * @author jan + * + */ +public class WaiterAgent { + private static final String USAGE = "This application needs exactly 2 parameters: <\"XVSM\"|\"JMS\"> "; + private static final Logger log = LoggerFactory.getLogger(WaiterAgent.class); + + public static void main(String[] args) { + if (args.length != 2) { + throw new IllegalArgumentException(USAGE); + } + + String mw = args[0]; + int parsedId = 0; + try { + parsedId = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + log.error(USAGE); + return; + } + + if ("JMS".equalsIgnoreCase(mw)) { + new JMSWaiter(parsedId); + } + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java similarity index 88% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java index 76aaf6e..39b4479 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/JMSWaiter.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.sbc.valesriegler.waiter; +package at.ac.tuwien.sbc.valesriegler.waiter.jms; import java.io.Serializable; @@ -22,16 +22,11 @@ import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToSitAtTabl * @author jan * */ -public class Waiter implements Serializable { - private static final Logger log = LoggerFactory.getLogger(Waiter.class); - private static int nextID = 0; +public class JMSWaiter implements Serializable { + private static final Logger log = LoggerFactory.getLogger(JMSWaiter.class); final private int id; - public static void main(String[] args) { - new Waiter(++nextID); - } - - public Waiter(int id) { + public JMSWaiter(int id) { this.id = id; log.info("I AM A WAITER WITH ID {}", id); 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 index 7c06318..4ad83e7 100644 --- 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 @@ -15,8 +15,8 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; -import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** * Listener listening on the CookedOrders MQ, handling all incomming messages. @@ -26,9 +26,9 @@ import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; */ public class CookedOrders implements MessageListener { private static final Logger log = LoggerFactory.getLogger(CookedOrders.class); - private final Waiter waiter; + private final JMSWaiter waiter; - public CookedOrders(Waiter waiter) { + public CookedOrders(JMSWaiter waiter) { this.waiter = waiter; } 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 a49a0d0..396cbd1 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 @@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; -import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** * Listener listening on the WantToOrder MQ, handling all incomming messages. @@ -25,9 +25,9 @@ import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; */ public class WantToOrder implements MessageListener { private static final Logger log = LoggerFactory.getLogger(WantToOrder.class); - private final Waiter waiter; + private final JMSWaiter waiter; - public WantToOrder(Waiter waiter) { + public WantToOrder(JMSWaiter waiter) { this.waiter = waiter; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java index 0658280..6a8e858 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java @@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; -import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** * Listener listening on the WantToPay MQ, handling all incomming messages. @@ -25,9 +25,9 @@ import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; */ public class WantToPay implements MessageListener { private static final Logger log = LoggerFactory.getLogger(WantToPay.class); - private final Waiter waiter; + private final JMSWaiter waiter; - public WantToPay(Waiter waiter) { + public WantToPay(JMSWaiter waiter) { this.waiter = waiter; } 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 774cafa..2caed2f 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 @@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; import at.ac.tuwien.sbc.valesriegler.types.Table; -import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.JMSWaiter; /** * Listener listening on the WantToSitAtTable MQ, handling all incomming @@ -28,9 +28,9 @@ import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; */ public class WantToSitAtTable implements MessageListener { private static final Logger log = LoggerFactory.getLogger(WantToSitAtTable.class); - private final Waiter waiter; + private final JMSWaiter waiter; - public WantToSitAtTable(Waiter waiter) { + public WantToSitAtTable(JMSWaiter waiter) { this.waiter = waiter; } -- 2.43.0