/** * Adds a local object as a listener of messages. * * <p>The user (i.e., RemoteObject) of this class calls this method to register the RemoteObject * as "local RemoteObject" with this class. * * <pre> * [RemoteObject] [RemoteObject] [RemoteObject] * dispatchEvent() dispatchRequest() dispatchRequest() * ^ ^ ^ * | | | * +---------------+ + +---------------+ * | | | * [MessageDispatcher]<>-----[SubscribersMap] * | * message * | * [pubsub server] * </pre> * * <p>This method also sends "SUBSCRIBE own-object-ID-as-channel" to pubsub server to receive * PUBLISH destined to the remote object, since requestSync() method sends PUBLISH as "request" to * the remote object. * * @param localObject a remote object * @see org.o3project.odenos.remoteobject.RemoteObject#dispatchEvent * @see org.o3project.odenos.remoteobject.RemoteObject#dispatchRequest */ public void addLocalObject(RemoteObject localObject) { String objectId = localObject.getObjectId(); if (localObjectsMap.putIfAbsent(objectId, localObject) == null) { driverImpl.subscribeChannel(objectId); } if (objectId.equals(systemManagerId)) { driverImpl.systemManagerAttached(); } }
/** * Starts the services. * * <p>This method must be called after instantiating this class to start a {@link IPubSubDriver} * implementation class. */ public void start() { // This method blocks until the connectivity with pubsub server // has become ready. driverImpl.start(); // To receive Request from "remote" RemoteObject, // MessageDispatcher needs to register itself w/ pubsub server. driverImpl.subscribeChannel(sourceDispatcherId); // This thread feeds subscription info to EventManager // in an eventually-consistent manner. subscriptionFeeder = new Thread( new Runnable() { @Override public void run() { do { Request request = null; try { request = eventManagerQueue.take(); Response response = requestSync(request); if (response == null || !response.statusCode.equals(Response.OK)) { log.warn("Unsuccessful transaction to EventManager: " + response.statusCode); } } catch (InterruptedException e) { log.warn("Unsuccessful transaction to EventManager due to some internal error"); } catch (Exception e) { log.warn("EventManager may be inactive"); } } while (true); // TODO: graceful thread termination } }); subscriptionFeeder.start(); log.info("started"); }