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.

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🙂

16 Responses to Creating custom messaging adapters

  1. Tom says:

    It’s really easy to set up😉 Blazeds is the best idea of Adobe😉

  2. Sujit Reddy G says:

    Yes Tom, you are right … i Love LCDS and BlazeDS🙂 They are awesome🙂 Three Cheers to the product developers🙂

  3. Steven Toth says:

    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.

  4. James says:

    How would you create a AIR application which uses Flex messaging?What configuration changes would I need to do?

  5. Sujit Reddy G says:

    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.🙂

  6. Sujit Reddy G says:

    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.🙂

  7. […] FlexFactory et si l’on veut aller plus loin et gérer sont propre protocole c’est possible. Des exemples avec Spring et […]

  8. kuttikumar says:

    hi

    i have a one question ,how to implement the Producer and Consumer component using ‘BlazeDS Integration’ in DataBase valueobject insert.

    if its possible or not???

  9. Hi Kutti kumar,

    You want to notify all clients about a database operation, is that what you want? In that case, you can either use Data Management in LiveCycle Data Services or use messaging service of BlazeDS and write code to publish a message to all consumers when ever required.

    Hope this helps.

  10. Oussama says:

    Thank you for this great work. All samples are clear.
    However, I tried this sample but every time i execute it i have a message box saying “Send Failed”.
    All my configuration files are fine.
    I don’t know where the problem is😦
    I can send you the project and thank you for any help🙂

  11. Hi Oussamabk,

    Please try recompiling you Flex application and also debug and check for more details in the FaultEvent object passed.

    Hope this helps.

  12. Rakesh Reddy says:

    Hi Sujit,

    Thanks alot for the article. I have a problem in the configuration.

    I exactly followed your steps but when I start JBoss, I am getting NoClassDefFoundError(flex.messaging.services.ServiceAdaptor). All the required jar files are in the lib folder but still I am getting this error. I feel problem with class loading priority. Can you please provide a solution for this?

    Thanks,
    Rakesh Reddy Kaki

  13. Hi Rakesh,

    Never encountered such problem, please let us know if you figured out why its not working. I am trying to see why this might be failing.

  14. Kanthi says:

    hi nice work i have a doubt ..How can u route a message to a specific client based on it’s request
    for example if there are 100 clients and a client tries to login then after login the info has to be routed only back to that client rite how can we do that

  15. Maykut says:

    hi sujit
    can i take online users list with using adapter? or any solution is there ?

  16. Hi Maykut,

    As far as I know you can just list of client ids, which I don’t think will help much. When i had to do something similar, I used a session listener to keep track of users logging into my server. When the users authenticate successfully, i create a session and kill the same when he logs out.

    Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: