eneter.messaging.endpoints.rpc
Class RpcFactory

java.lang.Object
  extended by eneter.messaging.endpoints.rpc.RpcFactory
All Implemented Interfaces:
IRpcFactory

public class RpcFactory
extends java.lang.Object
implements IRpcFactory

Creates services and clients that can communicate using RPC (Remote Procedure Calls). RPC is the communication scenario where an application (typically client) executes a method in another application (typically service). RpcFactory provides methods to instantiate RpcService and RpcClient objects. RpcService acts as a stub which provides the communication functionality allowing the service to be reached from outside. RpcClient acts as a proxy which provides the communication functionality allowing the client to call remote methods in the service. The following example shows simple client-service communication using RPC. The service side:

 public interface IHello
 {
     // Event that can be subscribed from the client.
     Event<MyEventArgs> somethingHappened();
     
// Simple method that can be called from the client. int calculate(int a, int b); }
public class HelloService implements IHello { @Override Event<MyEventArgs> somethingHappened() { return mySomethingHappenedEvent.getApi(); }
@Override public int calculate(int a, int b) { return a + b; }
// Helper method just to demonstrate how // to raise an event. public void raiseEvent() { if (mySomethingHappenedEvent.isSubscribed()) { MyEventArgs anEvent = new MyEventArgs(); mySomethingHappenedEvent.raise(this, anEvent); } }
private EventImpl<MyEventArgs> mySomethingHappenedEvent= new EventImpl<MyEventArgs>(); }
public class Program { public static void main(String[] args) throws Exception { // Instantiating the service class. HelloService aHelloService = new HelloService();
// Exposing the service via RPC. RpcFactory anRpcFactory = new RpcFactory(); IRpcService<IHello> aService = anRpcFactory.createService(aHelloService, IHello.class);
// Using TCP for the communication. IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("tcp://127.0.0.1:8045/");
// Attach input channel and start listening. aService.attachDuplexInputChannel(anInputChannel);
System.out.println("Hello service is running. Press ENTER to stop."); new BufferedReader(new InputStreamReader(System.in)).readLine();
// Detach input channel and stop listening. aService.detachDuplexInputChannel(); } }
Calling the service from the client:
 public class Program
 {
     // Event handler
     private static EventHandler<MyEventArgs> myOnSomethingHappened = new EventHandler<MyEventArgs>()
     {
         @Override
         public void onEvent(Object sender, MyEventArgs e)
         {
             onSomethingHappened(sender, e);
         }
     };
     
public static void main(String[] args) throws Exception { // Create the client. IRpcFactory anRpcFactory = new RpcFactory(); IRpcClient<IHello> aClient = anRpcFactory.createClient(IHello.class);
// Use TCP. IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("tcp://127.0.0.1:8045/");
// Attach the output channel and connect the service. aClient.attachDuplexOutputChannel(anOutputChannel);

// Get the service proxy. ICalculator aHelloProxy = aClient.getProxy();
// Subscribe event in the service. aHelloProxy.somethingHappened().subscribe(myOnSomethingHappened);
// Call method in the service. int aResult = aHelloProxy.calculate(10, 30);
System.out.println("Result = " + Integer.toString(aResult));

// Unsubscribe the event. aHelloProxy.somethingHappened().unsubscribe(myOnSomethingHappened);
// Disconnect from the service. aClient.detachDuplexOutputChannel(); }
private static void onSomethingHappened(Object sender, EventArgs e) { // Handle the event from the service here. } }


Constructor Summary
RpcFactory()
          Constructs RpcFactory with default XmlStringSerializer.
RpcFactory(ISerializer serializer)
          Constructs RpcFactory with specified serializer.
 
Method Summary
<TServiceInterface>
IRpcClient<TServiceInterface>
createClient(java.lang.Class<TServiceInterface> clazz)
          Creates RPC client for the given interface.
<TServiceInterface>
IRpcService<TServiceInterface>
createPerClientInstanceService(IFunction<TServiceInterface> serviceFactoryMethod, java.lang.Class<TServiceInterface> clazz)
          Creates per-client-instance RPC service for the given interface.
<TServiceInterface>
IRpcService<TServiceInterface>
createSingleInstanceService(TServiceInterface service, java.lang.Class<TServiceInterface> clazz)
          Creates single-instance RPC service for the given interface.
 IThreadDispatcherProvider getRpcClientThreading()
          Gets threading mechanism used for invoking events (if RPC interface has some) and ConnectionOpened and ConnectionClosed events.
 int getRpcTimeout()
          Gets timeout which specifies until when a call to a remote method must return.
 ISerializer getSerializer()
          Returns serializer used to serialize/deserialize messages between client and service.
 GetSerializerCallback getSerializerProvider()
          Gets callback for retrieving serializer based on response receiver id.
 RpcFactory setRpcClientThreading(IThreadDispatcherProvider threading)
          Sets threading mechanism used for invoking events (if RPC interface has some) and ConnectionOpened and ConnectionClosed events.
 RpcFactory setRpcTimeout(int rpcTimeout)
          Sets timeout which specifies until when a call to a remote method must return.
 RpcFactory setSerializer(ISerializer serializer)
          Sets serializer to be used to serialize/deserialize messages between client and service.
 RpcFactory setSerializerProvider(GetSerializerCallback serializerProvider)
          Sets callback for retrieving serializer based on response receiver id.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RpcFactory

public RpcFactory()
Constructs RpcFactory with default XmlStringSerializer.


RpcFactory

public RpcFactory(ISerializer serializer)
Constructs RpcFactory with specified serializer.

Parameters:
serializer - serializer to be used to serialize/deserialize communication between client and service. Here is the list of serializers provided by Eneter: eneter.messaging.dataprocessing.serializing.
Method Detail

createClient

public <TServiceInterface> IRpcClient<TServiceInterface> createClient(java.lang.Class<TServiceInterface> clazz)
Description copied from interface: IRpcFactory
Creates RPC client for the given interface.

Specified by:
createClient in interface IRpcFactory
Parameters:
clazz - service interface type.
Returns:
RpcClient instance

createSingleInstanceService

public <TServiceInterface> IRpcService<TServiceInterface> createSingleInstanceService(TServiceInterface service,
                                                                                      java.lang.Class<TServiceInterface> clazz)
Description copied from interface: IRpcFactory
Creates single-instance RPC service for the given interface. Single-instance means that there is one instance of the service shared by all clients.

Specified by:
createSingleInstanceService in interface IRpcFactory
Parameters:
service - instance implementing the given service interface.
clazz - service interface type.
Returns:
RpcService instance.

createPerClientInstanceService

public <TServiceInterface> IRpcService<TServiceInterface> createPerClientInstanceService(IFunction<TServiceInterface> serviceFactoryMethod,
                                                                                         java.lang.Class<TServiceInterface> clazz)
Description copied from interface: IRpcFactory
Creates per-client-instance RPC service for the given interface. Per-client-instance means that for each connected client is created a separate instace of the service.

Specified by:
createPerClientInstanceService in interface IRpcFactory
Parameters:
serviceFactoryMethod - factory method used to create the service instance when the client is connected
clazz - service interface type
Returns:
RpcService instance

getSerializer

public ISerializer getSerializer()
Returns serializer used to serialize/deserialize messages between client and service.

Returns:

setSerializer

public RpcFactory setSerializer(ISerializer serializer)
Sets serializer to be used to serialize/deserialize messages between client and service.

Parameters:
serializer -
Returns:

getSerializerProvider

public GetSerializerCallback getSerializerProvider()
Gets callback for retrieving serializer based on response receiver id. This callback is used by RpcService when it needs to serialize/deserialize the communication with RpcClient. Providing this callback allows to use a different serializer for each connected client. This can be used e.g. if the communication with each client needs to be encrypted using a different password.

The default value is null and it means SerializerProvider callback is not used and one serializer which specified in the Serializer property is used for all serialization/deserialization.
If SerializerProvider is not null then the setting in the Serializer property is ignored.

Returns:
GetSerializerCallback

setSerializerProvider

public RpcFactory setSerializerProvider(GetSerializerCallback serializerProvider)
Sets callback for retrieving serializer based on response receiver id. This callback is used by RpcService when it needs to serialize/deserialize the communication with RpcClient. Providing this callback allows to use a different serializer for each connected client. This can be used e.g. if the communication with each client needs to be encrypted using a different password.

The default value is null and it means SerializerProvider callback is not used and one serializer which specified in the Serializer property is used for all serialization/deserialization.
If SerializerProvider is not null then the setting in the Serializer property is ignored.

Parameters:
serializerProvider -
Returns:
GetSerializerCallback

getRpcClientThreading

public IThreadDispatcherProvider getRpcClientThreading()
Gets threading mechanism used for invoking events (if RPC interface has some) and ConnectionOpened and ConnectionClosed events.

Returns:
thread dispatcher provider which returns thread dispatcher that is used to rout events.

setRpcClientThreading

public RpcFactory setRpcClientThreading(IThreadDispatcherProvider threading)
Sets threading mechanism used for invoking events (if RPC interface has some) and ConnectionOpened and ConnectionClosed events. Default setting is that events are routed one by one via a working thread.
It is recomended not to set the same threading mode for the attached output channel because a deadlock can occur when a remote procedure is called (e.g. if a return value from a remote method is routed to the same thread as is currently waiting for that return value the deadlock occurs).

Note: The threading mode for the RPC service is defined by the threading mode of attached duplex input channel.

Parameters:
threading - threading mode that shall be used for routing events.
Returns:

getRpcTimeout

public int getRpcTimeout()
Gets timeout which specifies until when a call to a remote method must return. Default value is 0 what is the infinite time.

Returns:

setRpcTimeout

public RpcFactory setRpcTimeout(int rpcTimeout)
Sets timeout which specifies until when a call to a remote method must return. Default value is 0 what is the infinite time.

Parameters:
rpcTimeout -
Returns: