2 Replies Latest reply on Oct 13, 2010 5:02 AM by freezxq zhang

    hornetq client throw exception

    freezxq zhang Newbie

      hi:

           when I code a hornetq client there is a exception ,the exception :

       

      Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
      at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
      at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.init(Unknown Source)
      at javax.naming.InitialContext.<init>(Unknown Source)
      at com.tao.hornetq_pressure.HornetqPublish.<init>(HornetqPublish.java:66)
      at com.tao.hornetq_pressure.HornetqPublish.main(HornetqPublish.java:110)
      Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClassInternal(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
      ... 6 more

       

       

      the follows are the source code:

       

      package com.tao.hornetq_pressure;
      import java.util.Properties;

      import javax.jms.BytesMessage;
      import javax.jms.DeliveryMode;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.Session;
      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;

       

      public class HornetqPublish {

      private static Context context = null;
      //private TopicConnectionFactory topicFactory = null;
      private static TopicConnection topicConnection = null;
      private TopicSession topicSession = null;
      private TopicPublisher topicPublisher = null;
      private Topic topic = null;

      private String provider_url = "tcp://10.232.20.116:1099";
      private static HornetqPublish  instance = null;


      private boolean transacted = true;
      private boolean durable = true;
      private int messageSize = 512;
      private int messageType = 0;
      private String messagetopic = "/topic/exampleTopic";

      private HornetqPublish(){
        provider_url = "jnp://127.0.0.1" + ":1099";
      }

      synchronized static public HornetqPublish instance() {
        if (null == instance) {
        instance = new HornetqPublish();
        }
        return instance;
        }

      public void init(String serverIP) {
        this.provider_url = "jnp://" + serverIP + ":1099";
        }

      public HornetqPublish(boolean transacted, boolean durable, int messageSize, int messageType) throws NamingException, JMSException{
        this.transacted = transacted;
        this.durable = durable;
        this.messageSize = messageSize;
        this.messageType = messageType;
       
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
        properties.put(Context.URL_PKG_PREFIXES,
        "org.jboss.naming:org.jnp.interfaces");
        properties.put(Context.PROVIDER_URL, provider_url);
        context = new InitialContext(properties);
        TopicConnectionFactory topicFactory =  
         (TopicConnectionFactory)context.lookup("/ConnectionFactory");
        topicConnection = topicFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        topic = topicSession.createTopic(messagetopic);
        topicPublisher = topicSession.createPublisher(topic);
        if (this.durable)
         topicPublisher.setDeliveryMode(DeliveryMode.PERSISTENT);
        else
         topicPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
      public void publish(){
        try {
                  Message message = null;
                  switch (this.messageType) {
                  // ByteMessage
                  case 0:
                      BytesMessage byteMessage = topicSession.createBytesMessage();
                      message = byteMessage;
                      byteMessage.writeBytes(Utils.createBinaryData(this.messageSize));
                      break;
                  default:

                      break;
                  }
                  // 开始发送消息
                  topicPublisher.publish(message);
                  // 事务消息还需要手动提交
                  if (this.transacted)
                      topicSession.commit();

                  System.out.println("消息发送完成...");
              }
              catch (JMSException e) {
                  try {
                      topicSession.rollback();
                  }
                  catch (JMSException e1) {
                      e1.printStackTrace();
                  }
              }
          }
      public static void main (String args[]) throws NamingException, JMSException{
        HornetqPublish hp = new HornetqPublish(true, true,512,0);
        //hp.init("10.232.20.116");
        hp.publish();
       
      }

      }

       

       

       

      how can  I do ?

        • 1. Re: hornetq client throw exception
          Jeff Mesnil Master

          freezxq zhang wrote:

           

          hi:

               when I code a hornetq client there is a exception ,the exception :

           

          Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]

           

          how can  I do ?

          http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/user-manual/en/html/client-classpath.html#d0e1565

          • 2. Re: hornetq client throw exception
            freezxq zhang Newbie

            hi:I hava add the jnp-client.jar to the classpath,and i have start the hornet server on 10.232.20.116,but when i run the program,the program also come up exception,the exception as follows:

             

             

            Exception in thread "main" javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
            at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1763)
            at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:693)
            at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
            at javax.naming.InitialContext.lookup(Unknown Source)
            at com.tao.hornetq_pressure.HornetqPublish.<init>(HornetqPublish.java:67)
            at com.tao.hornetq_pressure.HornetqPublish.main(HornetqPublish.java:109)
            Caused by: javax.naming.CommunicationException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]
            at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:335)
            at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1734)
            ... 5 more
            Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]
            at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:305)
            ... 6 more
            Caused by: java.net.ConnectException: Connection refused: connect
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.PlainSocketImpl.doConnect(Unknown Source)
            at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
            at java.net.PlainSocketImpl.connect(Unknown Source)
            at java.net.SocksSocketImpl.connect(Unknown Source)
            at java.net.Socket.connect(Unknown Source)
            at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:97)
            at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:82)
            at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:301)
            ... 6 more