[44] | 1 | package omq.server; |
---|
| 2 | |
---|
| 3 | import java.lang.reflect.InvocationTargetException; |
---|
| 4 | import java.util.concurrent.BlockingQueue; |
---|
| 5 | |
---|
| 6 | import omq.common.message.Request; |
---|
| 7 | import omq.common.message.Response; |
---|
| 8 | import omq.common.util.Serializer; |
---|
| 9 | import omq.exception.OmqException; |
---|
| 10 | |
---|
| 11 | import com.rabbitmq.client.AMQP.BasicProperties; |
---|
| 12 | import com.rabbitmq.client.Channel; |
---|
| 13 | import com.rabbitmq.client.QueueingConsumer.Delivery; |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * |
---|
| 17 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 18 | * |
---|
| 19 | */ |
---|
| 20 | public class InvocationThread extends Thread { |
---|
| 21 | private RemoteObject obj; |
---|
| 22 | private BlockingQueue<Delivery> deliveryQueue; |
---|
| 23 | private boolean killed = false; |
---|
| 24 | |
---|
| 25 | public InvocationThread(RemoteObject obj, BlockingQueue<Delivery> deliveryQueue) { |
---|
| 26 | this.obj = obj; |
---|
| 27 | this.deliveryQueue = deliveryQueue; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | @Override |
---|
| 31 | public void run() { |
---|
| 32 | while (!killed) { |
---|
| 33 | try { |
---|
| 34 | // Get the delivery |
---|
| 35 | Delivery delivery = deliveryQueue.take(); |
---|
| 36 | |
---|
[47] | 37 | String serializerType = delivery.getProperties().getType(); |
---|
| 38 | |
---|
[44] | 39 | // Deserialize the json |
---|
[47] | 40 | Request request = Serializer.deserializeRequest(serializerType, delivery.getBody(), obj); |
---|
[44] | 41 | // Log.saveLog("Server-Deserialize", delivery.getBody()); |
---|
| 42 | |
---|
| 43 | String methodName = request.getMethod(); |
---|
| 44 | String requestID = request.getId(); |
---|
| 45 | |
---|
| 46 | System.out.println("Invoke method: " + methodName + " CorrID: " + requestID); |
---|
| 47 | |
---|
| 48 | // Invoke the method |
---|
| 49 | Object result = null; |
---|
| 50 | OmqException error = null; |
---|
| 51 | try { |
---|
| 52 | result = obj.invokeMethod(request.getMethod(), request.getParams()); |
---|
| 53 | } catch (InvocationTargetException e) { |
---|
| 54 | Throwable throwable = e.getTargetException(); |
---|
| 55 | error = new OmqException(throwable.getClass().getCanonicalName(), throwable.getMessage()); |
---|
| 56 | } catch (NoSuchMethodException e) { |
---|
| 57 | error = new OmqException(e.getClass().getCanonicalName(), e.getMessage()); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // Reply if it's necessary |
---|
| 61 | if (!request.isAsync()) { |
---|
| 62 | Response resp = new Response(request.getId(), obj.getRef(), result, error); |
---|
| 63 | |
---|
| 64 | Channel channel = obj.getChannel(); |
---|
| 65 | |
---|
| 66 | BasicProperties props = delivery.getProperties(); |
---|
| 67 | |
---|
| 68 | BasicProperties replyProps = new BasicProperties.Builder().appId(obj.getRef()).correlationId(props.getCorrelationId()).build(); |
---|
| 69 | |
---|
[47] | 70 | byte[] bytesResponse = Serializer.serialize(serializerType, resp); |
---|
[44] | 71 | channel.basicPublish("", props.getReplyTo(), replyProps, bytesResponse); |
---|
| 72 | |
---|
| 73 | // Log.saveLog("Server-Serialize", bytesResponse); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | } catch (InterruptedException i) { |
---|
| 77 | i.printStackTrace(); |
---|
| 78 | killed = true; |
---|
| 79 | } catch (Exception e) { |
---|
| 80 | System.out.println("Error a l'Invocation Thread \nException: " + e); |
---|
| 81 | e.printStackTrace(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | public RemoteObject getObj() { |
---|
| 88 | return obj; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public void setObj(RemoteObject obj) { |
---|
| 92 | this.obj = obj; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public BlockingQueue<Delivery> getDeliveryQueue() { |
---|
| 96 | return deliveryQueue; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public void setDeliveryQueue(BlockingQueue<Delivery> deliveryQueue) { |
---|
| 100 | this.deliveryQueue = deliveryQueue; |
---|
| 101 | } |
---|
| 102 | } |
---|