[44] | 1 | package omq.client.listener; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.util.HashMap; |
---|
| 5 | import java.util.Hashtable; |
---|
| 6 | import java.util.Map; |
---|
| 7 | import java.util.Properties; |
---|
| 8 | |
---|
[49] | 9 | import org.apache.log4j.Logger; |
---|
| 10 | |
---|
[44] | 11 | import omq.client.proxy.Proxymq; |
---|
| 12 | import omq.common.broker.Broker; |
---|
| 13 | import omq.common.util.ParameterQueue; |
---|
| 14 | |
---|
| 15 | import com.rabbitmq.client.AMQP.BasicProperties; |
---|
| 16 | import com.rabbitmq.client.Channel; |
---|
| 17 | import com.rabbitmq.client.ConsumerCancelledException; |
---|
| 18 | import com.rabbitmq.client.QueueingConsumer; |
---|
| 19 | import com.rabbitmq.client.QueueingConsumer.Delivery; |
---|
| 20 | import 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 | */ |
---|
| 29 | public class ResponseListener extends Thread { |
---|
[53] | 30 | private final Logger logger = Logger.getLogger(ResponseListener.class.getName()); |
---|
[44] | 31 | |
---|
[53] | 32 | private Broker broker; |
---|
[44] | 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 | */ |
---|
[53] | 45 | public ResponseListener(Broker broker) throws Exception { |
---|
| 46 | this.broker = broker; |
---|
| 47 | env = broker.getEnvironment(); |
---|
[44] | 48 | |
---|
| 49 | // Init the hashtable (it's concurrent) |
---|
[53] | 50 | results = new Hashtable<String, Map<String, byte[]>>(); |
---|
[44] | 51 | |
---|
| 52 | startRPCQueue(); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | @Override |
---|
| 56 | public void run() { |
---|
[54] | 57 | logger.info("ResponseListener started"); |
---|
[44] | 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(); |
---|
[50] | 71 | logger.debug("Response received -> proxy reference: " + props.getAppId() + ", corrId: " + uid_request); |
---|
[44] | 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) { |
---|
[49] | 85 | logger.error(i.toString(), i); |
---|
[44] | 86 | } catch (ShutdownSignalException e) { |
---|
[49] | 87 | logger.error(e.toString(), e); |
---|
[44] | 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) { |
---|
[49] | 99 | logger.error(e2.toString(), e2); |
---|
[44] | 100 | } |
---|
| 101 | } |
---|
| 102 | } catch (ConsumerCancelledException e) { |
---|
[49] | 103 | logger.error(e.toString(), e); |
---|
[44] | 104 | } catch (Exception e) { |
---|
[49] | 105 | logger.error(e.toString(), e); |
---|
[44] | 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | private void startRPCQueue() throws Exception { |
---|
[53] | 111 | channel = broker.getNewChannel(); |
---|
[44] | 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); |
---|
[56] | 125 | logger.info("ResponseListener creating queue: " + reply_queue + ", durable: " + durable + "TTL: " + (ttl > 0 ? ttl : "not set")); |
---|
[44] | 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 { |
---|
[49] | 147 | logger.warn("Killing ResponseListener"); |
---|
[44] | 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 | } |
---|