fulmine.context
Class RpcManager

java.lang.Object
  extended by fulmine.AbstractLifeCycle
      extended by fulmine.context.RpcManager
All Implemented Interfaces:
IRpcManagerOperations, IDestroyable, ILifeCycle, IRpcManager, IRpcPublishOperations

public final class RpcManager
extends AbstractLifeCycle
implements IRpcManager, IRpcManagerOperations

The standard implementation of an IRpcManager.

RPC definitions can be invoked in a multi-threaded context. However, simultaneous calls to the same RPC definition are handled sequentially.

Every RPC definition has an associated 'result record'. When a local context handles an RPC invocation from a remote context, it also creates a result record that will contain the result from the RPC invocation. The result record has a specific naming convention, in ABNF form:

 result-record            = remote-context-identity ":" RPC-registry-key
 
 remote-context-identity  = 1*(ALPHA / DIGIT)
 RPC-registry-key         = "RpcKey" + 1*(DIGIT)
 
When invoking an RPC, the local context first subscribes for the result record then issues the RPC. On the receiving end, the invocation is packaged up into an event. The remote context's RpcManager registers an IEventListener that will respond to these RpcInvokeEvents. This listener will be responsible for locating the appropriate IRpcHandler and invoking it with the arguments encapsulated in the event. The result record attached to the remote context for the RPC definition is then updated with the result and the invoking context will receive this result and return it to the application caller.

The sequence diagram below helps to illustrate the operation.

 Application   IRpcManager    RpcResultHandler      RpcInvokeHandler   IRpcHandler   ResultRecord
  |                 |                |                     |                |             |
  |   invoke        |                |                     |                |             |
  |---------------->|                |                     |                |             |
  |                 |            RpcInokeEvent             |                |             |
  |                 |------------------------------------->|                |             |
  |                 |            (remote call)             |                |             |
  |                 |                |                     |                |             |
  |                 |                |                     |    handle      |             |
  |                 |                |                     |--------------->|             |
  |                 |                |                     |                |             |
  |                 |                |                     |    result      |             |
  |                 |                |                     |<---------------|             |
  |                 |                |                     |                |             |
  |                 |                |                     |     update with result       |
  |                 |                |                     |----------------------------->|
  |                 |                |                     |                |             |
  |                 |                |   get result details (after remote transmission)   |
  |                 |                |--------------------------------------------------->|
  |                 |                |                     |                |             |
  |              result              |                     |                |             |
  |<---------------------------------|                     |                |             |
  |                 |                |                     |                |             |
 
The timeout for RPC calls is defined by the system property IRpcManager.RPC_TIMEOUT. If the timeout expires, an exception is printed and a null value will be returned from invoke(String, String, IField...).

Every context has a special component called the 'RPC registry'. This holds every RPC that a local context exposes. Remote contexts must subscribe for this via IRpcManager.addRpcPublicationListener(String, IRpcPublicationListener) in order for the remote context to receive the RPC definitions of the context. Calling IRpcManager.invoke(String, String, IField...) before the RPC registry is received is safe; when the RPC definition is received from the target context, the RPC will be invoked.

Author:
Ramon Servadei
See Also:
RpcRegistry

Field Summary
 
Fields inherited from interface fulmine.rpc.IRpcManager
DEFAULT_RPC_TIMEOUT, RPC_TIMEOUT
 
Method Summary
 boolean addRpcPublicationListener(String remoteContextIdentity, IRpcPublicationListener listener)
          Add a listener for RPC publication events.
protected  void doDestroy()
          Overridden in subclasses to perform custom logic on destruction.
protected  void doStart()
          Overridden in subclasses to perform custom logic on activation.
protected  AsyncLog getLog()
          Get the log to use for the object hierarchy
 DualValue<String,IRpcDefinition> getRegistryKeyAndDefinition(String remoteContextIdentity, String procedure, IField[] args)
          Get the RPC registry key and IRpcDefinition for the procedure.
 IRpcMarker invoke(IRpcResultHandler resultHandler, String remoteContextIdentity, String procedure, IField... args)
          Asynchronously invoke the RPC in the named remote context.
 IRpcResult invoke(String remoteContextIdentity, String procedure, IField... args)
          Synchronously invoke the RPC in the named remote context.
 void invoke(String remoteContextIdentity, String rpcKey, IRpcDefinition definition, IField[] args, IRpcResultHandler resultHandler, IRpcMarker marker)
          Invoke the RPC in the named remote context.
 boolean publishProdedure(IRpcHandler handler, IRpcDefinition rpcDefinition)
          Publish the named procedure to all known remote contexts.
 boolean publishRpcs(Class<?> definition, Object handler)
          Allows application code to remotely enable all methods in a handler object that are implementations of methods in an interface definition.
 boolean removeRpcPublicationListener(String remoteContextIdentity, IRpcPublicationListener listener)
          Remove an RPC publication listener.
 boolean unpublishProdedure(IRpcDefinition rpcDefinition)
          Unpublish the procedure from all connected remote contexts.
 boolean unpublishRpcs(Class<?> definition, Object handler)
          Allows application code to remove any previously remotely enabled methods published via IRpcPublishOperations.publishRpcs(Class, Object)
 
Methods inherited from class fulmine.AbstractLifeCycle
checkActive, destroy, finalize, isActive, start, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface fulmine.ILifeCycle
isActive, start
 
Methods inherited from interface fulmine.IDestroyable
destroy
 

Method Detail

invoke

public IRpcResult invoke(String remoteContextIdentity,
                         String procedure,
                         IField... args)
Description copied from interface: IRpcManager
Synchronously invoke the RPC in the named remote context. This method will block until a result is returned or a timeout occurs.

It is assumed that this method is called after an RPC publication event is received by an IRpcPublicationListener.

Specified by:
invoke in interface IRpcManager
Parameters:
remoteContextIdentity - the remote context to invoke the RPC in
procedure - the RPC name
args - the arguments for the RPC
Returns:
the result from the RPC
See Also:
IRpcManager.RPC_TIMEOUT

invoke

public IRpcMarker invoke(IRpcResultHandler resultHandler,
                         String remoteContextIdentity,
                         String procedure,
                         IField... args)
Description copied from interface: IRpcManager
Asynchronously invoke the RPC in the named remote context. It is assumed that this method is called after an RPC publication event is received by an IRpcPublicationListener

Specified by:
invoke in interface IRpcManager
Parameters:
resultHandler - the handler to process the asynchronous result
remoteContextIdentity - the remote context to invoke the RPC in
procedure - the RPC name
args - the arguments for the RPC
Returns:
a marker that allows the resultHandler to tie up the result from this specific RPC invocation

invoke

public void invoke(String remoteContextIdentity,
                   String rpcKey,
                   IRpcDefinition definition,
                   IField[] args,
                   IRpcResultHandler resultHandler,
                   IRpcMarker marker)
Description copied from interface: IRpcManagerOperations
Invoke the RPC in the named remote context.

Specified by:
invoke in interface IRpcManagerOperations
Parameters:
remoteContextIdentity - the remote context to invoke the RPC in
rpcKey - the RPC key
definition - the RPC definition
args - the arguments for the RPC
resultHandler - the handler to process the asynchronous result
marker - the marker that allows the resultHandler to tie up the result from this specific RPC invocation

publishProdedure

public boolean publishProdedure(IRpcHandler handler,
                                IRpcDefinition rpcDefinition)
Description copied from interface: IRpcPublishOperations
Publish the named procedure to all known remote contexts.

There can only be one handler per procedure instance. Different handlers can be used for overloaded procedures. RPC definitions cannot overload the result type, if this method is called with a definition that attempts to overload the result type, the operation is ignored and no publish happens.

After this method has completed, remote contexts will be able to invoke the RPC.

Specified by:
publishProdedure in interface IRpcPublishOperations
Parameters:
handler - the object that will handle any RPC invocations for this RPC definition. This object must be thread safe.
rpcDefinition - the RPC definition
Returns:
true if the procedure was published, false if has already been published

unpublishProdedure

public boolean unpublishProdedure(IRpcDefinition rpcDefinition)
Description copied from interface: IRpcPublishOperations
Unpublish the procedure from all connected remote contexts.

Specified by:
unpublishProdedure in interface IRpcPublishOperations
Parameters:
rpcDefinition - the RPC definition to unpublish
Returns:
true if the procedure was unpublished

addRpcPublicationListener

public boolean addRpcPublicationListener(String remoteContextIdentity,
                                         IRpcPublicationListener listener)
Description copied from interface: IRpcManager
Add a listener for RPC publication events. This will be activated when an RPC publication event is received from any connected remote context.

This is an idempotent operation.

Specified by:
addRpcPublicationListener in interface IRpcManager
Parameters:
remoteContextIdentity - the remote context to monitor for RPC publish/unpublish events
listener - a listener to receive the RPC publish/unpublish events.
Returns:
true if the listener was added, false otherwise

removeRpcPublicationListener

public boolean removeRpcPublicationListener(String remoteContextIdentity,
                                            IRpcPublicationListener listener)
Description copied from interface: IRpcManager
Remove an RPC publication listener. The listener will no longer receive notifications when RPC publish/unpublish events occur. *

This is an idempotent operation.

Specified by:
removeRpcPublicationListener in interface IRpcManager
Parameters:
remoteContextIdentity - the remote context that the listener was monitoring for RPC publish/unpublish events
listener - the listener to remove
Returns:
true if the listener was found and removed, false otherwise

doDestroy

protected void doDestroy()
Description copied from class: AbstractLifeCycle
Overridden in subclasses to perform custom logic on destruction. Any exceptions should be thrown as a RuntimeException or subclass thereof.

Specified by:
doDestroy in class AbstractLifeCycle

doStart

protected void doStart()
Description copied from class: AbstractLifeCycle
Overridden in subclasses to perform custom logic on activation. Any exceptions should be thrown as a RuntimeException or subclass thereof. When this method is called, the AbstractLifeCycle.isActive() method will return true.

Specified by:
doStart in class AbstractLifeCycle

getLog

protected AsyncLog getLog()
Description copied from class: AbstractLifeCycle
Get the log to use for the object hierarchy

Overrides:
getLog in class AbstractLifeCycle
Returns:
the log to use for the object hierarchy

getRegistryKeyAndDefinition

public DualValue<String,IRpcDefinition> getRegistryKeyAndDefinition(String remoteContextIdentity,
                                                                    String procedure,
                                                                    IField[] args)
Get the RPC registry key and IRpcDefinition for the procedure. This checks the RPC registry record of the remote context to find the key for an RPC with matching name and arguments.

Specified by:
getRegistryKeyAndDefinition in interface IRpcManagerOperations
Parameters:
remoteContextIdentity - the remote context
procedure - the RPC name
args - the RPC arguments
Returns:
the RPC registry key and IRpcDefinition for the RPC in the remote context or null if not found

unpublishRpcs

public boolean unpublishRpcs(Class<?> definition,
                             Object handler)
Description copied from interface: IRpcPublishOperations
Allows application code to remove any previously remotely enabled methods published via IRpcPublishOperations.publishRpcs(Class, Object)

Specified by:
unpublishRpcs in interface IRpcPublishOperations
Parameters:
definition - an interface that defines all the methods in the handler that should be remotely disabled. This includes all methods in the super-interfaces, if any.
handler - the object that implements the methods declared in the definition interface. These methods will no longer be available for remote invocation.
Returns:
true if all methods were successfully unpublished

publishRpcs

public boolean publishRpcs(Class<?> definition,
                           Object handler)
Description copied from interface: IRpcPublishOperations
Allows application code to remotely enable all methods in a handler object that are implementations of methods in an interface definition. This essentially publishes these methods as RPCs for other contexts to invoke.

There are some caveats to methods that can be remotely enabled; the method arguments and return types must be scalar and only the following types are supported:

This is a convenience method in-lieu of calling IRpcPublishOperations.publishProdedure(IRpcHandler, IRpcDefinition).

Specified by:
publishRpcs in interface IRpcPublishOperations
Parameters:
definition - an interface that defines all the methods in the handler that should be remotely enabled. This includes all methods in the super-interfaces, if any.
handler - the object that implements the methods declared in the definition interface. The handler does not necessarily have to be an instance of the interface (i.e. does not need to explicitly implement the interface, it only needs to implement all the methods). All remote invocations of these methods will be handled by this object. The remotely enabled methods must be thread safe.
Returns:
true if all methods were successfully published


Copyright © 2007-2009. All Rights Reserved.