1 | package omq.server; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.lang.reflect.Method; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.HashMap; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Map; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import omq.Remote; |
---|
12 | import omq.common.broker.Broker; |
---|
13 | import omq.common.util.ParameterQueue; |
---|
14 | import omq.exception.SerializerException; |
---|
15 | |
---|
16 | import org.apache.log4j.Logger; |
---|
17 | |
---|
18 | import com.rabbitmq.client.Channel; |
---|
19 | import com.rabbitmq.client.ConsumerCancelledException; |
---|
20 | import com.rabbitmq.client.QueueingConsumer; |
---|
21 | import com.rabbitmq.client.QueueingConsumer.Delivery; |
---|
22 | import com.rabbitmq.client.ShutdownSignalException; |
---|
23 | |
---|
24 | /** |
---|
25 | * |
---|
26 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
27 | * |
---|
28 | */ |
---|
29 | public abstract class RemoteObject extends Thread implements Remote { |
---|
30 | |
---|
31 | private static final long serialVersionUID = -1778953938739846450L; |
---|
32 | private static final String multi = "multi#"; |
---|
33 | private static final Logger logger = Logger.getLogger(RemoteObject.class.getName()); |
---|
34 | |
---|
35 | private String UID; |
---|
36 | private String multiQueue; |
---|
37 | private Properties env; |
---|
38 | private transient Broker broker; |
---|
39 | private transient RemoteWrapper remoteWrapper; |
---|
40 | private transient Map<String, List<Class<?>>> params; |
---|
41 | private transient Channel channel; |
---|
42 | private transient QueueingConsumer consumer; |
---|
43 | private transient boolean killed = false; |
---|
44 | |
---|
45 | private static final Map<String, Class<?>> primitiveClasses = new HashMap<String, Class<?>>(); |
---|
46 | |
---|
47 | static { |
---|
48 | primitiveClasses.put("byte", Byte.class); |
---|
49 | primitiveClasses.put("short", Short.class); |
---|
50 | primitiveClasses.put("char", Character.class); |
---|
51 | primitiveClasses.put("int", Integer.class); |
---|
52 | primitiveClasses.put("long", Long.class); |
---|
53 | primitiveClasses.put("float", Float.class); |
---|
54 | primitiveClasses.put("double", Double.class); |
---|
55 | } |
---|
56 | |
---|
57 | public RemoteObject() { |
---|
58 | } |
---|
59 | |
---|
60 | public void startRemoteObject(String reference, Broker broker, Properties env) throws Exception { |
---|
61 | this.broker = broker; |
---|
62 | this.UID = reference; |
---|
63 | this.multiQueue = UID + System.currentTimeMillis(); |
---|
64 | this.env = env; |
---|
65 | |
---|
66 | this.params = new HashMap<String, List<Class<?>>>(); |
---|
67 | for (Method m : this.getClass().getMethods()) { |
---|
68 | List<Class<?>> list = new ArrayList<Class<?>>(); |
---|
69 | for (Class<?> clazz : m.getParameterTypes()) { |
---|
70 | list.add(clazz); |
---|
71 | } |
---|
72 | this.params.put(m.getName(), list); |
---|
73 | } |
---|
74 | |
---|
75 | // Get num threads to use |
---|
76 | int numThreads = Integer.parseInt(env.getProperty(ParameterQueue.NUM_THREADS, "1")); |
---|
77 | this.remoteWrapper = new RemoteWrapper(this, numThreads, broker.getSerializer()); |
---|
78 | |
---|
79 | startQueues(); |
---|
80 | |
---|
81 | // Start this listener |
---|
82 | this.start(); |
---|
83 | } |
---|
84 | |
---|
85 | public void startTriggerEvent(String reference, Broker broker) throws Exception { |
---|
86 | this.broker = broker; |
---|
87 | UID = reference; |
---|
88 | if (channel == null || !channel.isOpen()) { |
---|
89 | channel = broker.getChannel(); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | @Override |
---|
94 | public void run() { |
---|
95 | while (!killed) { |
---|
96 | try { |
---|
97 | Delivery delivery = consumer.nextDelivery(); |
---|
98 | |
---|
99 | logger.debug(UID + " has received a message"); |
---|
100 | |
---|
101 | remoteWrapper.notifyDelivery(delivery); |
---|
102 | } catch (InterruptedException i) { |
---|
103 | logger.error(i); |
---|
104 | } catch (ShutdownSignalException e) { |
---|
105 | logger.error(e); |
---|
106 | try { |
---|
107 | if (channel.isOpen()) { |
---|
108 | channel.close(); |
---|
109 | } |
---|
110 | startQueues(); |
---|
111 | } catch (Exception e1) { |
---|
112 | try { |
---|
113 | long milis = Long.parseLong(env.getProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000")); |
---|
114 | Thread.sleep(milis); |
---|
115 | } catch (InterruptedException e2) { |
---|
116 | logger.error(e2); |
---|
117 | } |
---|
118 | logger.error(e1); |
---|
119 | } |
---|
120 | } catch (ConsumerCancelledException e) { |
---|
121 | logger.error(e); |
---|
122 | } catch (SerializerException e) { |
---|
123 | logger.error(e); |
---|
124 | } catch (Exception e) { |
---|
125 | logger.error(e); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | public String getRef() { |
---|
132 | return UID; |
---|
133 | } |
---|
134 | |
---|
135 | public void kill() throws IOException { |
---|
136 | logger.warn("Killing objectmq: " + this.getRef()); |
---|
137 | killed = true; |
---|
138 | interrupt(); |
---|
139 | channel.close(); |
---|
140 | remoteWrapper.stopRemoteWrapper(); |
---|
141 | } |
---|
142 | |
---|
143 | public Object invokeMethod(String methodName, Object[] arguments) throws Exception { |
---|
144 | |
---|
145 | // Get the specific method identified by methodName and its arguments |
---|
146 | Method method = loadMethod(methodName, arguments); |
---|
147 | |
---|
148 | return method.invoke(this, arguments); |
---|
149 | } |
---|
150 | |
---|
151 | private Method loadMethod(String methodName, Object[] args) throws NoSuchMethodException { |
---|
152 | Method m = null; |
---|
153 | |
---|
154 | // Obtain the class reference |
---|
155 | Class<?> clazz = this.getClass(); |
---|
156 | Class<?>[] argArray = null; |
---|
157 | |
---|
158 | if (args != null) { |
---|
159 | argArray = new Class<?>[args.length]; |
---|
160 | for (int i = 0; i < args.length; i++) { |
---|
161 | argArray[i] = args[i].getClass(); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | try { |
---|
166 | m = clazz.getMethod(methodName, argArray); |
---|
167 | } catch (NoSuchMethodException nsm) { |
---|
168 | m = loadMethodWithPrimitives(methodName, argArray); |
---|
169 | } |
---|
170 | return m; |
---|
171 | } |
---|
172 | |
---|
173 | private Method loadMethodWithPrimitives(String methodName, Class<?>[] argArray) throws NoSuchMethodException { |
---|
174 | if (argArray != null) { |
---|
175 | Method[] methods = this.getClass().getMethods(); |
---|
176 | int length = argArray.length; |
---|
177 | |
---|
178 | for (Method method : methods) { |
---|
179 | String name = method.getName(); |
---|
180 | int argsLength = method.getParameterTypes().length; |
---|
181 | |
---|
182 | if (name.equals(methodName) && length == argsLength) { |
---|
183 | // This array can have primitive types inside |
---|
184 | Class<?>[] params = method.getParameterTypes(); |
---|
185 | |
---|
186 | boolean found = true; |
---|
187 | |
---|
188 | for (int i = 0; i < length; i++) { |
---|
189 | if (params[i].isPrimitive()) { |
---|
190 | Class<?> paramWrapper = primitiveClasses.get(params[i].getName()); |
---|
191 | |
---|
192 | if (!paramWrapper.equals(argArray[i])) { |
---|
193 | found = false; |
---|
194 | break; |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | if (found) { |
---|
199 | return method; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | throw new NoSuchMethodException(methodName); |
---|
205 | } |
---|
206 | |
---|
207 | public List<Class<?>> getParams(String methodName) { |
---|
208 | return params.get(methodName); |
---|
209 | } |
---|
210 | |
---|
211 | public Channel getChannel() { |
---|
212 | return channel; |
---|
213 | } |
---|
214 | |
---|
215 | private void startQueues() throws Exception { |
---|
216 | // Get info about which exchange and queue will use |
---|
217 | String exchange = env.getProperty(ParameterQueue.RPC_EXCHANGE); |
---|
218 | String queue = UID; |
---|
219 | String routingKey = UID; |
---|
220 | // Multi info |
---|
221 | String multiExchange = multi + exchange; |
---|
222 | |
---|
223 | boolean durable = Boolean.parseBoolean(env.getProperty(ParameterQueue.DURABLE_QUEUES, "false")); |
---|
224 | |
---|
225 | // Start channel |
---|
226 | channel = broker.getNewChannel(); |
---|
227 | |
---|
228 | // Declares and bindings |
---|
229 | logger.info("RemoteObject: " + UID + " declaring direct exchange: " + exchange + ", Queue: " + queue + ", Durable: " + durable); |
---|
230 | channel.exchangeDeclare(exchange, "direct"); |
---|
231 | channel.queueDeclare(queue, durable, false, false, null); |
---|
232 | channel.queueBind(queue, exchange, routingKey); |
---|
233 | |
---|
234 | logger.info("RemoteObject: " + UID + " declaring fanout exchange: " + multiExchange + ", Queue: " + multiQueue + ", Durable: " + durable); |
---|
235 | channel.exchangeDeclare(multiExchange, "fanout"); |
---|
236 | channel.queueDeclare(multiQueue, durable, false, false, null); |
---|
237 | channel.queueBind(multiQueue, multiExchange, ""); |
---|
238 | |
---|
239 | // Declare a new consumer |
---|
240 | consumer = new QueueingConsumer(channel); |
---|
241 | channel.basicConsume(queue, true, consumer); |
---|
242 | channel.basicConsume(multiQueue, true, consumer); |
---|
243 | } |
---|
244 | |
---|
245 | } |
---|