6.6. The Adapters at Work

You have seen the JMSAdapter and the ActionScriptAdapter in the preceding examples. This section explains both these adapters and lays out the configuration options for them. It also includes a brief discussion on custom adapters.

6.6.1. The ActionScriptAdapter

The ActionScriptAdapter is a lightweight adapter that enables publish/subscribe messaging between Flex clients. The ActionScriptAdapter extends the MessagingAdapter class, which is the base abstract class that supports messaging. Adapters sit at the end of the service call pipe and facilitate interaction with server-side objects or special formats and standards.

The ServiceAdapter interface defines the core contract of an adapter in BlazeDS. The HTTPProxyAdapter that you learned about in Chapter 3 and the JavaAdapter that you learned about in Chapter 4 extend the ServiceAdapter interface. The MessagingAdapter abstract class also extends the ServiceAdapter.

The core features of the ActionScriptAdapter are contained in the invoke method of the adapter. Messages are routed to subscribed Flex clients from inside the invoke method. Following is the code that sends the messages out:

msgService.pushMessageToClients(message, true);
msgService.sendPushMessageFromPeer(message, true);

The pushMessageToClients method of the MessageService class pushes the message to all subscribed local Flex clients. By local, I mean those that reside on the same node within a cluster. If a message needs to be transmitted to clients that are members of the other cluster nodes, then the method sendPushMessageFromPeer comes in handy. You can configure one of the following message routing modes for a cluster:

  • broadcast

  • server-to-server

In the broadcast mode, the message is broadcast through the cluster. In the server-to-server mode the message is sent only to servers with matching subscription requests.

That covers the core aspects of the ActionScriptAdapter. Next, you will learn about the JMSAdapter, which you are already familiar with from the previous sections of this chapter.

6.6.2. The JMSAdapter

Like the ActionScriptAdapter, the JMSAdapter extends the MessagingAdapter. The JMSAdapter bridges the gap between BlazeDS and the JMS queues and topics. The essential mechanism involves message routing and passing as with the ActionScriptAdapter. However, things are more complicated than what you saw in the ActionScriptAdapter because the program needs to connect and send messages using the JMS idioms and syntax. The JMSAdapter class is assisted by a set of classes that specifically cater to interfacing with the JMS infrastructure. The JMS specific classes are:

  • JMSConfigConstants—Constants for the JMS adapter

  • JMSConsumer—JMS Proxy subclass for the JMS message consumer

  • JMSExceptionEvent—Event triggered on a JMS exception

  • JMSExceptionListener—Listener for the JMS exception

  • JMSMessageEvent—Event triggered on JMS message

  • JMSMessageListener—Listener for the JMS message

  • JMSProducer—JMS Proxy subclass for the JMS message producer

  • JMSProxy—Superclass of the JMS consumers and producers

  • JMSQueueConsumer—JMS queue receiver

  • JMSQueueProducer—JMS queue sender

  • JMSSettings—JMS adapter settings and configuration

  • JMSTopicConsumer—JMS topic receiver

  • JMSTopicProducer—JMS topic sender

You have already seen the JMS adapter in action and configured an instance with the example application so repeating the same here would be redundant.

The invoke method still remains the primary entry point of the adapter. The invoke method of the JMSAdapter first checks to find out the destination type from its settings. If the destination type is a topic, then it uses a TopicProducer to send a message and a TopicConsumer to consume a message. On the other hand, if the destination type is a queue, then it uses a QueueProducer to send a message and a QueueConsumer to consume a message.

To create a custom adapter, first create a class that extends the MessagingAdapter abstract class. Then start coding the logic for message sending and receiving in the invoke method. It's as straightforward as that. Thus, the logic itself could be complicated, but the adapter API ensures that it does not add further complications and keeps the adapter contract simple and elegant.

Next, we touch upon the topic of extending BlazeDS for enterprise-grade data pushing. That is the last section before it's time to wrap up this chapter.

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

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