9.5. Extending the Messaging Adapter

The ActionScriptAdapter is the simplest of the built-in messaging adapters. It extends the MessagingAdapter abstract class.

Almost all of the functionality in the ActionScriptAdapter class resides in its invoke method. In its invoke method, an ActionScriptAdapter uses a MessageService instance that takes control of the message delivery to all connected clients and server peers. In the invoke method, a call is made to the MessageService's pushMessageToClients method, which sends a message to all connected clients within a local node of a cluster. In addition, the MessageService instance's sendPushMessageFromPeer method is invoked to send messages to the peer servers in the cluster. These servers, in turn, deliver the message to their connected clients.

In this section, let's create another simple messaging adapter like the ActionScriptAdapter. In the custom adapter, I continue to send messages the way the ActionScriptAdapter does. The only modifications I make are:

  • Modify the message body and add the text "Before message body" and "After message body" to the existing message body

  • Add a header named "interceptor" with "CustomActionScriptAdapter" as the value to the message

The source for CustomActionScriptAdapter is shown in Listing 9-3.

Example 9.3. CustomActionScriptAdapter Source
package dsadapters.messaging.adapters.as3;

import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.messaging.adapters.MessagingAdapter;

public class CustomActionScripAdapter extends MessagingAdapter
{

    /**
     *
     */
    public CustomActionScripAdapter()
    {

    }

    /**
     * @param enableManagement
     */

public CustomActionScripAdapter(boolean enableManagement)
    {
        super(enableManagement);
    }

    /* (non-Javadoc)
     * @see flex.messaging.services.ServiceAdapter#invoke(flex.messaging.messages.
        Message)
     */
    @Override
    public Object invoke(Message message)
    {
        AsyncMessage modifiedMessage = (AsyncMessage)message;
        modifiedMessage.setBody("Before message body " + message.getBody() +
           " After
message body");
        modifiedMessage.setHeader("interceptor", "CustomActionScriptAdapter");
        MessageService msgService = (MessageService)getDestination().getService();
        msgService.pushMessageToClients(modifiedMessage, true);
        msgService.sendPushMessageFromPeer(modifiedMessage, true);
        return null;
    }

}

If you browse through the invoke method in the preceding listing, you will notice that a new AsyncMessage instance is created from an existing incoming message. Then the body of the new message is modified to add "before message body" before the message body and "after message body" after the message body. Also, a custom header is added to the modified message. After these modifications, the message is sent using a MessageService instance.

Next, let's explore a few advanced strategies for custom adapter creation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.240.196