4 Replies Latest reply on Apr 30, 2012 10:20 AM by Justin Bertram

    JMS publisher calling JBoss AS7 problem.

    Haroon Foad Newbie

      Why I'm getting runtime error running this jms publisher

       

      org.jnp.interfaces.NamingContext  - Failed to connect to localhost:5445

      javax.naming.CommunicationException: Failed to retrieve stub from server localhost:5445 [Root exception is java.io.EOFException]

       

      JBoss AS7.1.1.Final

       

      package javaapplication3;
      
      
      import java.util.HashMap;
      import java.util.Hashtable;
      
      
      import javax.jms.JMSException;
      import javax.jms.Session;
      import javax.jms.TextMessage;
      import javax.jms.Topic;
      import javax.jms.TopicConnection;
      import javax.jms.TopicConnectionFactory;
      import javax.jms.TopicPublisher;
      import javax.jms.TopicSession;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      
      import org.apache.log4j.BasicConfigurator;
      import org.apache.log4j.Logger;
      
      
      //import com.lit.jms.utils.log.Log;
      /**
       * Publish to the JMS the required messages.
       *
       * @author R.Couturier
       */
      public class JMSPublisher {
      
      
          private static HashMap<String, JMSPublisher> jmsSenders = new HashMap<String, JMSPublisher>();
          public static long jsmId = 0;
          /**
           * @param serverIP The IP of the server that run the JMS service.
           * @param serverPort The port of the server that run the JMS service.
           * @param topicName The name of the JMS topic (the topic do the same functionality as the queue).
           */
          private Context context;
          private String topicName;
          private TopicConnection topicConnection;
          private TopicPublisher topicPublisher;
          private TopicSession topicSession;
      
      
          private JMSPublisher(String topicName) {
              super();
              this.topicName = topicName;
      
      
              /**
               * create a JNDI context
               */
              Hashtable<String, String> properties = new Hashtable<String, String>();
      
      
              properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
              properties.put(Context.PROVIDER_URL, "jnp://localhost:5445/");
              properties.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
      
      
      
              try {
                  Logger.getLogger(JMSPublisher.class.getName()).debug("initializing context ... ");
      
      
                  this.context = new InitialContext(properties);
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Context initialized");
      
      
                  /**
                   *  retrieve topic connection factory
                   */
                 TopicConnectionFactory factory = (TopicConnectionFactory) this.context.lookup("java:/ConnectionFactory");
                  Logger.getLogger(this.getClass()).info("2");
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Factory looked up");
                  Logger.getLogger(this.getClass()).info("3");
      
      
                  /**
                   *  create a topic connection using factory
                   */
                  this.topicConnection = factory.createTopicConnection();
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Topic created");
      
      
                  this.topicConnection.start();
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Topic connection started");
      
      
                  /**
                   *  create a topic session
                   */
                  /**
                   *  set transactions to false and set auto acknowledgement of receipt of messages
                   */
                  this.topicSession = this.topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Topic session created");
      
      
                  /**
                   *  lookup the topic
                   */
                  Topic topic = (Topic) this.context.lookup(this.topicName);
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Topic looked up");
      
      
                  /**
                   *  create a topic publisher and associate to the retrieved topic
                   */
                  this.topicPublisher = this.topicSession.createPublisher(topic);
                  Logger.getLogger(JMSPublisher.class.getName()).debug("Topic publisher created");
              } catch (NamingException e) {
                  Logger.getLogger(JMSPublisher.class.getName()).error("NamingException:" + e.getCause().getMessage());
              } catch (JMSException e) {
                  Logger.getLogger(JMSPublisher.class.getName()).error("JMSException:" + e.getMessage());
              }
          }
      
      
          /**
           * DOCUMENT ME!
           */
          public void finish() {
              /**
               *  clean up
               */
              try {
                  this.context.close();
                  this.topicSession.close();
                  this.topicPublisher.close();
                  this.topicConnection.close();
      
      
                  this.topicPublisher = null;
                  this.topicSession = null;
                  this.topicConnection = null;
                  this.context = null;
      
      
                  jmsSenders = new HashMap<String, JMSPublisher>();
              } catch (JMSException e) {
                  Logger.getLogger(JMSPublisher.class.getName()).error(e);
              } catch (NamingException e) {
                  Logger.getLogger(JMSPublisher.class.getName()).error(e);
              }
              Logger.getLogger(JMSPublisher.class.getName()).info("JMS sender closed");
          }
      
      
          /**
           * DOCUMENT ME!
           *
           * @param topicName DOCUMENT ME!
           *
           * @return DOCUMENT ME!
           */
          public static JMSPublisher getInstance(String topicName) {
              JMSPublisher jmsSender = jmsSenders.get(topicName);
      
      
              if (jmsSender == null) {
                  jmsSender = new JMSPublisher(topicName);
                  jmsSenders.put(topicName, jmsSender);
              }
      
      
              return jmsSender;
          }
      
      
          /**
           * Publish a in input to the JMS queue.
           * @param input The The input to send to the JMS.
           */
          public void sendInput(String xmlMessage) throws JMSException {
              /**
               *  publish the point to the topic
               */
              TextMessage message = this.topicSession.createTextMessage(xmlMessage);
              message.setJMSMessageID("ID:");
              this.topicPublisher.publish(message);
              Logger.getLogger(JMSPublisher.class.getName()).info(message);
              finish();
          }
      
      
          public static void main(String[] args) {
              BasicConfigurator.configure();
              try {
                  getInstance("topic/newtestTopic").sendInput("()()()()()()()()()() Sending some special characters ()()()()()()()()()()");
              } catch (JMSException e) {
                  // TODO Auto-generated catch block
                  Logger.getLogger(JMSPublisher.class.getName()).error("listen da there is an error "+e);
              }
          }
      }