[44] | 1 | package omq.server; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.concurrent.BlockingQueue; |
---|
| 5 | import java.util.concurrent.LinkedBlockingDeque; |
---|
| 6 | |
---|
[53] | 7 | import omq.common.util.Serializer; |
---|
| 8 | |
---|
[49] | 9 | import org.apache.log4j.Logger; |
---|
| 10 | |
---|
[44] | 11 | import com.rabbitmq.client.QueueingConsumer; |
---|
| 12 | import com.rabbitmq.client.QueueingConsumer.Delivery; |
---|
| 13 | |
---|
| 14 | /** |
---|
[83] | 15 | * This class is used to encapsulate the invocationThreads under the |
---|
| 16 | * RemoteObject. |
---|
[44] | 17 | * |
---|
| 18 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | public class RemoteWrapper { |
---|
[49] | 22 | private static final Logger logger = Logger.getLogger(RemoteWrapper.class.getName()); |
---|
| 23 | |
---|
[44] | 24 | private RemoteObject obj; |
---|
| 25 | private int numThreads; |
---|
| 26 | private ArrayList<InvocationThread> invocationList; |
---|
| 27 | private BlockingQueue<Delivery> deliveryQueue; |
---|
| 28 | |
---|
[53] | 29 | public RemoteWrapper(RemoteObject obj, int numThreads, Serializer serializer) { |
---|
[44] | 30 | this.obj = obj; |
---|
| 31 | this.numThreads = numThreads; |
---|
| 32 | invocationList = new ArrayList<InvocationThread>(); |
---|
| 33 | deliveryQueue = new LinkedBlockingDeque<QueueingConsumer.Delivery>(); |
---|
| 34 | |
---|
[49] | 35 | logger.info("Object reference: " + obj.getRef() + ", numthreads listening = " + numThreads); |
---|
[44] | 36 | |
---|
| 37 | for (int i = 0; i < numThreads; i++) { |
---|
[53] | 38 | InvocationThread thread = new InvocationThread(obj, deliveryQueue, serializer); |
---|
[44] | 39 | invocationList.add(thread); |
---|
| 40 | thread.start(); |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
[83] | 44 | /** |
---|
| 45 | * This method notifies a delivery to an invocationThread using a |
---|
| 46 | * blockingQueue. |
---|
| 47 | * |
---|
| 48 | * @param delivery |
---|
| 49 | * - delivery which contains a Request to be invoked |
---|
| 50 | * @throws Exception |
---|
| 51 | */ |
---|
[44] | 52 | public void notifyDelivery(Delivery delivery) throws Exception { |
---|
| 53 | this.deliveryQueue.put(delivery); |
---|
| 54 | } |
---|
| 55 | |
---|
[83] | 56 | /** |
---|
| 57 | * This method interrups all the invocationThreads under this remoteWrapper |
---|
| 58 | */ |
---|
[44] | 59 | public void stopRemoteWrapper() { |
---|
[49] | 60 | logger.warn("Stopping Invocation threads vinculed to " + obj.getRef()); |
---|
[44] | 61 | for (InvocationThread thread : invocationList) { |
---|
| 62 | thread.interrupt(); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public RemoteObject getObj() { |
---|
| 67 | return obj; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | public void setObj(RemoteObject obj) { |
---|
| 71 | this.obj = obj; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public int getNumThreads() { |
---|
| 75 | return numThreads; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public void setNumThreads(int numThreads) { |
---|
| 79 | this.numThreads = numThreads; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public ArrayList<InvocationThread> getInvocationList() { |
---|
| 83 | return invocationList; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | public void setInvocationList(ArrayList<InvocationThread> invocationList) { |
---|
| 87 | this.invocationList = invocationList; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | public BlockingQueue<Delivery> getDeliveryQueue() { |
---|
| 91 | return deliveryQueue; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public void setDeliveryQueue(BlockingQueue<Delivery> deliveryQueue) { |
---|
| 95 | this.deliveryQueue = deliveryQueue; |
---|
| 96 | } |
---|
| 97 | } |
---|