public class MultiTypedMessagesFactory extends java.lang.Object implements IMultiTypedMessagesFactory
public class Program { public static class MyRequestMessage { public double Number1; public double Number2; }Implementation of sender (client):
public static void main(String[] args) { try { // Create multi-typed receiver. IMultiTypedMessagesFactory aFactory = new MultiTypedMessagesFactory(); IMultiTypedMessageReceiver aReceiver = aFactory.createMultiTypedMessageReceiver();
// Register message types which can be processed. aReceiver.registerRequestMessageReceiver(myIntegerHandler, Integer.class); aReceiver.registerRequestMessageReceiver(myMyRequestMessageHandler, MyRequestMessage.class);
// Attach input channel and start listening e.g. using TCP. IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("tcp://127.0.0.1:8033/"); aReceiver.attachDuplexInputChannel(anInputChannel);
System.out.println("Service is running. Press ENTER to stop."); new BufferedReader(new InputStreamReader(System.in)).readLine();
// Detach input channel to stop the listening thread. aReceiver.detachDuplexInputChannel(); } catch (Exception err) { EneterTrace.error("Service failed.", err); } }
private static void onIntegerMessage(Object eventSender, TypedRequestReceivedEventArgs<String> e) { int aNumber = e.getRequestMessage();
// Calculate factorial. int aResult = 1; for (int i = 1; i <= aNumber; ++i) { aResult *= i; }
System.out.println(aNumber + "! =" + aResult);
// Send back the result. IMultiTypedMessageReceiver aReceiver = (IMultiTypedMessageReceiver)eventSender; try { aReceiver.sendResponseMessage(e.getResponseReceiverId(), aResult, Integer.class); } catch (Exception err) { EneterTrace.error("Failed to send the response message.", err); } }
private static void onMyReqestMessage(Object eventSender, TypedRequestReceivedEventArgs<MyRequestMessage> e) { MyRequestMessage aRequestMessage = e.getRequestMessage();
double aResult = aRequestMessage.Number1 + aRequestMessage.Number2;
System.out.println(aRequestMessage.Number1 + " + " + aRequestMessage.Number2 + " = " + aResult);
// Send back the message. IMultiTypedMessageReceiver aReceiver = (IMultiTypedMessageReceiver)eventSender; try { aReceiver.sendResponseMessage(e.getResponseReceiverId(), aResult, Double.class); } catch (Exception err) { EneterTrace.error("Failed to send the response message.", err); } }
private static EventHandler<TypedRequestReceivedEventArgs<Integer>> myIntegerHandler = new EventHandler<TypedRequestReceivedEventArgs<Integer>>() { @Override public void onEvent(Object sender, TypedRequestReceivedEventArgs<Integer> e) { onStringMessage(sender, e); } };
private static EventHandler<TypedRequestReceivedEventArgs<MyRequestMessage>> myMyRequestMessageHandler = new EventHandler<TypedRequestReceivedEventArgs<MyRequestMessage>>() { @Override public void onEvent(Object sender, TypedRequestReceivedEventArgs<MyRequestMessage> e) { onMyReqestMessage(sender, e); } };
}
public class Program { public static class MyRequestMessage { public double Number1; public double Number2; }
public static void main(String[] args) { // Create multi-typed sender. IMultiTypedMessagesFactory aFactory = new MultiTypedMessagesFactory(); ISyncMultitypedMessageSender aSender = aFactory.createSyncMultiTypedMessageSender();
try { // Attach output channel and be able to communicate with the service. IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("tcp://127.0.0.1:8033/"); aSender.attachDuplexOutputChannel(anOutputChannel);
// Request to calculate two numbers. MyRequestMessage aRequestMessage = new MyRequestMessage(); aRequestMessage.Number1 = 10; aRequestMessage.Number2 = 20; double aResult = aSender.sendRequestMessage(aRequestMessage, Double.class, MyRequestMessage.class); System.out.println(aRequestMessage.Number1 + " + " + aRequestMessage.Number2 + " = " + aResult);
// Request to calculate factorial. int aFactorial = aSender.sendRequestMessage((int)6, Integer.class, Integer.class); System.out.println("6! = " + aFactorial); } catch (Exception err) { EneterTrace.error("Calculating failed.", err); }
// Detach input channel and stop listening to responses. aSender.detachDuplexOutputChannel(); }
}
Constructor and Description |
---|
MultiTypedMessagesFactory()
Constructs the factory with default XmlSerializer.
|
MultiTypedMessagesFactory(ISerializer serializer)
Constructs the factory.
|
Modifier and Type | Method and Description |
---|---|
IMultiTypedMessageReceiver |
createMultiTypedMessageReceiver()
Creates multityped message receiver which can receive request messages and send response messages.
|
IMultiTypedMessageSender |
createMultiTypedMessageSender()
Creates multityped message sender which can send request messages and receive response messages.
|
ISyncMultitypedMessageSender |
createSyncMultiTypedMessageSender()
Creates mulityped message sender which sends a request message and then waits for the response.
|
ISerializer |
getSerializer()
Gets serializer for messages.
|
GetSerializerCallback |
getSerializerProvider()
Gets callback for retrieving serializer based on response receiver id.
|
IThreadDispatcherProvider |
getSyncDuplexTypedSenderThreadMode()
Gets the threading mode which is used for receiving connectionOpened and connectionClosed events in SyncDuplexTypedMessageSender.
|
int |
getSyncResponseReceiveTimeout()
Gets the timeout which is used for SyncMultitypedMessageSender.
|
MultiTypedMessagesFactory |
setSerializer(ISerializer serializer)
Sets serializer for messages.
|
MultiTypedMessagesFactory |
setSerializerProvider(GetSerializerCallback serializerProvider)
Sets callback for retrieving serializer based on response receiver id.
|
MultiTypedMessagesFactory |
setSyncDuplexTypedSenderThreadMode(IThreadDispatcherProvider threadingMode)
Sets the threading mode for receiving connectionOpened and connectionClosed events for SyncDuplexTypedMessageSender.
|
MultiTypedMessagesFactory |
setSyncResponseReceiveTimeout(int milliseconds)
Sets the timeout which is used for SyncMultitypedMessageSender.
|
public MultiTypedMessagesFactory()
public MultiTypedMessagesFactory(ISerializer serializer)
serializer
- serializer which will serialize messages.public IMultiTypedMessageSender createMultiTypedMessageSender()
IMultiTypedMessagesFactory
createMultiTypedMessageSender
in interface IMultiTypedMessagesFactory
public ISyncMultitypedMessageSender createSyncMultiTypedMessageSender()
IMultiTypedMessagesFactory
createSyncMultiTypedMessageSender
in interface IMultiTypedMessagesFactory
public IMultiTypedMessageReceiver createMultiTypedMessageReceiver()
IMultiTypedMessagesFactory
createMultiTypedMessageReceiver
in interface IMultiTypedMessagesFactory
public MultiTypedMessagesFactory setSyncDuplexTypedSenderThreadMode(IThreadDispatcherProvider threadingMode)
threadingMode
- threading that shall be used for receiving connectionOpened and connectionClosed events.public IThreadDispatcherProvider getSyncDuplexTypedSenderThreadMode()
public MultiTypedMessagesFactory setSerializer(ISerializer serializer)
serializer
- serializerpublic ISerializer getSerializer()
public GetSerializerCallback getSerializerProvider()
public MultiTypedMessagesFactory setSerializerProvider(GetSerializerCallback serializerProvider)
serializerProvider
- public MultiTypedMessagesFactory setSyncResponseReceiveTimeout(int milliseconds)
milliseconds
- timeout in millisecondspublic int getSyncResponseReceiveTimeout()