2 Replies Latest reply on May 13, 2015 10:22 AM by Miroslav Novak

    Sending Messages from Standalone JMS Client Program To JBOSS EAP6.1 AS

    Arun Thirunavukkarasu Newbie

      Hi,

       

      I am getting below exception while trying to send messages from Standalone JMS Client Program To JBOSS EAP6.1 AS.

       

      Can you please help me to resolve this issue.

       

       

       

      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.test.HelloWorldJMS.main(HelloWorldJMS.java:52)

      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)

          ... 4 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)

          ... 5 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)

          ... 5 more

       

       

      HelloWorldJMS.java:

       

      package com.test;

       

      import java.util.Hashtable;

       

      import javax.jms.Connection;

      import javax.jms.ConnectionFactory;

      import javax.jms.Message;

      import javax.jms.MessageConsumer;

      import javax.jms.MessageProducer;

      import javax.jms.Queue;

      import javax.jms.Session;

      import javax.jms.TextMessage;

      import javax.naming.Context;

      import javax.naming.InitialContext;

       

      public class HelloWorldJMS {

       

          public static void main(String[] args) {

       

              try {

       

                  /*

                   *

                   *

                   * Connecting to JBoss naming service running on local host and on

                   *

                   *

                   * default port 1099 the environment that should be created is like

                   * the

                   *

                   *

                   * one shown below :

                   */

       

                  Hashtable<String, String> env = new Hashtable();

       

                  env.put(Context.INITIAL_CONTEXT_FACTORY,

                          "org.jnp.interfaces.NamingContextFactory");

       

                  env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

       

                  env.put(Context.URL_PKG_PREFIXES,

                          "org.jboss.naming:org.jnp.interfaces");

       

                  // Create the initial context

       

                  Context ctx = new InitialContext(env);

       

                  // Lookup the JMS connection factory from the JBoss 5.1 object store

                 

                  ConnectionFactory connectionFactory = (ConnectionFactory) ctx

                          .lookup("/QueueConnectionFactory");

                 

                  System.out.println("connectionFactory = "+connectionFactory);

                 

                 

                  Queue queue = (javax.jms.Queue) ctx.lookup("/queue/testQueue");

                 

                 

                  // Create a connection to the JBoss 5.1 Message Service

       

                  Connection connection = connectionFactory.createConnection();

       

                  // Create a session within the connection

       

                  Session session = connection.createSession(false,

                          Session.AUTO_ACKNOWLEDGE);

       

                  // Create a message producer to put messages on the queue

       

                  MessageProducer messageProducer = session.createProducer(queue);

       

                  // Create and send a message to the queue

       

                  TextMessage textMessage = session.createTextMessage();

       

                  textMessage.setText("Hello World");

       

                  System.out.println("Sending Message: " + textMessage.getText());

       

                  messageProducer.send(textMessage);

       

                  // Create a message consumer

       

                  MessageConsumer messageConsumer = session.createConsumer(queue);

       

                  // Start the Connection created

       

                  connection.start();

       

                  // Receive a message from the queue.

       

                  Message msg = messageConsumer.receive();

       

                  // Retrieve the contents of the message.

       

                  if (msg instanceof TextMessage) {

       

                      TextMessage txtMsg = (TextMessage) msg;

       

                      System.out.println("Read Message: " + txtMsg.getText());

       

                  }

       

                  // Close the session and connection resources.

       

                  session.close();

       

                  connection.close();

       

              } catch (Exception ex) {

       

                  ex.printStackTrace();

              }

          }

      }

      - See more at: https://developer.jboss.org/thread/256585#sthash.28Lby1aM.dpuf