FileDocCategorySizeDatePackage
HAJMSClient.javaAPI DocJBoss 4.2.16159Fri Jul 13 20:52:42 BST 2007org.jboss.mq.il.ha.examples

HAJMSClient

public class HAJMSClient extends org.jboss.system.ServiceMBeanSupport implements javax.jms.MessageListener, javax.jms.ExceptionListener, HAJMSClientMBean
Helps to manually test the HAIL
author
Ivelin Ivanov

Fields Summary
private javax.jms.Topic
topic_
private javax.jms.TopicSession
topicSession_
private javax.jms.TopicConnection
topicConn_
private javax.jms.JMSException
connectionException_
private javax.jms.TopicSubscriber
topicSub_
private javax.jms.TopicPublisher
topicPub_
private javax.jms.TextMessage
lastMessage_
private javax.jms.Queue
q_
private javax.jms.QueueConnection
qConn_
private javax.jms.QueueSession
qSession_
private javax.jms.QueueReceiver
qRecv_
private javax.jms.QueueSender
qSend_
private static Logger
log
Constructors Summary
Methods Summary
public voidconnect()

    log.info("Connecting");

    InitialContext iniCtx = new InitialContext();
    Object tmp = iniCtx.lookup("HAILXAConnectionFactory");
    
    TopicConnectionFactory tcf = (TopicConnectionFactory)tmp;
    topicConn_ = tcf.createTopicConnection(); 
    topic_ = (Topic)iniCtx.lookup("topic/testTopic");
    topicSession_ = topicConn_.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    topicConn_.setExceptionListener(this);
    topicSub_ = topicSession_.createSubscriber(topic_);
    topicSub_.setMessageListener( this );
    topicPub_ = topicSession_.createPublisher(topic_);
    topicConn_.start();

    QueueConnectionFactory qcf = (QueueConnectionFactory)tmp;
    qConn_ = qcf.createQueueConnection(); 
    q_ = (Queue)iniCtx.lookup("queue/testQueue");
    qSession_ = qConn_.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    qRecv_ = qSession_.createReceiver(q_);
    qRecv_.setMessageListener( this );
    qSend_ = qSession_.createSender(q_);
    qConn_.start();

    log.info("Connected");
  
public voiddisconnect()

    if (topicConn_ == null) return;

    log.info("Disconnecting");

    connectionException_ = null;
    
    try
    {
    topicConn_.setExceptionListener(null);

    topicSub_.close();    
    topicPub_.close();    
    topicConn_.stop();
    topicSession_.close();
    
    qRecv_.close();
    qSend_.close();
    qConn_.stop();
    qSession_.close();
    }
    finally
    {
      try
      {
        topicConn_.close();
      }
      finally
      {
        topicConn_ = null;
        try
        {
          qConn_.close();
        }
        finally
        {
          qConn_ = null;
        }
      }
    
    }
    log.info("Disconnected");
  
public java.lang.StringgetConnectionException()

    if (connectionException_ == null) return null;
    return connectionException_.toString();
  
public java.lang.StringgetLastMessage()

    if (lastMessage_ == null) return null;
    return lastMessage_.getText();
  
public voidonException(javax.jms.JMSException connEx)
Acknowledges connenction exception. Should be invoked every time the HAIL singleton moves.

    log.info("Notification received by ExceptionListener. Singleton Probably Moved.");
    try
    {
      reconnect();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      connectionException_ = connEx;
    }
  
public voidonMessage(javax.jms.Message msg)
Handle JMS message

see
javax.jms.MessageListener#onMessage(javax.jms.Message)

    lastMessage_ = (TextMessage)msg;
    log.info("Message received: " + msg);
  
public voidpublishMessageToTopic(java.lang.String text)

    TextMessage msg = topicSession_.createTextMessage(text);
    topicPub_.publish(msg);
    log.info("HA JMS message published to topic: " + text);
  
protected voidreconnect()

    log.info("Reconnecting");
    try
    {
      disconnect();
    }
    finally
    {
      connect();
    }
  
public voidsendMessageToQueue(java.lang.String text)

    TextMessage msg = qSession_.createTextMessage(text);
    qSend_.send(msg);
    log.info("HA JMS message sent to queue: " + text);
  
protected voidstartService()
create connection, sessions and subscribe for topic and queue

    connect();
  
protected voidstopService()
unsubscribe from topic, queue, stop sessions and connection

    disconnect();