eneter.messaging.nodes.broker
Class DuplexBrokerFactory

java.lang.Object
  extended by eneter.messaging.nodes.broker.DuplexBrokerFactory
All Implemented Interfaces:
IDuplexBrokerFactory

public class DuplexBrokerFactory
extends java.lang.Object
implements IDuplexBrokerFactory

Creates broker and broker client. The broker is the component for publish-subscribe scenarios. It maintains the list of subscribers. When it receives a notification message it forwards it to subscribed clients. IDuplexBrokerClient can publish messages via the broker and also can subscribe for desired messages.

The example shows how to create and use the broker via TCP.

 // Create Tcp based messaging.
 IMessagingSystemFactory aMessagingFactory = new TcpMessagingSystemFactory();

 // Create duplex input channel listening to messages.
 IDuplexInputChannel anInputChannel = aMessagingFactory.createDuplexInputChannel("tcp://127.0.0.1:7980/");

 // Create the factory for the broker.
 IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory();

 // Create the broker.
 IDuplexBroker aBroker = aBrokerFactory.createBroker();

 // Attach the Tcp duplex input channel to the broker and start listening.
 aBroker.attachDuplexInputChannel(anInputChannel);
 
 

Subscribing for the notification messages.
 // Create Tcp based messaging for the silverlight client.
 IMessagingSystemFactory aTcpMessagingFactory = new TcpMessagingSystemFactory();

 // Create duplex output channel to send and receive messages.
 myOutputChannel = aTcpMessagingFactory.createDuplexOutputChannel("tcp://127.0.0.1:7980/");

 // Create the broker client
 IDuplexBrokerFactory aDuplexBrokerFactory = new DuplexBrokerFactory();
 myBrokerClient = aDuplexBrokerFactory.createBrokerClient();

 // Handler to process notification messages.
 myBrokerClient.brokerMessageReceived().subscribe(myNotifyMessageReceived);

 // Attach the channel to the broker client to be able to send and receive messages.
 myBrokerClient.attachDuplexOutputChannel(myOutputChannel);

 // Subscribe in broker to receive chat messages.
 myBrokerClient.subscribe("MyChatMessageType");


 ...


 // Send message to the broker. The broker will then forward it to all subscribers.
 XmlStringSerializer anXmlSerializer = new XmlStringSerializer();
 Object aSerializedChatMessage = anXmlSerializer.Serialize(aChatMessage, ChatMessage.class);
 myBrokerClient.sendMessage("MyChatMessageType", aSerializedChatMessage);

 
 


Constructor Summary
DuplexBrokerFactory()
          Constructs the broker factory with optimized custom serializer.
DuplexBrokerFactory(ISerializer serializer)
          Constructs the broker factory with specified serializer.
 
Method Summary
 IDuplexBroker createBroker()
          Creates the broker.
 IDuplexBrokerClient createBrokerClient()
          Creates the broker client.
 AuthorizeBrokerRequestCallback getBrokerRequestAuthorizer()
          Gets callback for authorizing request messages received from DuplexBrokerClient.
 boolean getIsPublisherNotified()
          Gets the flag whether the publisher which sent a message shall be notified in case it is subscribed to the same message.
 ISerializer getSerializer()
          Returns the serializer which is used to serialize/deserialize BrokerMessate.
 GetSerializerCallback getSerializerProvider()
          Gets callback for retrieving serializer based on response receiver id.
 DuplexBrokerFactory setBrokerRequestAuthorizer(AuthorizeBrokerRequestCallback authorizeCallaback)
          Sets callback for authorizing request messages received from DuplexBrokerClient.
 DuplexBrokerFactory setIsPublisherNotified(boolean isPublisherNotified)
          Sets the flag whether the publisher which sent a message shall be notified in case it is subscribed to the same message.
 DuplexBrokerFactory setSerializer(ISerializer serializer)
          Sets the serializer to serialize/deserialize BrokerMessage.
 DuplexBrokerFactory 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

DuplexBrokerFactory

public DuplexBrokerFactory()
Constructs the broker factory with optimized custom serializer.


DuplexBrokerFactory

public DuplexBrokerFactory(ISerializer serializer)
Constructs the broker factory with specified serializer.

Parameters:
serializer - serializer used by the broker
Method Detail

createBrokerClient

public IDuplexBrokerClient createBrokerClient()
                                       throws java.lang.Exception
Description copied from interface: IDuplexBrokerFactory
Creates the broker client. The broker client is able to send messages to the broker (via attached duplex output channel). It also can subscribe for messages to receive notifications from the broker.

Specified by:
createBrokerClient in interface IDuplexBrokerFactory
Returns:
Throws:
java.lang.Exception

createBroker

public IDuplexBroker createBroker()
                           throws java.lang.Exception
Description copied from interface: IDuplexBrokerFactory
Creates the broker. The broker receives messages and forwards them to subscribers.

Specified by:
createBroker in interface IDuplexBrokerFactory
Returns:
Throws:
java.lang.Exception

setSerializer

public DuplexBrokerFactory setSerializer(ISerializer serializer)
Sets the serializer to serialize/deserialize BrokerMessage. BrokerMessage is used for the communication with the broker.

Parameters:
serializer - serializer
Returns:
this DuplexBrokerFactory

getSerializer

public ISerializer getSerializer()
Returns the serializer which is used to serialize/deserialize BrokerMessate.

Returns:

getSerializerProvider

public GetSerializerCallback getSerializerProvider()
Gets callback for retrieving serializer based on response receiver id. This callback is used by DuplexBroker when it needs to serialize/deserialize the BrokerMessage from a DuplexBrokerClient. Providing this callback allows to use a different serializer for each connected DuplexBrokerClient. 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 DuplexBrokerFactory setSerializerProvider(GetSerializerCallback serializerProvider)
Sets callback for retrieving serializer based on response receiver id. This callback is used by DuplexBroker when it needs to serialize/deserialize the BrokerMessage from a DuplexBrokerClient. Providing this callback allows to use a different serializer for each connected DuplexBrokerClient. 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

getBrokerRequestAuthorizer

public AuthorizeBrokerRequestCallback getBrokerRequestAuthorizer()
Gets callback for authorizing request messages received from DuplexBrokerClient. DuplexBrokerClient can send request messages for publishing, subscribing or unsubscribing of messages. If the callback is not null it is called whenever such request is received from DuplexBrokerClient. If the callback returns true the request is considered authorized and the broker will perform it. If the callback returns false the request is considered invalid and will not be performed and DuplexBrokerClient will be disconnected. E.g. if a DuplexBrokerClient asks broker to publish a message and the callback returns false the message will not be sent to subscribers and DuplexBrokerClient will be disconnected.

Returns:
callback for authorizing requests

setBrokerRequestAuthorizer

public DuplexBrokerFactory setBrokerRequestAuthorizer(AuthorizeBrokerRequestCallback authorizeCallaback)
Sets callback for authorizing request messages received from DuplexBrokerClient. DuplexBrokerClient can send request messages for publishing, subscribing or unsubscribing of messages. If the callback is not null it is called whenever such request is received from DuplexBrokerClient. If the callback returns true the request is considered authorized and the broker will perform it. If the callback returns false the request is considered invalid and will not be performed and DuplexBrokerClient will be disconnected. E.g. if a DuplexBrokerClient asks broker to publish a message and the callback returns false the message will not be sent to subscribers and DuplexBrokerClient will be disconnected.

Parameters:
authorizeCallaback -
Returns:
this DuplexBrokerFactory

setIsPublisherNotified

public DuplexBrokerFactory setIsPublisherNotified(boolean isPublisherNotified)
Sets the flag whether the publisher which sent a message shall be notified in case it is subscribed to the same message. When a DuplexBrokerClient sent a message the broker forwards the message to all subscribed DuplexBrokerClients. In case the DuplexBrokerClient is subscribed to the same message the broker will notify it if the flag IsBublisherNotified is set to true. If it is set to false then the broker will not forward the message to the DuplexBrokerClient which published the message.

Parameters:
isPublisherNotified - true if the DuplexBrokerClient which sent the message shall be notified too.
Returns:
this DuplexBrokerFactory

getIsPublisherNotified

public boolean getIsPublisherNotified()
Gets the flag whether the publisher which sent a message shall be notified in case it is subscribed to the same message. When a DuplexBrokerClient sent a message the broker forwards the message to all subscribed DuplexBrokerClients. In case the DuplexBrokerClient is subscribed to the same message the broker will notify it if the flag IsBublisherNotified is set to true. If it is set to false then the broker will not forward the message to the DuplexBrokerClient which published the message.

Returns: