eneter.messaging.endpoints.typedmessages
Interface IMultiTypedMessageReceiver

All Superinterfaces:
IAttachableDuplexInputChannel

public interface IMultiTypedMessageReceiver
extends IAttachableDuplexInputChannel

Receiver for multiple message types. It is a service component which can receive and send messages of multiple types.
The following example shows how to create a service which can receive messages of various types:

 // 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);
         }
     };
   
 
 


Method Summary
 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>
void
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.
 
Methods inherited from interface eneter.messaging.infrastructure.attachable.IAttachableDuplexInputChannel
attachDuplexInputChannel, detachDuplexInputChannel, getAttachedDuplexInputChannel, isDuplexInputChannelAttached
 

Method Detail

responseReceiverConnected

Event<ResponseReceiverEventArgs> responseReceiverConnected()
Raised when a new client is connected.


responseReceiverDisconnected

Event<ResponseReceiverEventArgs> responseReceiverDisconnected()
Raised when a client closed the connection. The event is raised only if the connection was closed by the client. It is not raised if the client was disconnected by IDuplexInputChannel.disconnectResponseReceiver(...).


registerRequestMessageReceiver

<T> void registerRequestMessageReceiver(EventHandler<TypedRequestReceivedEventArgs<T>> handler,
                                        java.lang.Class<T> clazz)
                                    throws java.lang.Exception
Registers message handler for specified message type. If the specified message type is received the handler will be called to process it.

Parameters:
handler - message handler which shall be called when the specified message type is received.
clazz - type of the message.
Throws:
java.lang.Exception

unregisterRequestMessageReceiver

<T> void unregisterRequestMessageReceiver(java.lang.Class<T> clazz)
Unregisters the message handler for the specified message type.

Parameters:
clazz - type of the message.

getRegisteredRequestMessageTypes

java.util.ArrayList<java.lang.Class<?>> getRegisteredRequestMessageTypes()
Returns the list of registered message types which can be received.

Returns:
registered message types

sendResponseMessage

<TResponseMessage> void sendResponseMessage(java.lang.String responseReceiverId,
                                            TResponseMessage responseMessage,
                                            java.lang.Class<TResponseMessage> clazz)
                         throws java.lang.Exception
Sends the response message. The message of the specified type will be serialized and sent back to the response receiver. If the response receiver has registered a handler for this message type then the handler will be called to process the message.

Parameters:
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 message
clazz - type of the response message
Throws:
java.lang.Exception