1 Reply Latest reply on Oct 13, 2010 10:51 AM by clebert.suconic

    the client cann`t connect server via jndi

    xiaoqiang

      when I run my program of client,it tell me I cann`t connect the server,i know maybe the jndi config is wrong,the part of performing a lookup on the Connection Factory has a program,the config of hornet server in hornet-jms.xml as follows:

       

      <configuration xmlns="urn:hornetq"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">

      <connection-factory name="NettyConnectionFactory">
          <connectors>
               <connector-ref connector-name="netty"/>
          </connectors>
        <entries>
         <entry name="/ConnectionFactory"/>
         <entry name="/XAConnectionFactory"/>
        </entries>
      </connection-factory>

      <queue name="DLQ">
        <entry name="/queue/DLQ"/>
      </queue>
      <queue name="ExpiryQueue">
        <entry name="/queue/ExpiryQueue"/>
      </queue>  
      <queue name="ExampleQueue">
        <entry name="/queue/ExampleQueue"/>
      </queue>
      <topic name="ExampleTopic">
        <entry name="/topic/ExampleTopic"/>
      </topic>

      </configuration>

       

       

       

      the program of my client are thatthe red part maybe wrong)

       

      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 = "jnp://10.232.20.119: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";


      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");
        topic = (Topic) context.lookup(messagetopic);
        topicConnection = topicFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        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.publish();
       
      }

      }

       

      the runtime exception:

      Exception in thread "main" javax.naming.CommunicationException: Could not obtain connection to any of these urls: 10.232.20.119: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 /10.232.20.119:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server /10.232.20.119: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:110)
      Caused by: javax.naming.CommunicationException: Failed to connect to server /10.232.20.119:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server /10.232.20.119: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 /10.232.20.119: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

       

       

      who can help me ,thanks!