1 Reply Latest reply on Nov 27, 2002 2:25 PM by garyg

    Please help very urgent

    suvankar17

      Hi all,
      I have written a very small test consumer. It listens for any message on a particular topic on the jboss-3.0.0 server and when the server goes down for any reason, the test consumer detects the server down through exception listener and prints that. Now what i want is that, whenever the server is restarted, the test consumer should detect it and resume the connection with the server for listening for more messages.... I could not find out how to re start the connection...My code is listed below....



      import javax.jms.*;
      import java.util.*;
      import javax.naming.*;
      public class jmsTest implements MessageListener, javax.jms.ExceptionListener
      {
      static boolean status = true;

      TopicConnectionFactory topicConnectionFactory = null;
      TopicConnection topicConnection = null;
      TopicSession topicSession = null;
      Topic topic = null;
      TopicSubscriber topicSubscriber = null;


      public jmsTest(){
      try{

      Properties prop = new Properties();
      prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      prop.put(Context.PROVIDER_URL,"localhost:1099");
      prop.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");

      InitialContext ctx = new InitialContext(prop);
      try{
      topicConnectionFactory = (TopicConnectionFactory)ctx.lookup("ConnectionFactory");
      }catch(Exception b){
      System.out.println("Server Unreachable");

      }
      topicConnection = topicConnectionFactory.createTopicConnection();
      topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
      topic = (Topic)ctx.lookup("topic/aTopic");
      topicSubscriber = topicSession.createSubscriber(topic);
      topicSubscriber.setMessageListener(this);
      topicConnection.setExceptionListener(this);
      topicConnection.start();
      System.out.println("Server Has Started");

      }catch(Exception e){e.printStackTrace();}

      }
      public void onMessage(Message msg){
      }
      public void onException(JMSException ex){
      try{
      System.out.println("Server Uncreachable..");
      topicConnection.stop();
      }catch(Exception a){a.printStackTrace();}
      }


      public static void main(String[] args)
      {
      jmsTest jms = new jmsTest();
      }

      }



      Thanks very much,
      Suv

        • 1. Re: Please help very urgent
          garyg

          Suv,

          If I'm understanding you right, and if your server goes down ... then when your server comes back up just have it recreate the same topic. Now if the client goes down, you can detect if the topic is there by isRegistered() and if it does already exists, either delete it and recreate or just re-use that same topic which I think is ok.

          Just make sure that when your client detects the server going down by printing the exception msg, that it doesn't take any action to stop subscribing.

          Hope that was of help.