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

Last change on this file since 59 was 56, checked in by stoda, 11 years ago
File size: 4.3 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                logger.info("ResponseListener started");
58                Delivery delivery;
59                String uid_request;
60
61                while (!killed) {
62                        try {
63                                // Get the delivery
64
65                                delivery = consumer.nextDelivery();
66
67                                BasicProperties props = delivery.getProperties();
68
69                                // Get the response with its uid
70                                uid_request = delivery.getProperties().getCorrelationId();
71                                logger.debug("Response received -> proxy reference: " + props.getAppId() + ", corrId: " + uid_request);
72
73                                // Stores the new response
74                                Map<String, byte[]> proxyResults = results.get(props.getAppId());
75
76                                // Put the result into the proxy results and notify him
77                                synchronized (proxyResults) {
78                                        // If we haven't received this response before, we store it
79                                        if (!proxyResults.containsKey(uid_request)) {
80                                                proxyResults.put(uid_request, delivery.getBody());
81                                                proxyResults.notifyAll();
82                                        }
83                                }
84                        } catch (InterruptedException i) {
85                                logger.error(i.toString(), i);
86                        } catch (ShutdownSignalException e) {
87                                logger.error(e.toString(), e);
88                                try {
89                                        if (channel.isOpen()) {
90                                                channel.close();
91                                        }
92                                        startRPCQueue();
93                                } catch (Exception e1) {
94                                        e1.printStackTrace();
95                                        try {
96                                                long milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000"));
97                                                Thread.sleep(milis);
98                                        } catch (InterruptedException e2) {
99                                                logger.error(e2.toString(), e2);
100                                        }
101                                }
102                        } catch (ConsumerCancelledException e) {
103                                logger.error(e.toString(), e);
104                        } catch (Exception e) {
105                                logger.error(e.toString(), e);
106                        }
107                }
108        }
109
110        private void startRPCQueue() throws Exception {
111                channel = broker.getNewChannel();
112
113                Map<String, Object> args = null;
114
115                String reply_queue = env.getProperty(ParameterQueue.RPC_REPLY_QUEUE);
116                boolean durable = Boolean.parseBoolean(env.getProperty(ParameterQueue.DURABLE_QUEUES, "false"));
117
118                int ttl = Integer.parseInt(env.getProperty(ParameterQueue.MESSAGE_TTL_IN_QUEUES, "-1"));
119                if (ttl > 0) {
120                        args = new HashMap<String, Object>();
121                        args.put("x-message-ttl", ttl);
122                }
123
124                channel.queueDeclare(reply_queue, durable, false, false, args);
125                logger.info("ResponseListener creating queue: " + reply_queue + ", durable: " + durable + "TTL: " + (ttl > 0 ? ttl : "not set"));
126
127                // Declare a new consumer
128                consumer = new QueueingConsumer(channel);
129                channel.basicConsume(reply_queue, true, consumer);
130        }
131
132        /**
133         *
134         * @param key
135         * @return whether the map has the param key
136         */
137        public boolean containsKey(String key) {
138                return results.containsKey(key);
139        }
140
141        /**
142         * Interrupt and kill the Thread
143         *
144         * @throws IOException
145         */
146        public void kill() throws IOException {
147                logger.warn("Killing ResponseListener");
148                interrupt();
149                killed = true;
150                channel.close();
151        }
152
153        // Revisar això
154        public void registerProxy(Proxymq proxy) {
155                if (!results.containsKey(proxy.getRef())) {
156                        results.put(proxy.getRef(), proxy.getResults());
157                }
158        }
159}
Note: See TracBrowser for help on using the repository browser.