public interface IMultiTypedMessageReceiver extends IAttachableDuplexInputChannel
// Create multityped receiver
IMultiTypedMessagesFactory aFactory = new MultiTypedMessagesFactory();
IMultiTypedMessageReceiver aReceiver = aFactory.createMultiTypedMessageReceiver();
// Register handlers for message types which can be received.
aReceiver.registerRequestMessageReceiver(myAlarmHandler, Alarm.class);
aReceiver.registerRequestMessageReceiver(myImageHandler, Image.class);
// Attach input channel and start listening. E.g. using TCP.
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("tcp://127.0.0.1:9043/");
aReceiver.attachDuplexInputChannel(anInputChannel);
System.out.println("Service is running. Press ENTER to stop.");
new BufferedReader(new InputStreamReader(System.in)).readLine();
// Detach input channel and stop listening.
aReceiver.detachInputChannel();
The following code demonstrates how to implement handlers:
private void onAlarmMessage(object sender, TypedRequestReceivedEventArgs<Alarm> e)
{
// Get alarm message data.
Alarm anAlarm = e.getRequestMessage();
....
// Send response message.
aReceiver.sendResponseMessage(e.getResponseReceiverId(), aResponseMessage, ResponseMessage.class);
}
private void onImageMessage(object sender, TypedRequestReceivedEventArgs<Image> e)
{
// Get image message data.
Image anImage = e.getRequestMessage();
....
// Send response message.
aReceiver.sendResponseMessage(e.getResponseReceiverId(), aResponseMessage, ResponseMessage.class);
}
private EventHandler<TypedRequestReceivedEventArgs<Alarm>> myAlarmHandler =
new EventHandler<TypedRequestReceivedEventArgs<Alarm>>()
{
public void onEvent(Object sender, TypedRequestReceivedEventArgs<Alarm> e)
{
onAlarmMessage(sender, e);
}
};
private EventHandler<TypedRequestReceivedEventArgs<Image>> myImageHandler =
new EventHandler<TypedRequestReceivedEventArgs<Image>>()
{
public void onEvent(Object sender, TypedRequestReceivedEventArgs<Image> e)
{
onImageMessage(sender, e);
}
};
Modifier and Type | Method and Description |
---|---|
java.util.ArrayList<java.lang.Class<?>> |
getRegisteredRequestMessageTypes()
Returns the list of registered message types which can be received.
|
<T> void |
registerRequestMessageReceiver(EventHandler<TypedRequestReceivedEventArgs<T>> handler,
java.lang.Class<T> clazz)
Registers message handler for specified message type.
|
Event<ResponseReceiverEventArgs> |
responseReceiverConnected()
Raised when a new client is connected.
|
Event<ResponseReceiverEventArgs> |
responseReceiverDisconnected()
Raised when a client closed the connection.
|
<TResponseMessage> |
sendResponseMessage(java.lang.String responseReceiverId,
TResponseMessage responseMessage,
java.lang.Class<TResponseMessage> clazz)
Sends the response message.
|
<T> void |
unregisterRequestMessageReceiver(java.lang.Class<T> clazz)
Unregisters the message handler for the specified message type.
|
attachDuplexInputChannel, detachDuplexInputChannel, getAttachedDuplexInputChannel, isDuplexInputChannelAttached
Event<ResponseReceiverEventArgs> responseReceiverConnected()
Event<ResponseReceiverEventArgs> responseReceiverDisconnected()
<T> void registerRequestMessageReceiver(EventHandler<TypedRequestReceivedEventArgs<T>> handler, java.lang.Class<T> clazz) throws java.lang.Exception
handler
- message handler which shall be called when the specified message type is received.clazz
- type of the message.java.lang.Exception
<T> void unregisterRequestMessageReceiver(java.lang.Class<T> clazz)
clazz
- type of the message.java.util.ArrayList<java.lang.Class<?>> getRegisteredRequestMessageTypes()
<TResponseMessage> void sendResponseMessage(java.lang.String responseReceiverId, TResponseMessage responseMessage, java.lang.Class<TResponseMessage> clazz) throws java.lang.Exception
responseReceiverId
- identifies the client. If responseReceiverId is * then the broadcast message
to all connected clients is sent.
// Send broadcast to all connected clients. aReceiver.sendResponseMessage("*", aBroadcastMessage, YourBroadcast.class);
responseMessage
- response messageclazz
- type of the response messagejava.lang.Exception