24.13. JMS Messaging
Seam Remoting provides experimental support for JMS Messaging. This section describes currently-implemented JMS support. Note that this may change in the future. At present, we do not recommend using this feature within a production environment.
24.13.1. Configuration
Before you can subscribe to a JMS topic, you must first configure a list of the topics that Seam Remoting can subscribe to. List the topics under
org.jboss.seam.remoting.messaging.subscriptionRegistry. allowedTopics in seam.properties, web.xml or components.xml:
<remoting:remoting poll-timeout="5" poll-interval="1"/>
24.13.2. Subscribing to a JMS Topic
The following example demonstrates how to subscribe to a JMS Topic:
function subscriptionCallback(message) {
if (message instanceof Seam.Remoting.TextMessage)
alert("Received message: " + message.getText());
}
Seam.Remoting.subscribe("topicName", subscriptionCallback);
The
Seam.Remoting.subscribe() method accepts two parameters: the name of the JMS topic to subscribe to, and the callback function to invoke when a message is received.
Two message types are supported: Text messages, and Object messages. To test for the message type that is passed to your callback function, use the
instanceof operator. This tests whether the message is a Seam.Remoting.TextMessage or Seam.Remoting.ObjectMessage. A TextMessage contains the text value in its text field. (You can also fetch this value by calling the object's getText() method.) An ObjectMessage contains its object value in its value field. (You can also fetch this value by calling the object's getValue() method.)
24.13.3. Unsubscribing from a Topic
To unsubscribe from a topic, call
Seam.Remoting.unsubscribe() and pass in the topic name:
Seam.Remoting.unsubscribe("topicName");
24.13.4. Tuning the Polling Process
Polling can be controlled and modified with two parameters.
Seam.Remoting.pollInterval controls how long to wait between subsequent polls for new messages. This parameter is expressed in seconds, and its default setting is 10.
Seam.Remoting.pollTimeout is also expressed in seconds. It controls how long a request to the server should wait for a new message before timing out and sending an empty response. Its default is 0 seconds, which means that when the server is polled, if there are no messages ready for delivery, an empty response will be immediately returned.
Use caution when setting a high
pollTimeout value. Each request that has to wait for a message uses a server thread until either the message is received, or the request times out. If many such requests are served simultaneously, a large number of server threads will be used.
We recommend setting these options in
components.xml, but they can be overridden with JavaScript if desired. The following example demonstrates a more aggressive polling method. Set these parameters to values that suit your application:
In
components.xml:
<remoting:remoting poll-timeout="5" poll-interval="1"/>
With Java:
// Only wait 1 second between receiving a poll response and sending the next poll request. Seam.Remoting.pollInterval = 1; // Wait up to 5 seconds on the server for new messages Seam.Remoting.pollTimeout = 5;