]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java
Merge branch 'master' of
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / cook / JMSCook.java
1 package at.ac.tuwien.sbc.valesriegler.cook;
2
3 import javax.jms.Connection;
4 import javax.jms.JMSException;
5 import javax.jms.MessageConsumer;
6 import javax.jms.Session;
7
8 import org.apache.activemq.ActiveMQConnectionFactory;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.CookRequestedPizza;
13
14 /**
15  * (JMS)Cook is far too primitive to require any abstraction. It would take more
16  * time to it, than to code it twice.
17  * 
18  * @author jan
19  * 
20  */
21 public class JMSCook {
22         private static final Logger log = LoggerFactory.getLogger(JMSCook.class);
23         private static int nextID = 0;
24         final private int id;
25
26         public static void main(String[] args) throws Exception {
27                 new JMSCook(++nextID);
28         }
29
30         public JMSCook(int id) {
31                 this.id = id;
32                 log.info("I AM A JMSCook WITH ID {}", this.id);
33
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
40                         Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
41                         MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrderedPizzas"));
42                         consWantToSit.setMessageListener(new CookRequestedPizza(this));
43                 } catch (JMSException e) {
44                         log.error("EXCEPTION!", e);
45                 }
46         }
47
48         public int getId() {
49                 return id;
50         }
51 }