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