You create custom messaging service adapters when you want a functionality which the default adapter is not providing. Default messaging adapter (Action script adapter) shipped with BlazeDS will just broadcast the message, which the producer sends. What if you want change this functionality? You just have to create your own service adapters.
You can create custom service adapter class by extending the flex.messaging.services.ServiceAdapter class. Other classes which we will use are:
MessageService class is used for sending message to all the clients subscribed to the destination. This class provides methods like pushMessageToClients(), using which we can send messages to all the subscribed clients.
AsyncMessage class is used for creating message to send to the users.
The ServiceAdapter class and other classes required are in the flex-messaging-common.jar and flex-messaging-core.jar files. These jar files are bundled with BlazeDS.
I have created a simple application in which I am creating a message and sending it to all the clients subscribed to a destination. When the user clicks on a button reading “Ready to bid”, the application will send message to all the subscribed clients. I used custom adapter to modify the message and send it to all the clients.
Note: you can achieve this sample application without using custom adapter also 🙂
Creating sample application
1. Create a custom messaging adapter class
2. Add the adapter to the services-config.xml
3. Create a destination
4. Create Flex application to produce/consume the message
Create custom messaging adapter
In this class, I just modified the message body and sent it to all the clients. You have to override the invoke() method of the ServiceAdapter class. Invoke() method will be called when any client sends a message to the destination. By overriding the invoke() method you get access to the message object, which the client sent.
Download the custom messaging adapter’s Java file
Configure the services-config.xml and creating destination
You have to add your destination and adapter definitions to either services-config.xml or the messaging-config.xml file. Add the destination and the adapter as shown in the code snippet below. I have added the custom adapter, created above to the configuration file using the adapter element. I have also added the destination and mapped it to the adapter.
<service id=”message-service” class=”flex.messaging.services.MessageService”>
<adapter-definition id=”SujitAdapter” class=”com.adobe.adapter.CustomMessagingAdapter”/>
Create Flex application
We have completed the configuration and the development on the server. We will create a flex application which subscribe to the destination and receive messages from other users. Users can also send messages to other clients on click of a button.
Download the mxml file
That’s it. We have created a custom messaging adapter for BlazeDS. You can create your own messaging adapters and map your destinations to the adapters. There is lots more than just creating custom messaging adapters. You can even manage the list of clients subscribed to a destination yourself, instead of leaving it to BlazeDS 🙂
Adobe Rocks 🙂