]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java
Implemented JMS OrderRequest sent by Group.
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / jms / JMSGroupConnector.java
1 package at.ac.tuwien.sbc.valesriegler.group.jms;
2
3 import javax.jms.Connection;
4 import javax.jms.DeliveryMode;
5 import javax.jms.JMSException;
6 import javax.jms.Message;
7 import javax.jms.MessageConsumer;
8 import javax.jms.MessageListener;
9 import javax.jms.MessageProducer;
10 import javax.jms.ObjectMessage;
11 import javax.jms.Session;
12
13 import org.apache.activemq.ActiveMQConnectionFactory;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import at.ac.tuwien.sbc.valesriegler.group.AbstractGroupConnector;
18 import at.ac.tuwien.sbc.valesriegler.group.Group;
19 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
20 import at.ac.tuwien.sbc.valesriegler.group.actions.AbstractAction;
21 import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest;
22 import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest;
23 import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse;
24
25 /**
26  * This class handles the communication with other processes using JMS.
27  * 
28  * @author jan
29  * 
30  */
31 public class JMSGroupConnector extends AbstractGroupConnector implements MessageListener {
32         private static final Logger log = LoggerFactory.getLogger(JMSGroupConnector.class);
33
34         private Connection connection;
35
36         @Override
37         public void init() {
38                 try {
39                         connection = new ActiveMQConnectionFactory("tcp://localhost:61616").createConnection();
40                         connection.start();
41                         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
42                         MessageConsumer consWantToSit = session.createConsumer(session.createQueue("GroupConnector"));
43                         consWantToSit.setMessageListener(this);
44                 } catch (JMSException e) {
45                         log.error("EXCEPTION!", e);
46                 }
47         }
48
49         @Override
50         public void onMessage(Message msg) {
51                 try {
52                         msg.acknowledge();
53                         if (msg instanceof ObjectMessage) {
54                                 ObjectMessage objMsg = (ObjectMessage) msg;
55                                 Object obj = objMsg.getObject();
56
57                                 if (obj instanceof TableResponse) {
58                                         TableResponse tablerresponse = (TableResponse) obj;
59                                         log.debug("Received: " + tablerresponse);
60                                         Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(tablerresponse.getGroupdata().getId());
61                                         g.assignTable(tablerresponse.getTable(), tablerresponse.getWaiterId());
62                                         return;
63                                 }
64                         }
65
66                         log.warn("Unknown messagetype received!");
67                 } catch (JMSException e) {
68                         log.error("EXCEPTION!", e);
69                 }
70         }
71
72         @Override
73         public void send(AbstractAction request) {
74                 try {
75                         if (request instanceof TableRequest) {
76                                 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
77                                 MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable"));
78                                 wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
79                                 wantToSitAtTable.send(session.createObjectMessage(request));
80                                 session.close();
81
82                                 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
83                                 MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzariaConnector"));
84                                 informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
85                                 informPizzariaGui.send(session.createObjectMessage(request));
86                                 session.close();
87                                 return;
88                         } else if (request instanceof OrderRequest) {
89                                 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
90                                 MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToOrder"));
91                                 wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
92                                 wantToSitAtTable.send(session.createObjectMessage(request));
93                                 session.close();
94
95                                 session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
96                                 MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzariaConnector"));
97                                 informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
98                                 informPizzariaGui.send(session.createObjectMessage(request));
99                                 session.close();
100                                 return;
101                         }
102                 } catch (JMSException e) {
103                         log.error("EXCEPTION!", e);
104                 }
105         }
106
107 }