1 Reply Latest reply on Jan 16, 2004 11:31 AM by Adrian Brock

    Implementing JMS onException wrong - keep reconnecting after

    Larry Farrar Newbie

      Hi all,

      I am not understanding how to implement a method to reconnect using onException with JMS if JBoss or network connection is lost. I've searched this forum, but obviously I am missing something. I am using JBoss 3.2.1 and JDK 1.4.2 running on Windows 2000 server. On startup, I connect just fine and continue to run as long as JBoss is up. Once I bounce Jboss, I reconnect, but continue to get JMS exceptions in onException resulting in a reconnect, which re-occurs each minute. Client ID continues to increment and it appears that I am actually connected, but keep getting "ping timeouts". Any help would be greatly appreciated. My code and resulting output:

      import java.util.*;
      import java.util.logging.*;
      import javax.naming.InitialContext;
      import javax.naming.Context;
      import javax.naming.NamingException;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.Queue;
      import javax.jms.ConnectionFactory;
      import javax.jms.QueueConnection;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueReceiver;
      import javax.jms.QueueSession;
      import javax.jms.ObjectMessage;
      import javax.jms.ExceptionListener;
      import oxy.oac.Server;

      public class MessageReceiver implements MessageListener, ExceptionListener
      {
      Properties env = null;
      Context jndiCtx = null;
      QueueConnection qConn = null;
      Queue msgQue = null;
      QueueSession qSession = null;
      QueueReceiver qRecver = null;
      ObjectMessage objMsg = null;
      boolean connected = false;
      String queueName = null;
      String providerUrl = null;
      Server server = null;

      public MessageReceiver(Server server)
      {
      this.server = server;
      this.providerUrl = server.getServerName() + ":1099";
      this.queueName = server.getAlarmQueue();
      connectToMessageQueue(providerUrl, queueName);
      }

      public void onException(JMSException jmse)
      {
      String message = jmse.getErrorCode();
      Exception ex = jmse.getLinkedException();
      System.out.println("\n\nGot JMS Exception: " + message);
      System.out.println("Linked exception: " + ex.getMessage());
      connectToMessageQueue(providerUrl, queueName);
      }

      public void onMessage(Message msg)
      {
      System.out.println("Got Message");
      }

      private boolean connectToMessageQueue(String providerUrl, String queueName)
      {
      boolean connected = false;
      qConn = null;
      qRecver = null;
      qSession = null;
      try
      {
      connected = setupPTP(providerUrl, queueName);
      System.out.println("connectToMessageQueue.connected = " + connected);
      qConn.setExceptionListener(this);
      }
      catch (Exception ex)
      {
      ex.printStackTrace();
      }
      finally
      {
      return connected;
      }
      }

      private Context getInitialContext(String providerUrl)
      {
      Context ctx = null;
      try
      {
      env = new Properties();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      env.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
      env.put(Context.PROVIDER_URL, providerUrl);
      ctx = new InitialContext(env);
      }
      catch (Exception ex)
      {
      ex.printStackTrace();
      }
      finally
      {
      return ctx;
      }
      }

      private boolean setupPTP(String providerURL, String queueName)
      {
      boolean connected = false;
      while (qConn == null)
      {
      System.out.println("Attempting connection to: " + providerURL + " - " + queueName);
      try
      {
      jndiCtx = getInitialContext(providerURL);
      Object ref = jndiCtx.lookup("ConnectionFactory");
      QueueConnectionFactory qcf = (QueueConnectionFactory) ref;
      if (qcf != null)
      {
      System.out.println("Got queueConnectionFactory");
      qConn = qcf.createQueueConnection();
      msgQue = (Queue) jndiCtx.lookup(queueName);
      qSession = qConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
      qRecver = qSession.createReceiver(msgQue);
      qRecver.setMessageListener(this);
      System.out.println("JMS ClientId: " + qConn.getClientID());
      qConn.start();
      connected = true;
      }
      }
      catch (Exception ex)
      {
      System.out.print("Exception in setPTP: " );
      ex.printStackTrace();
      if (qConn == null)
      {
      try
      {
      Thread.sleep(5000);
      }
      catch (Exception se)
      {
      se.printStackTrace();
      }
      }
      }
      }
      return connected;
      }


      public static void main(String [] args)
      {
      Server server = new Server("osqautopca", 10, "queue/testQueue");
      System.out.println("serverName = " + server.getServerName());
      MessageReceiver receiver = new MessageReceiver(server);
      }
      }

      OUTPUT:
      serverName = osqautopca
      Attempting connection to: osqautopca:1099 - queue/testQueue
      Got queueConnectionFactory
      JMS ClientId: ID:3
      connectToMessageQueue.connected = true

      NOTE: JBOSS bounced here (only 1 time)

      Got JMS Exception: null
      Linked exception: Connection reset by peer: socket write error
      Attempting connection to: osqautopca:1099 - queue/testQueue
      Exception in setPTP: javax.naming.CommunicationException. Root exception is java.rmi.ConnectException: Connection refused to host: 10.66.66.11; nested exception is:
      java.net.ConnectException: Connection refused: connect
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:567)
      at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
      at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:101)
      at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:492)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:471)
      at javax.naming.InitialContext.lookup(InitialContext.java:347)
      at MessageReceiver.setupPTP(Unknown Source)
      at MessageReceiver.connectToMessageQueue(Unknown Source)
      at MessageReceiver.onException(Unknown Source)
      at org.jboss.mq.Connection.asynchFailure(Connection.java:726)
      at org.jboss.mq.Connection$PingTask.run(Connection.java:1311)
      at EDU.oswego.cs.dl.util.concurrent.ClockDaemon$RunLoop.run(ClockDaemon.java:364)
      at java.lang.Thread.run(Thread.java:536)
      Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
      at java.net.Socket.connect(Socket.java:426)
      at java.net.Socket.connect(Socket.java:376)
      at java.net.Socket.(Socket.java:291)
      at java.net.Socket.(Socket.java:119)
      at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
      at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)
      ... 14 more
      Attempting connection to: osqautopca:1099 - queue/testQueue
      Got queueConnectionFactory
      JMS ClientId: ID:3
      connectToMessageQueue.connected = true


      Got JMS Exception: null
      Linked exception: ping timeout.
      Attempting connection to: osqautopca:1099 - queue/testQueue
      Got queueConnectionFactory
      JMS ClientId: ID:4
      connectToMessageQueue.connected = true


      Got JMS Exception: null
      Linked exception: ping timeout.
      Attempting connection to: osqautopca:1099 - queue/testQueue
      Got queueConnectionFactory
      JMS ClientId: ID:5
      connectToMessageQueue.connected = true


      Got JMS Exception: null
      Linked exception: ping timeout.
      Attempting connection to: osqautopca:1099 - queue/testQueue
      Got queueConnectionFactory
      JMS ClientId: ID:6
      connectToMessageQueue.connected = true