23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

JMS provides an ExceptionListener interface for trapping a lost connection and notifying<br />

the client of this condition. The ExceptionListener is bound to the connection - unlike<br />

<strong>Message</strong>Listeners, which are bound to sessions. The definition of the ExceptionListener<br />

is:<br />

public interface ExceptionListener{<br />

void onException(JMSException exception);<br />

}<br />

It is the responsibility of the JMS provider to call the onException( ) method of all<br />

registered ExceptionListeners after making reasonable attempts to reestablish the<br />

connection automatically. The JMS client can implement the ExceptionListener so that it<br />

can be alerted to a lost connection, and possibly attempt to reestablish the connection<br />

manually.<br />

How can the JMS provider call an ExceptionListener if the client has been disconnected?<br />

Every JMS provider has some amount of functionality that resides in the client application.<br />

We have been referring to this as the client runtime portion of the JMS provider. It is the<br />

responsibility of the client runtime to detect the loss of connection and call the<br />

ExceptionListener.<br />

6.5.1 The Wholesaler Becomes an ExceptionListener<br />

To make our Wholesaler into an ExceptionListener, we start by changing the formal<br />

declaration of the class to implement the javax.jms.ExceptionListener interface:<br />

public class Wholesaler implements<br />

javax.jms.<strong>Message</strong>Listener,<br />

javax.jms.ExceptionListener<br />

{<br />

...<br />

Next, we remove the connection setup information from the constructor and isolate it in its<br />

own method, establishConnection( ):<br />

public Wholesaler(String broker, String username, String password){<br />

mBroker = broker;<br />

mUsername = username;<br />

mPassword = password;<br />

}<br />

establishConnection( broker, username, password );<br />

establishConnection( ) sets up the connection, the publisher, and the subscriber. The new<br />

addition is the retry logic on the TopicConnectionFactory's createTopicConnection( )<br />

method, which continually retries the connection every ten seconds until it is established:<br />

private void establishConnection(String broker, String username,<br />

String password)<br />

{<br />

...<br />

while (connect == null)<br />

{<br />

try {<br />

connect =<br />

factory.createTopicConnection (username, password);<br />

} catch (javax.jms.JMSException jmse)<br />

105

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!