Creating custom messaging adapters
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:
- flex.messaging.messages.AsyncMessage
- flex.messaging.messages.Message
- flex.messaging.services.MessageService
- flex.messaging.services.ServiceAdapter
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.
Sample application
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
Steps
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”>
<adapters>
<adapter-definition id=”SujitAdapter” class=”com.adobe.adapter.CustomMessagingAdapter”/>
</adapters>
<destination id=”CustomMessagingAdapter”>
<adapter ref=”SujitAdapter”/>
</destination>
</service>
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.
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 ![]()
March 14, 2008 at 11:25 am
It’s really easy to set up
Blazeds is the best idea of Adobe 
March 14, 2008 at 11:39 am
Yes Tom, you are right … i Love LCDS and BlazeDS
They are awesome
Three Cheers to the product developers 
March 22, 2008 at 4:51 pm
Interesting post. Thanks for the information. One question though - you’re just sending the intercepted message. What if you have a server-side event that triggers a need to push an unsolicited message. What properties of the message object need to be set (ex. do you need a message id? If so, how do you generate one?)? Thx.
March 25, 2008 at 4:01 am
How would you create a AIR application which uses Flex messaging?What configuration changes would I need to do?
March 25, 2008 at 5:11 am
Hi James,
I don’t think there are any specific configuration for AIR applications. You can use it as you were using it in a Flex web application. If you have any problem implementing, please let me know.
March 25, 2008 at 5:12 am
Hi Steven,
I am not sure what all are the properties which are to be set. I can try that and let you know.
Meanwhile if you find out, i will be glad to learn it from you. 
April 8, 2008 at 5:11 pm
[...] FlexFactory et si l’on veut aller plus loin et gérer sont propre protocole c’est possible. Des exemples avec Spring et [...]