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

    JMS publisher calling JBoss AS7 problem.

    haroonfoad

      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);
              }
          }
      }
      
      
        • 1. Re: JMS publisher calling JBoss AS7 problem.
          haroonfoad

          Libraries that I added to the project are:

          1.jboss-client.jar

          2.log4j.jar

          3.jnpserver.jar

          • 2. Re: JMS publisher calling JBoss AS7 problem.
            jbertram

            You're getting an error because you doing it wrong for at least these reasons (maybe more):

             

            1. Port 5445 is a Netty port and not suitable for JNDI look-ups.
            2. Even if you had the port right your InitialContext properties are wrong.

             

            Look at the JMS quick-start for AS7.

             

            Also, as far as JBoss libraries goe you should only need <JBOSS_HOME>/bin/client/jboss-client.jar on your classpath.

            • 3. Re: JMS publisher calling JBoss AS7 problem.
              haroonfoad

              Thanks Mr.Justin for your help.

              I followed the example you provided and it worked fine for me .But I have two questions:

               

              1. I wonder why should I use remote calling?

              2. What if I make the subscriber as a servlet, how jndi lookup will look like?

              • 4. Re: JMS publisher calling JBoss AS7 problem.
                jbertram

                I assumed you needed an example of a remote client because you were using a non-blank InitialContext.  Basically the only reason you would ever use a non-blank InitialContext is if you are performing a remote look-up.  If you are performing a local look-up you should use a blank InitialContext.  Also, in the case of a local look-up for a JMS connection factory you would either look-up "java:/ConnectionFactory" or "java:/JmsXA" depending on your use-case.  See the documentation for more details on which kind of connection factory is suitable for you.

                1 of 1 people found this helpful