source: trunk/src/main/java/omq/client/listener/ResponseListener.java @ 53

Last change on this file since 53 was 53, checked in by stoda, 11 years ago

Non static broker
TODO: change all test to see whether the new broker configuration works

File size: 4.1 KB
Line 
1package omq.client.listener;
2
3import java.io.IOException;
4import java.util.HashMap;
5import java.util.Hashtable;
6import java.util.Map;
7import java.util.Properties;
8
9import org.apache.log4j.Logger;
10
11import omq.client.proxy.Proxymq;
12import omq.common.broker.Broker;
13import omq.common.util.ParameterQueue;
14
15import com.rabbitmq.client.AMQP.BasicProperties;
16import com.rabbitmq.client.Channel;
17import com.rabbitmq.client.ConsumerCancelledException;
18import com.rabbitmq.client.QueueingConsumer;
19import com.rabbitmq.client.QueueingConsumer.Delivery;
20import com.rabbitmq.client.ShutdownSignalException;
21
22/**
23 * Class that inherits from RemoteListener. It's used in the server side. This
24 * class gets the deliveries from the server and stores them into the proxies
25 *
26 * @author Sergi Toda <sergi.toda@estudiants.urv.cat>
27 *
28 */
29public class ResponseListener extends Thread {
30        private final Logger logger = Logger.getLogger(ResponseListener.class.getName());
31
32        private Broker broker;
33        private Channel channel;
34        private QueueingConsumer consumer;
35        private boolean killed = false;
36        private Map<String, Map<String, byte[]>> results;
37        private Properties env;
38
39        /**
40         * Protected constructor used by the singleton pattern
41         *
42         * @param env
43         * @throws Exception
44         */
45        public ResponseListener(Broker broker) throws Exception {
46                this.broker = broker;
47                env = broker.getEnvironment();
48
49                // Init the hashtable (it's concurrent)
50                results = new Hashtable<String, Map<String, byte[]>>();
51
52                startRPCQueue();
53        }
54
55        @Override
56        public void run() {
57                Delivery delivery;
58                String uid_request;
59
60                while (!killed) {
61                        try {
62                                // Get the delivery
63
64                                delivery = consumer.nextDelivery();
65
66                                BasicProperties props = delivery.getProperties();
67
68                                // Get the response with its uid
69                                uid_request = delivery.getProperties().getCorrelationId();
70                                logger.debug("Response received -> proxy reference: " + props.getAppId() + ", corrId: " + uid_request);
71
72                                // Stores the new response
73                                Map<String, byte[]> proxyResults = results.get(props.getAppId());
74
75                                // Put the result into the proxy results and notify him
76                                synchronized (proxyResults) {
77                                        // If we haven't received this response before, we store it
78                                        if (!proxyResults.containsKey(uid_request)) {
79                                                proxyResults.put(uid_request, delivery.getBody());
80                                                proxyResults.notifyAll();
81                                        }
82                                }
83                        } catch (InterruptedException i) {
84                                logger.error(i.toString(), i);
85                        } catch (ShutdownSignalException e) {
86                                logger.error(e.toString(), e);
87                                try {
88                                        if (channel.isOpen()) {
89                                                channel.close();
90                                        }
91                                        startRPCQueue();
92                                } catch (Exception e1) {
93                                        e1.printStackTrace();
94                                        try {
95                                                long milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000"));
96                                                Thread.sleep(milis);
97                                        } catch (InterruptedException e2) {
98                                                logger.error(e2.toString(), e2);
99                                        }
100                                }
101                        } catch (ConsumerCancelledException e) {
102                                logger.error(e.toString(), e);
103                        } catch (Exception e) {
104                                logger.error(e.toString(), e);
105                        }
106                }
107        }
108
109        private void startRPCQueue() throws Exception {
110                channel = broker.getNewChannel();
111
112                Map<String, Object> args = null;
113
114                String reply_queue = env.getProperty(ParameterQueue.RPC_REPLY_QUEUE);
115                boolean durable = Boolean.parseBoolean(env.getProperty(ParameterQueue.DURABLE_QUEUES, "false"));
116
117                int ttl = Integer.parseInt(env.getProperty(ParameterQueue.MESSAGE_TTL_IN_QUEUES, "-1"));
118                if (ttl > 0) {
119                        args = new HashMap<String, Object>();
120                        args.put("x-message-ttl", ttl);
121                }
122
123                channel.queueDeclare(reply_queue, durable, false, false, args);
124
125                // Declare a new consumer
126                consumer = new QueueingConsumer(channel);
127                channel.basicConsume(reply_queue, true, consumer);
128        }
129
130        /**
131         *
132         * @param key
133         * @return whether the map has the param key
134         */
135        public boolean containsKey(String key) {
136                return results.containsKey(key);
137        }
138
139        /**
140         * Interrupt and kill the Thread
141         *
142         * @throws IOException
143         */
144        public void kill() throws IOException {
145                logger.warn("Killing ResponseListener");
146                interrupt();
147                killed = true;
148                channel.close();
149        }
150
151        // Revisar això
152        public void registerProxy(Proxymq proxy) {
153                if (!results.containsKey(proxy.getRef())) {
154                        results.put(proxy.getRef(), proxy.getResults());
155                }
156        }
157}
Note: See TracBrowser for help on using the repository browser.