public class RpcFactory extends java.lang.Object implements IRpcFactory
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 and Description |
|---|
RpcFactory()
Constructs RpcFactory with default
XmlStringSerializer. |
RpcFactory(ISerializer serializer)
Constructs RpcFactory with specified serializer.
|
| Modifier and Type | Method and Description |
|---|---|
<TServiceInterface> |
createClient(java.lang.Class<TServiceInterface> clazz)
Creates RPC client for the given interface.
|
<TServiceInterface> |
createPerClientInstanceService(IFunction<TServiceInterface> serviceFactoryMethod,
java.lang.Class<TServiceInterface> clazz)
Creates per-client-instance RPC service for the given interface.
|
<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.
|
public RpcFactory()
XmlStringSerializer.public RpcFactory(ISerializer serializer)
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.public <TServiceInterface> IRpcClient<TServiceInterface> createClient(java.lang.Class<TServiceInterface> clazz)
IRpcFactorycreateClient in interface IRpcFactoryclazz - service interface type.public <TServiceInterface> IRpcService<TServiceInterface> createSingleInstanceService(TServiceInterface service, java.lang.Class<TServiceInterface> clazz)
IRpcFactorycreateSingleInstanceService in interface IRpcFactoryservice - instance implementing the given service interface.clazz - service interface type.public <TServiceInterface> IRpcService<TServiceInterface> createPerClientInstanceService(IFunction<TServiceInterface> serviceFactoryMethod, java.lang.Class<TServiceInterface> clazz)
IRpcFactorycreatePerClientInstanceService in interface IRpcFactoryserviceFactoryMethod - factory method used to create the service instance when the client is connectedclazz - service interface typepublic ISerializer getSerializer()
public RpcFactory setSerializer(ISerializer serializer)
serializer - public GetSerializerCallback getSerializerProvider()
public RpcFactory setSerializerProvider(GetSerializerCallback serializerProvider)
serializerProvider - public IThreadDispatcherProvider getRpcClientThreading()
public RpcFactory setRpcClientThreading(IThreadDispatcherProvider threading)
threading - threading mode that shall be used for routing events.public int getRpcTimeout()
public RpcFactory setRpcTimeout(int rpcTimeout)
rpcTimeout -