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