[44] | 1 | package omq.common.broker; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
[70] | 4 | import java.lang.reflect.Proxy; |
---|
[44] | 5 | import java.util.HashMap; |
---|
[59] | 6 | import java.util.Hashtable; |
---|
[44] | 7 | import java.util.Map; |
---|
| 8 | import java.util.Properties; |
---|
| 9 | |
---|
| 10 | import omq.Remote; |
---|
| 11 | import omq.client.listener.ResponseListener; |
---|
[70] | 12 | import omq.client.proxy.MultiProxymq; |
---|
[44] | 13 | import omq.client.proxy.Proxymq; |
---|
| 14 | import omq.common.util.OmqConnectionFactory; |
---|
| 15 | import omq.common.util.ParameterQueue; |
---|
| 16 | import omq.common.util.Serializer; |
---|
[83] | 17 | import omq.exception.AlreadyBoundException; |
---|
[44] | 18 | import omq.exception.InitBrokerException; |
---|
| 19 | import omq.exception.RemoteException; |
---|
| 20 | import omq.server.RemoteObject; |
---|
[92] | 21 | import omq.supervisor.Supervisor; |
---|
[44] | 22 | |
---|
[49] | 23 | import org.apache.log4j.Logger; |
---|
| 24 | |
---|
[111] | 25 | import com.rabbitmq.client.AMQP.BasicProperties; |
---|
[44] | 26 | import com.rabbitmq.client.Channel; |
---|
| 27 | import com.rabbitmq.client.Connection; |
---|
| 28 | import com.rabbitmq.client.QueueingConsumer; |
---|
| 29 | import com.rabbitmq.client.QueueingConsumer.Delivery; |
---|
| 30 | import com.rabbitmq.client.ShutdownListener; |
---|
| 31 | import com.rabbitmq.client.ShutdownSignalException; |
---|
| 32 | |
---|
[83] | 33 | /** |
---|
| 34 | * A "broker" allows a new connection to a RabbitMQ server. Under this |
---|
| 35 | * connection it can have binded object and proxies. |
---|
| 36 | * |
---|
| 37 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 38 | * |
---|
| 39 | */ |
---|
[44] | 40 | public class Broker { |
---|
[49] | 41 | |
---|
| 42 | private static final Logger logger = Logger.getLogger(Broker.class.getName()); |
---|
| 43 | |
---|
[53] | 44 | private Connection connection; |
---|
| 45 | private Channel channel; |
---|
| 46 | private ResponseListener responseListener; |
---|
| 47 | private Serializer serializer; |
---|
| 48 | private boolean clientStarted = false; |
---|
| 49 | private boolean connectionClosed = false; |
---|
| 50 | private Properties environment = null; |
---|
| 51 | private Map<String, RemoteObject> remoteObjs; |
---|
[59] | 52 | private Map<String, Object> proxies = new Hashtable<String, Object>(); |
---|
[70] | 53 | private Map<String, Object> multiProxies = new Hashtable<String, Object>(); |
---|
[44] | 54 | |
---|
[92] | 55 | // Supervisor |
---|
| 56 | private Supervisor supervisor; |
---|
| 57 | |
---|
[53] | 58 | public Broker(Properties env) throws Exception { |
---|
| 59 | // Load log4j configuration |
---|
[111] | 60 | // URL log4jResource = Broker.class.getResource("/log4j.xml"); |
---|
| 61 | // DOMConfigurator.configure(log4jResource); |
---|
[49] | 62 | |
---|
[53] | 63 | remoteObjs = new HashMap<String, RemoteObject>(); |
---|
| 64 | serializer = new Serializer(env); |
---|
| 65 | environment = env; |
---|
| 66 | connection = OmqConnectionFactory.getNewConnection(env); |
---|
| 67 | channel = connection.createChannel(); |
---|
| 68 | addFaultTolerance(); |
---|
[111] | 69 | if (!connection.isOpen() || !channel.isOpen()) { |
---|
| 70 | if (connection.isOpen()) { |
---|
| 71 | connection.close(); |
---|
| 72 | } |
---|
[53] | 73 | throw new InitBrokerException("The connection didn't work"); |
---|
[44] | 74 | } |
---|
[111] | 75 | |
---|
| 76 | // try { |
---|
| 77 | // tryConnection(env); |
---|
| 78 | // } catch (Exception e) { |
---|
| 79 | // channel.close(); |
---|
| 80 | // connection.close(); |
---|
| 81 | // throw new InitBrokerException("The connection didn't work"); |
---|
| 82 | // } |
---|
[44] | 83 | } |
---|
| 84 | |
---|
[83] | 85 | /** |
---|
| 86 | * This method stops the broker's connection and all the threads created |
---|
| 87 | * |
---|
| 88 | * @throws Exception |
---|
| 89 | */ |
---|
[53] | 90 | public void stopBroker() throws Exception { |
---|
[49] | 91 | logger.warn("Stopping broker"); |
---|
[44] | 92 | // Stop the client |
---|
| 93 | if (clientStarted) { |
---|
[53] | 94 | responseListener.kill(); |
---|
[70] | 95 | // TODO proxies = null; ?? |
---|
[44] | 96 | } |
---|
| 97 | // Stop all the remote objects working |
---|
| 98 | for (String reference : remoteObjs.keySet()) { |
---|
| 99 | unbind(reference); |
---|
| 100 | } |
---|
[47] | 101 | |
---|
[44] | 102 | // Close the connection once all the listeners are died |
---|
| 103 | closeConnection(); |
---|
[47] | 104 | |
---|
| 105 | clientStarted = false; |
---|
[111] | 106 | // connectionClosed = false; |
---|
[47] | 107 | environment = null; |
---|
| 108 | remoteObjs = null; |
---|
[44] | 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * @return Broker's connection |
---|
| 113 | * @throws Exception |
---|
| 114 | */ |
---|
[53] | 115 | public Connection getConnection() throws Exception { |
---|
[44] | 116 | return connection; |
---|
| 117 | } |
---|
| 118 | |
---|
[83] | 119 | /** |
---|
| 120 | * This method close the broker's connection |
---|
| 121 | * |
---|
| 122 | * @throws IOException |
---|
| 123 | */ |
---|
[53] | 124 | public void closeConnection() throws IOException { |
---|
[49] | 125 | logger.warn("Clossing connection"); |
---|
[44] | 126 | connectionClosed = true; |
---|
| 127 | connection.close(); |
---|
[111] | 128 | // connectionClosed = false; |
---|
[44] | 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[83] | 132 | * Return the broker's channel |
---|
[44] | 133 | * |
---|
| 134 | * @return Broker's channel |
---|
| 135 | * @throws Exception |
---|
| 136 | */ |
---|
[99] | 137 | public synchronized Channel getChannel() throws Exception { |
---|
[44] | 138 | return channel; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * Creates a new channel using the Broker's connection |
---|
| 143 | * |
---|
| 144 | * @return newChannel |
---|
| 145 | * @throws IOException |
---|
| 146 | */ |
---|
[99] | 147 | public synchronized Channel getNewChannel() throws IOException { |
---|
[44] | 148 | return connection.createChannel(); |
---|
| 149 | } |
---|
| 150 | |
---|
[83] | 151 | /** |
---|
[99] | 152 | * Publishes a request using a thread safe channel -if the channel is closed |
---|
| 153 | * it tries to open it once |
---|
| 154 | * |
---|
| 155 | * @param exchange |
---|
| 156 | * @param routingKey |
---|
| 157 | * @param props |
---|
| 158 | * @param bytesRequest |
---|
| 159 | * @throws IOException |
---|
| 160 | */ |
---|
[111] | 161 | public synchronized void publishMessge(String exchange, String routingKey, BasicProperties props, byte[] bytesRequest) |
---|
| 162 | throws IOException { |
---|
[99] | 163 | if (!channel.isOpen()) { |
---|
| 164 | logger.error("Broker's channel is closed opening a new one", channel.getCloseReason()); |
---|
| 165 | channel = getNewChannel(); |
---|
| 166 | } |
---|
| 167 | channel.basicPublish(exchange, routingKey, props, bytesRequest); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
[83] | 171 | * Returns the remote object for specified reference. |
---|
| 172 | * |
---|
| 173 | * @param reference |
---|
| 174 | * - Binding name |
---|
| 175 | * @param contract |
---|
| 176 | * - Remote Interface |
---|
| 177 | * @return newProxy |
---|
| 178 | * @throws RemoteException |
---|
| 179 | */ |
---|
[44] | 180 | @SuppressWarnings("unchecked") |
---|
[59] | 181 | public synchronized <T extends Remote> T lookup(String reference, Class<T> contract) throws RemoteException { |
---|
[44] | 182 | try { |
---|
| 183 | |
---|
| 184 | if (!clientStarted) { |
---|
[83] | 185 | initClient(); |
---|
[44] | 186 | } |
---|
| 187 | |
---|
[59] | 188 | if (!proxies.containsKey(reference)) { |
---|
[53] | 189 | Proxymq proxy = new Proxymq(reference, contract, this); |
---|
[44] | 190 | Class<?>[] array = { contract }; |
---|
[70] | 191 | Object newProxy = Proxy.newProxyInstance(contract.getClassLoader(), array, proxy); |
---|
[59] | 192 | proxies.put(reference, newProxy); |
---|
| 193 | return (T) newProxy; |
---|
[44] | 194 | } |
---|
[59] | 195 | return (T) proxies.get(reference); |
---|
[44] | 196 | |
---|
| 197 | } catch (Exception e) { |
---|
| 198 | throw new RemoteException(e); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[83] | 202 | /** |
---|
| 203 | * Returns the remote object for specified reference. This function returns |
---|
| 204 | * an special type of proxy, every method invoked will be multi and |
---|
| 205 | * asynchronous. |
---|
| 206 | * |
---|
| 207 | * @param reference |
---|
| 208 | * - Binding name |
---|
| 209 | * @param contract |
---|
| 210 | * - Remote Interface |
---|
| 211 | * @return newProxy |
---|
| 212 | * @throws RemoteException |
---|
| 213 | */ |
---|
[70] | 214 | @SuppressWarnings("unchecked") |
---|
| 215 | public synchronized <T extends Remote> T lookupMulti(String reference, Class<T> contract) throws RemoteException { |
---|
| 216 | try { |
---|
| 217 | if (!multiProxies.containsKey(reference)) { |
---|
| 218 | MultiProxymq proxy = new MultiProxymq(reference, contract, this); |
---|
| 219 | Class<?>[] array = { contract }; |
---|
| 220 | Object newProxy = Proxy.newProxyInstance(contract.getClassLoader(), array, proxy); |
---|
| 221 | multiProxies.put(reference, newProxy); |
---|
| 222 | return (T) newProxy; |
---|
| 223 | } |
---|
| 224 | return (T) multiProxies.get(reference); |
---|
| 225 | |
---|
| 226 | } catch (Exception e) { |
---|
| 227 | throw new RemoteException(e); |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
[83] | 231 | /** |
---|
| 232 | * Binds the reference to the specified remote object. This function uses |
---|
| 233 | * the broker's environment |
---|
| 234 | * |
---|
| 235 | * @param reference |
---|
| 236 | * - Binding name |
---|
| 237 | * @param remote |
---|
| 238 | * - RemoteObject to bind |
---|
| 239 | * @throws RemoteException |
---|
| 240 | * If the remote operation failed |
---|
| 241 | * @throws AlreadyBoundException |
---|
| 242 | * If name is already bound. |
---|
| 243 | */ |
---|
| 244 | public void bind(String reference, RemoteObject remote) throws RemoteException, AlreadyBoundException { |
---|
[74] | 245 | bind(reference, remote, environment); |
---|
[44] | 246 | } |
---|
[70] | 247 | |
---|
[106] | 248 | public void bind(String reference, String UID, RemoteObject remote) throws RemoteException, AlreadyBoundException { |
---|
| 249 | bind(reference, UID, remote, environment); |
---|
| 250 | } |
---|
| 251 | |
---|
[83] | 252 | /** |
---|
| 253 | * Binds the reference to the specified remote object. This function uses |
---|
| 254 | * the broker's environment |
---|
| 255 | * |
---|
| 256 | * @param reference |
---|
| 257 | * - Binding name |
---|
| 258 | * @param remote |
---|
| 259 | * - RemoteObject to bind |
---|
| 260 | * @param env |
---|
| 261 | * - RemoteObject environment. You can set how many threads will |
---|
| 262 | * be listen to the reference, the multiqueue name and the |
---|
| 263 | * properties of the object queue and multiqueue |
---|
| 264 | * @throws RemoteException |
---|
| 265 | * If the remote operation failed |
---|
| 266 | * @throws AlreadyBoundException |
---|
| 267 | * If name is already bound. |
---|
| 268 | */ |
---|
| 269 | public void bind(String reference, RemoteObject remote, Properties env) throws RemoteException, AlreadyBoundException { |
---|
| 270 | if (remoteObjs.containsKey(reference)) { |
---|
| 271 | throw new AlreadyBoundException(reference); |
---|
| 272 | } |
---|
| 273 | // Try to start the remtoeObject listeners |
---|
[66] | 274 | try { |
---|
[74] | 275 | remote.startRemoteObject(reference, this, env); |
---|
[66] | 276 | remoteObjs.put(reference, remote); |
---|
| 277 | } catch (Exception e) { |
---|
| 278 | throw new RemoteException(e); |
---|
| 279 | } |
---|
| 280 | } |
---|
[44] | 281 | |
---|
[106] | 282 | public void bind(String reference, String UID, RemoteObject remote, Properties env) throws RemoteException, AlreadyBoundException { |
---|
| 283 | if (remoteObjs.containsKey(reference)) { |
---|
| 284 | throw new AlreadyBoundException(reference); |
---|
| 285 | } |
---|
| 286 | // Try to start the remtoeObject listeners |
---|
| 287 | try { |
---|
| 288 | remote.startRemoteObject(reference, UID, this, env); |
---|
| 289 | remoteObjs.put(reference, remote); |
---|
| 290 | } catch (Exception e) { |
---|
| 291 | throw new RemoteException(e); |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
[83] | 295 | /** |
---|
| 296 | * Unbinds a remoteObject from its reference and kills all the threads |
---|
| 297 | * created. |
---|
| 298 | * |
---|
| 299 | * @param reference |
---|
| 300 | * - Binding name |
---|
| 301 | * @throws RemoteException |
---|
| 302 | * If the remote operation failed |
---|
| 303 | * @throws IOException |
---|
| 304 | * If there are problems while killing the threads |
---|
| 305 | */ |
---|
[53] | 306 | public void unbind(String reference) throws RemoteException, IOException { |
---|
[44] | 307 | if (remoteObjs.containsKey(reference)) { |
---|
| 308 | RemoteObject remote = remoteObjs.get(reference); |
---|
| 309 | remote.kill(); |
---|
[95] | 310 | remoteObjs.remove(reference); |
---|
[44] | 311 | } else { |
---|
| 312 | throw new RemoteException("The object referenced by 'reference' does not exist in the Broker"); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
[83] | 318 | * This method ensures the client will have only one ResponseListener. |
---|
[44] | 319 | * |
---|
| 320 | * @throws Exception |
---|
| 321 | */ |
---|
[83] | 322 | private synchronized void initClient() throws Exception { |
---|
[53] | 323 | if (responseListener == null) { |
---|
| 324 | responseListener = new ResponseListener(this); |
---|
[54] | 325 | responseListener.start(); |
---|
[83] | 326 | clientStarted = true; |
---|
[44] | 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | /** |
---|
| 331 | * This function is used to send a ping message to see if the connection |
---|
| 332 | * works |
---|
| 333 | * |
---|
| 334 | * @param env |
---|
| 335 | * @throws Exception |
---|
| 336 | */ |
---|
[53] | 337 | public void tryConnection(Properties env) throws Exception { |
---|
[44] | 338 | Channel channel = connection.createChannel(); |
---|
| 339 | String message = "ping"; |
---|
| 340 | |
---|
| 341 | String exchange = env.getProperty(ParameterQueue.USER_NAME) + "ping"; |
---|
| 342 | String queueName = exchange; |
---|
| 343 | String routingKey = "routingKey"; |
---|
| 344 | |
---|
| 345 | channel.exchangeDeclare(exchange, "direct"); |
---|
| 346 | channel.queueDeclare(queueName, false, false, false, null); |
---|
| 347 | channel.queueBind(queueName, exchange, routingKey); |
---|
| 348 | |
---|
| 349 | channel.basicPublish(exchange, routingKey, null, message.getBytes()); |
---|
| 350 | |
---|
| 351 | QueueingConsumer consumer = new QueueingConsumer(channel); |
---|
| 352 | |
---|
| 353 | channel.basicConsume(queueName, true, consumer); |
---|
| 354 | Delivery delivery = consumer.nextDelivery(1000); |
---|
| 355 | |
---|
| 356 | channel.exchangeDelete(exchange); |
---|
| 357 | channel.queueDelete(queueName); |
---|
| 358 | |
---|
| 359 | channel.close(); |
---|
| 360 | |
---|
| 361 | if (!message.equalsIgnoreCase(new String(delivery.getBody()))) { |
---|
| 362 | throw new IOException("Ping initialitzation has failed"); |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | /** |
---|
| 367 | * This method adds a ShutdownListener to the Broker's connection. When this |
---|
| 368 | * connection falls, a new connection will be created and this will also |
---|
| 369 | * have the listener. |
---|
| 370 | */ |
---|
[53] | 371 | private void addFaultTolerance() { |
---|
[44] | 372 | connection.addShutdownListener(new ShutdownListener() { |
---|
| 373 | @Override |
---|
| 374 | public void shutdownCompleted(ShutdownSignalException cause) { |
---|
[49] | 375 | logger.warn("Shutdown message received. Cause: " + cause.getMessage()); |
---|
[44] | 376 | if (!connectionClosed) |
---|
| 377 | if (cause.isHardError()) { |
---|
| 378 | if (connection.isOpen()) { |
---|
| 379 | try { |
---|
| 380 | connection.close(); |
---|
| 381 | } catch (IOException e) { |
---|
| 382 | e.printStackTrace(); |
---|
| 383 | } |
---|
| 384 | } |
---|
| 385 | try { |
---|
[47] | 386 | connection = OmqConnectionFactory.getNewWorkingConnection(environment); |
---|
[44] | 387 | channel = connection.createChannel(); |
---|
| 388 | addFaultTolerance(); |
---|
| 389 | } catch (Exception e) { |
---|
| 390 | e.printStackTrace(); |
---|
| 391 | } |
---|
| 392 | } else { |
---|
| 393 | Channel channel = (Channel) cause.getReference(); |
---|
| 394 | if (channel.isOpen()) { |
---|
| 395 | try { |
---|
| 396 | channel.close(); |
---|
| 397 | } catch (IOException e) { |
---|
| 398 | e.printStackTrace(); |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | } |
---|
| 403 | }); |
---|
| 404 | } |
---|
| 405 | |
---|
[53] | 406 | public Properties getEnvironment() { |
---|
[47] | 407 | return environment; |
---|
| 408 | } |
---|
| 409 | |
---|
[53] | 410 | public ResponseListener getResponseListener() { |
---|
| 411 | return responseListener; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | public Serializer getSerializer() { |
---|
| 415 | return serializer; |
---|
| 416 | } |
---|
[91] | 417 | |
---|
| 418 | public Map<String, RemoteObject> getRemoteObjs() { |
---|
| 419 | return remoteObjs; |
---|
| 420 | } |
---|
| 421 | |
---|
[92] | 422 | /* |
---|
| 423 | * Supervisor |
---|
| 424 | */ |
---|
[106] | 425 | public void setSupervisor(String supervisorName, String brokerSet, String brokerName) throws Exception { |
---|
[92] | 426 | // Create a RemoteBrokerImpl |
---|
[106] | 427 | bind(brokerSet, brokerName, new RemoteBrokerImpl()); |
---|
[92] | 428 | // Subscribe broker |
---|
| 429 | supervisor = lookup(supervisorName, Supervisor.class); |
---|
[106] | 430 | supervisor.subscribe(brokerSet, brokerName); |
---|
| 431 | logger.info("Supervisor set: " + supervisorName + ", BrokerSet: " + brokerSet + ", BrokerName: " + brokerName); |
---|
[92] | 432 | } |
---|
| 433 | |
---|
| 434 | public Supervisor getSupervisor() { |
---|
| 435 | return supervisor; |
---|
| 436 | } |
---|
[44] | 437 | } |
---|