]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/JMSCook.java
changed some coce in balancer + pom to be runnable inside jenkins and added some...
[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 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.common.HasId;
13 import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.DeliveryOrdersToCook;
14 import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.OrdersToCook;
15
16 /**
17  * JMSCook registers 2 Listeners. We do this to prevent a starvation of groups inside the pizzeria. But on the other
18  * hand we dont require all pizzas inside the pizzeria to be cooked before we start cooking DeliveryOrders.
19  * 
20  * @author jan
21  * 
22  */
23 public class JMSCook implements HasId {
24         private static final Logger log = LoggerFactory.getLogger(JMSCook.class);
25         private final String CONNECTSTRING;
26         final private int id;
27
28         public JMSCook(String jmsURL, int id) {
29                 CONNECTSTRING = jmsURL;
30                 this.id = id;
31                 log.info("I AM A JMSCook WITH ID {}", this.id);
32
33                 try {
34                         // Connecting to the Broker and to the output queue
35                         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTSTRING);
36                         Connection connection = connectionFactory.createConnection();
37                         connection.start();
38
39                         Session sessOrdersToCook = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
40                         MessageConsumer consOrdersToCook = sessOrdersToCook.createConsumer(sessOrdersToCook.createQueue("OrdersToCook"));
41                         consOrdersToCook.setMessageListener(new OrdersToCook(this));
42
43                         Session sessDeliveryOrdersToCook = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
44                         MessageConsumer consDeliveryOrdersToCook = sessDeliveryOrdersToCook.createConsumer(sessDeliveryOrdersToCook
45                                         .createQueue("DeliveryOrdersToCook"));
46                         consDeliveryOrdersToCook.setMessageListener(new DeliveryOrdersToCook(this));
47                 } catch (JMSException e) {
48                         log.error("EXCEPTION!", e);
49                 }
50         }
51
52         public String getCONNECTSTRING() {
53                 return CONNECTSTRING;
54         }
55
56         @Override
57         public int getId() {
58                 return id;
59         }
60 }