5 Replies Latest reply on Apr 15, 2011 4:24 AM by deryaaltuntas

    javax.naming.CommunicationException

    dimagh

      Hello Folks:

      I am trying to run the following simple JMS client but in process am getting an error. Pls. look at the bottom of the post.


      I am not sure if the factory implementations and the corresponding URL/port number are correct (Pls. see the code in red below). I am running JBoss 4.0.3 locally where this program is running.

      ---------------------------------------------------------------------------------
      package com.message;

      import java.util.Hashtable;
      import javax.jms.*;
      import javax.naming.*;

      // Topic client that sends message on a topic
      public class TopicSendClient {

      TopicConnection conn = null;
      TopicSession session = null;
      Topic topic = null;

      // Main Method
      public static void main(String[] args) {
      System.out.println("Starting the topic publisher client");

      TopicSendClient client = new TopicSendClient();
      client.sendMessage("This is test message sent at: " + System.currentTimeMillis());
      try {
      client.stop();
      } catch (JMSException e) {
      System.out.println("Exception: " + e);
      }


      System.out.println("Stopping the topic publisher client");
      System.exit(0);
      }

      public void setupPubSub() throws JMSException, NamingException {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      env.put(Context.PROVIDER_URL, "http://localhost:1099/");


      InitialContext intCtx = new InitialContext(env);
      Object obj = intCtx.lookup("ConnectionFactory");

      TopicConnectionFactory tcf = (TopicConnectionFactory)obj;
      conn = tcf.createTopicConnection();

      topic = (Topic)intCtx.lookup("topic/polkTopic");
      session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

      conn.start();
      }

      public void sendMessage(String text) {
      System.out.print("Getting ready to send message on topic: topic/polkTopic");

      try {
      setupPubSub();
      } catch (JMSException e) {
      System.out.println("Exception: " + e);
      } catch (NamingException e) {
      System.out.println("Exception: " + e);
      }

      try {
      TopicPublisher topicPub = session.createPublisher(topic);
      TextMessage msg = session.createTextMessage(text);

      topicPub.publish(msg);
      System.out.print("Sent message: " + "'" + msg.getText() + ", " + "on topic: topic/polkTopic");
      topicPub.close();
      } catch (JMSException e) {
      System.out.println("Exception: " + e);
      }

      System.out.println("Ending the TopicSendClient job!");
      }

      public void stop() throws JMSException {
      conn.stop();
      session.close();
      conn.close();
      }
      }
      ---------------------------------------------------------------------------------


      Following is the exception I am getting:

      ---------------------------------------------------------------------------------
      topic/polkTopicException: javax.naming.CommunicationException: Could not obtain connection to any of these urls: http://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 http:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server http:1099 [Root exception is java.net.UnknownHostException: http: http]]]
      java.lang.NullPointerException
      at com.message.TopicSendClient.sendMessage(TopicSendClient.java:60)
      at com.message.TopicSendClient.main(TopicSendClient.java:19)
      Exception in thread "main"
      ---------------------------------------------------------------------------------

      Any help would be appreciated.

      Thanks

      - AK

        • 1. Re: javax.naming.CommunicationException
          logas

          Hi!

          Try changing:

          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
          env.put(Context.PROVIDER_URL, "http://localhost:1099/");
          InitialContext intCtx = new InitialContext(env);

          For:

          Properties props = new Properties();
          props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
          props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
          props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
          InitialContext intCtx = new InitialContext( props );

          This is what I'm using in my client.


          The topic polkTopic has to be defined in the file jbossmq-destinations-service.xml

          The problem could also be the jar files, check you have all that JMS & JBoss need.

          Good Luck !!

          • 2. Re: javax.naming.CommunicationException
            linezfanatix

            I also faced this issue of communication exception: Request times out while accessing an EJB deployed on Jboss 4.0.2.

            The solution to this problem is to open the JMX-Console and click on the service=Naming to view the MBean view of the Naming service. Check if the port used is still 1099. In my case (to my surprise) the port was 1299.
            Changed the URL to jnp://localhost:1299, the client could communicate with the EJB.

            • 3. Re: javax.naming.CommunicationException
              deryaaltuntas

              I am receiving same error.My client class is below.In lookup method Communication error exits.I will send print of JMX-Console service=Naming

              screen.I see that my port is 1098.I am setting correct URL?

               

              public class Test {

              /**
                * @param args
                */
              public Test() {
              }


              public static void main(String[] args) throws FileNotFoundException, IOException, SecurityException{
                Test searchFacadeTest = new Test();
                searchFacadeTest.doTest();
              }


              public void doTest() throws FileNotFoundException, IOException{
                try {

                 Context jndiContext = getInitialContext();
                 FileManagerBeanRemote fileManager= (FileManagerBeanRemote)jndiContext.lookup("FileManagerBean/remote");
                 //fileManager.moveFile("C:\\test.txt", "C:\\Documents and Settings\\tr1a6358\\bowling.txt");
                }
                catch (Exception e) {
                 e.printStackTrace();
                }
              }

               

              // developed for JBoss only. this is vender dependency
              public static Context getInitialContext( ) throws javax.naming.NamingException {
                Properties p = new Properties( );
                p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                p.put(Context.PROVIDER_URL, "jnp://localhost:1098");
                Context context = new InitialContext(p);
                return context;
              }

              }

              • 4. javax.naming.CommunicationException
                wdfink

                If nothing was changed, and you jmx looks like! You should use 'jnp://localhost:1099' or 'localhost:1099' works also.

                How do you start JBoss, did you use a different binding with -b option?

                Does your client start on the same system that you can use localhost?

                • 5. Re: javax.naming.CommunicationException
                  deryaaltuntas

                  I have solved my problem Wolf-Dieter.Thanks alot.I am starting JBoss from MyEclipseIde not from console