Kbase 20393: SonicMQ: Code Example for Detecting a Broken Connection
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  01/12/2000 |
|
SUMMARY:
This solution provides sample code to ping a broker, listen for the broker response, and catch the exception if ping fails. The code is a small modification of the Chat sample.
SOLUTION:
It is a two step process:
1) Implement the exception listener interface.
2) Set the ping interval.
The code example follows:
/* Code example for pinging a broker, listening for the broker
response and catching the exception if ping fails. This is a small modification of the Chat example.
Steps are :
-implementing the exception listener interface:
javax.jms.ExceptionListener with method : public void
onException(JMSException exception)
-setting the ping interval:
progress.message.jclient.Connection.setPingInterval(long interval)
*/
public class PingChat
implements javax.jms.MessageListener, javax.jms.ExceptionListener
{
private javax.jms.TopicConnection connect = null;
private javax.jms.TopicSession session = null;
private javax.jms.TopicPublisher publisher = null;
String broker;
/** Create JMS client for publishing and subscribing to messages.
*/
private void chatter( String username, String password,
String sTopic, int interval)
{
// Create a connection.
try
{
javax.jms.TopicConnectionFactory factory;
factory = (new
progress.message.jclient.TopicConnectionFactory (broker));
connect =
factory.createTopicConnection (username, password);
// setting the ping interval on the connection
((progress.message.jclient.Connection)
connect).setPingInterval(interval);
session = connect.createTopicSession(
false,javax.jms.Session.AUTO_ACKNOWLEDGE);
// starting the exception listener for the connection
connect.setExceptionListener( this );
}
catch (javax.jms.JMSException jmse)
{
System.err.println("error: Cannot connect to Broker - " +
broker);
jmse.printStackTrace();
System.exit(1);
}
// Create Publisher and Subscriber to 'chat' topics
try
{
javax.jms.Topic topic = session.createTopic (sTopic);
javax.jms.TopicSubscriber subscriber =
session.createSubscriber(topic);
subscriber.setMessageListener(this);
publisher = session.createPublisher(topic);
// Now that setup is complete, start the Connection
connect.start();
}
catch (javax.jms.JMSException jmse)
{
jmse.printStackTrace();
}
try
{
// Read all standard input and send it as a message.
java.io.BufferedReader stdin =
new java.io.BufferedReader( new
java.io.InputStreamReader(
System.in ) );
System.out.println ("\nEnter text messages
\nPress Enter to publish each message.\n");
while ( true )
{
String s = stdin.readLine();
if ( s == null )
exit();
else if ( s.length() > 0 )
{
javax.jms.TextMessage msg =
session.createTextMessage();
msg.setText( username + ": " + s );
publisher.publish( msg );
}
}
}
catch ( java.io.IOException ioe )
{
ioe.printStackTrace();
}
catch ( javax.jms.JMSException jmse )
{
jmse.printStackTrace();
}
}
public void onException(javax.jms.JMSException exception) {
System.out.println("Connection to " + broker + " broken ");
System.exit(1);
}
/**
* Handle the message
* (as specified in the javax.jms.MessageListener interface).
*/
public void onMessage( javax.jms.Message aMessage)
{
try
{
// Cast the message as a text message.
javax.jms.TextMessage textMessage =
(javax.jms.TextMessage) aMessage;
// This handler reads a single String from the
// message and prints it to the standard output.
try
{
String string = textMessage.getText();
System.out.println( string );
}
catch (javax.jms.JMSException jmse)
{
jmse.printStackTrace();
System.out.println("onMessage");
}
}
catch (java.lang.RuntimeException rte)
{
rte.printStackTrace();
System.out.println("Outside of onMessage");
}
}
/** Cleanup resources and then exit. */
private void exit()
{
try
{
connect.close();
}
catch (javax.jms.JMSException jmse)
{
jmse.printStackTrace();
}
System.exit(0);
}
//
/** Main program entry point. */
public static void main(String argv[]) {
// Is there anything to do?
if (argv.length < 5) {
printUsage();
System.exit(1);
}
// construct chat object
PingChat chat = new PingChat();
// Values to be read from parameters
chat.broker = argv[0];
String username = argv[1];
String password = argv[2];
String topic = argv[3];
int interval = new Integer(argv[4]).intValue();
// Start the JMS client for the "chat".
chat.chatter (username, password, topic, interval);
}
/** Prints the usage. */
private static void printUsage() {
StringBuffer use = new StringBuffer();
use.append("\nusage: java Chat (options) ...\n\n");
use.append("options:\n\n");
use.append("name:port name:port of broker.\n");
use.append("username unique user name. \n");
use.append("password password for user.\n");
use.append("topic topic name to publish messages.\n");
use.append("interval interval to ping broker in sec.\n");
use.append("example:\n\n
java Chat pcrwe:2506 rwe test jms.samples.chat 20\n");
System.err.println (use);
}
}
References To Written Documentation:
SonicMQ 2000.1 API Reference.
Progress Knowledge Base Solution 19272, "SonicMQ: How to
Detect a Broker Disconnected from the Network".