From db288ad5b86c6e9bfc34b2b2861d6156087e5769 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Fri, 26 Apr 2013 12:49:35 +0200 Subject: [PATCH] early activemq producer/consumer implementation. --- .classpath | 6 +- pom.xml | 17 ++++- .../tuwien/sbc/valesriegler/common/Pizza.java | 30 +++++++++ .../sbc/valesriegler/jms/cook/Cook.java | 53 +++++++++++++-- .../sbc/valesriegler/jms/waiter/Waiter.java | 64 +++++++++++++++++-- 5 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java diff --git a/.classpath b/.classpath index 8dee4f3..e43402f 100644 --- a/.classpath +++ b/.classpath @@ -22,11 +22,15 @@ + + + + + - diff --git a/pom.xml b/pom.xml index cfb056a..63c6872 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,11 @@ logback-classic 1.0.7 + + org.apache.activemq + activemq-all + 5.8.0 + @@ -36,8 +41,16 @@ maven-compiler-plugin 2.3.2 - 1.6 - 1.6 + 1.7 + 1.7 + + + + org.apache.activemq.tooling + activemq-maven-plugin + 5.8.0 + + broker:(tcp://localhost:61616)?useJmx=true&persistent=false diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java new file mode 100644 index 0000000..7b5dff6 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java @@ -0,0 +1,30 @@ +package at.ac.tuwien.sbc.valesriegler.common; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.jms.cook.Cook; +import at.ac.tuwien.sbc.valesriegler.jms.waiter.Waiter; + +public class Pizza implements Serializable { + final private PizzaType typ; + + // debugzeugs + private static int nextID = 0; + final private int id; + final public Cook producer; + public Waiter deliveryAgent; + + // private Person consumer; + + public Pizza(PizzaType typ, Cook producer) { + id = ++nextID; + this.typ = typ; + this.producer = producer; + } + + @Override + public String toString() { + return "Pizza [typ=" + typ + ", id=" + id + ", producer=" + producer + "]"; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java index df34dd4..80e277f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java @@ -1,18 +1,57 @@ package at.ac.tuwien.sbc.valesriegler.jms.cook; +import java.io.Serializable; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +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; -public class Cook { +import at.ac.tuwien.sbc.valesriegler.common.Pizza; +import at.ac.tuwien.sbc.valesriegler.common.PizzaType; + +public class Cook implements Serializable { private static final Logger log = LoggerFactory.getLogger(Cook.class); + private static int nextID = 0; + final private int id; - public static void main(String[] args) { - if (args.length != 1) { - throw new IllegalArgumentException("Cook needs exactly one parameter: ID"); - } + public static void main(String[] args) throws Exception { + new Cook(++nextID); + } - int id = Integer.parseInt(args[0]); - log.info("I AM A COOK WITH ID {}", id); + public Cook(int id) { + this.id = id; + log.info("I AM A COOK WITH ID {}", this.id); + produce(PizzaType.CARDINALE); } + public void produce(PizzaType pizzatype) { + try { + // Connecting to the Broker and to the output queue + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageProducer producer = session.createProducer(session.createQueue("CookedPizzas")); + producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + // Create a messages + Pizza pizza = new Pizza(pizzatype, this); + ObjectMessage message = session.createObjectMessage(pizza); + producer.send(message); + + // Clean up + session.close(); + connection.close(); + } catch (Exception e) { + log.error("Caught: ", e); + e.printStackTrace(); + } + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java index 6ad521a..e236ff1 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java @@ -1,19 +1,71 @@ package at.ac.tuwien.sbc.valesriegler.jms.waiter; +import java.io.Serializable; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.ObjectMessage; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class Waiter { +import at.ac.tuwien.sbc.valesriegler.common.Pizza; + +public class Waiter implements Serializable { private static final Logger log = LoggerFactory.getLogger(Waiter.class); + private static int nextID = 0; + final private int id; public static void main(String[] args) { - if(args.length != 1) { - throw new IllegalArgumentException("Waiter needs exactly one parameter: ID"); - } - - int id = Integer.parseInt(args[0]); + new Waiter(++nextID); + } + + public Waiter(int id) { + this.id = id; log.info("I AM A WAITER WITH ID {}", id); + deliver(); + } + public void deliver() { + try { + // Connecting to the Broker and to the output queue + 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("CookedPizzas")); + + // Wait for a Pizza + Message message = consumer.receive(10000); + + if (message instanceof ObjectMessage) { + ObjectMessage pizzaMessage = (ObjectMessage) message; + Object data = pizzaMessage.getObject(); + if (data instanceof Pizza) { + Pizza pizza = (Pizza) data; + System.out.println("Received: " + pizza); + } else { + System.out.println("Received unknown Object: " + data); + } + } else { + System.out.println("Received unknown Message: " + message); + } + + consumer.close(); + session.close(); + connection.close(); + } catch (Exception e) { + log.error("Caught: ", e); + e.printStackTrace(); + } } + public synchronized void onException(JMSException ex) { + System.out.println("JMS Exception occured. Shutting down client."); + } } -- 2.43.0