]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java
Merge it!
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / cook / jms / JMSCook.java
1 package at.ac.tuwien.sbc.valesriegler.cook.jms;
2
3 import at.ac.tuwien.sbc.valesriegler.common.HasId;
4 import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.DeliveryOrdersToCook;
5 import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook;
6 import org.apache.activemq.ActiveMQConnectionFactory;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import javax.jms.Connection;
11 import javax.jms.JMSException;
12 import javax.jms.MessageConsumer;
13 import javax.jms.Session;
14
15 /**
16  * JMSCook registers all the Listeners.
17  * 
18  * @author jan
19  * 
20  */
21 public class JMSCook implements HasId {
22         private static final Logger log = LoggerFactory.getLogger(JMSCook.class);
23         private final String CONNECTSTRING;
24         final private int id;
25
26         public JMSCook(String jmsURL, int id) {
27                 CONNECTSTRING = jmsURL;
28                 this.id = id;
29                 log.info("I AM A JMSCook WITH ID {}", this.id);
30
31                 try {
32                         // Connecting to the Broker and to the output queue
33                         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTSTRING);
34                         Connection connection = connectionFactory.createConnection();
35                         connection.start();
36
37                         Session sessOrdersToCook = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
38                         MessageConsumer consOrdersToCook = sessOrdersToCook.createConsumer(sessOrdersToCook.createQueue("OrdersToCook"));
39                         consOrdersToCook.setMessageListener(new OrdersToCook(this));
40
41                         Session sessDeliveryOrdersToCook = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
42                         MessageConsumer consDeliveryOrdersToCook = sessDeliveryOrdersToCook.createConsumer(sessDeliveryOrdersToCook
43                                         .createQueue("DeliveryOrdersToCook"));
44                         consDeliveryOrdersToCook.setMessageListener(new DeliveryOrdersToCook(this));
45                 } catch (JMSException e) {
46                         log.error("EXCEPTION!", e);
47                 }
48         }
49
50         public String getCONNECTSTRING() {
51                 return CONNECTSTRING;
52         }
53
54         @Override
55         public int getId() {
56                 return id;
57         }
58 }