]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java
prepare merge of jms branch.
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / jms / waiter / Waiter.java
1 package at.ac.tuwien.sbc.valesriegler.jms.waiter;
2
3 import java.io.Serializable;
4
5 import javax.jms.Connection;
6 import javax.jms.JMSException;
7 import javax.jms.Message;
8 import javax.jms.MessageConsumer;
9 import javax.jms.ObjectMessage;
10 import javax.jms.Session;
11
12 import org.apache.activemq.ActiveMQConnectionFactory;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import at.ac.tuwien.sbc.valesriegler.common.Pizza;
17
18 public class Waiter implements Serializable {
19         private static final Logger log = LoggerFactory.getLogger(Waiter.class);
20         private static int nextID = 0;
21         final private int id;
22
23         public static void main(String[] args) {
24                 new Waiter(++nextID);
25         }
26
27         public Waiter(int id) {
28                 this.id = id;
29                 log.info("I AM A WAITER WITH ID {}", id);
30                 deliver();
31         }
32
33         public void deliver() {
34                 try {
35                         // Connecting to the Broker and to the output queue
36                         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
37                         Connection connection = connectionFactory.createConnection();
38                         connection.start();
39                         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
40
41                         MessageConsumer consumer = session.createConsumer(session.createQueue("CookedPizzas"));
42
43                         // Wait for a Pizza
44                         Message message = consumer.receive(10000);
45
46                         if (message instanceof ObjectMessage) {
47                                 ObjectMessage pizzaMessage = (ObjectMessage) message;
48                                 Object data = pizzaMessage.getObject();
49                                 if (data instanceof Pizza) {
50                                         Pizza pizza = (Pizza) data;
51                                         System.out.println("Received: " + pizza);
52                                 } else {
53                                         System.out.println("Received unknown Object: " + data);
54                                 }
55                         } else {
56                                 System.out.println("Received unknown Message: " + message);
57                         }
58
59                         consumer.close();
60                         session.close();
61                         connection.close();
62                 } catch (Exception e) {
63                         log.error("Caught: ", e);
64                         e.printStackTrace();
65                 }
66         }
67
68         public synchronized void onException(JMSException ex) {
69                 System.out.println("JMS Exception occured.  Shutting down client.");
70         }
71 }