Hi,

WildFly 8.x has become little bit old however it had bring some major changes in way we used to lookup resources in JBossAS7, hence as part of this article we will see what those changes were in the remoting based JNDI lookup approach.

In this article we will see what all different issues we may face while looking up a JMS resource from a Standalone java client and what al things we should keep in mind while doing so. We will also see what all jars are needed at the client classpath. Also how to creation the InitialContext object and which protocol should we use like “remoting” or “http-remoting” etc.

In this example we will be learning the following things:

1. How to create a simple JMS Queue on WildFly 8.x
2. How to create a user in ApplicationRealm and assign it to “guest” role.
3. How to create the InitialContext on the standalone client side.
4. What all jars are needed on the client side in order to lookup the JNDI resources deployed on WildFly.
5. Some common issues which you might encounter while running such java code.

Creating A Simple JMS Queue on WildFly 8.2 side

Step-1). Start the WildFly 8.2 “full” profile (which has messaging) as following:

   $ cd /PATH/TO/wildfly-8.2.1.Final/bin
   $ ./standalone.sh -c standalone-full.xml

Step-2). Create a simple JMS user on WildFly 8.2 side and this user must belong to “guest” role. Please see the “messaging subsystem” configuration of “standalone-full.xml” to know more about “guest” role.
username: jmsuser
password: jmsuser@123
user role: guest
Realm: ApplicationRealm

   $ cd /PATH/TO/wildfly-8.2.1.Final/bin
   $ ./add-user.sh 

What type of user do you wish to add? 
 a) Management User (mgmt-users.properties) 
 b) Application User (application-users.properties)
(a): b

Enter the details of the new user to add.
Using realm 'ApplicationRealm' as discovered from the existing property files.
Username : jmsuser
Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file.
 - The password should not be one of the following restricted values {root, admin, administrator}
 - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s)
 - The password should be different from the username
Password :  jmsuser@123
Re-enter Password :  jmsuser@123
What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: guest
About to add user 'jmsuser' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'jmsuser' to file '/PATH/TO/wildfly-8.2.1.Final/standalone/configuration/application-users.properties'
Added user 'jmsuser' to file '/PATH/TO/wildfly-8.2.1.Final/domain/configuration/application-users.properties'
Added user 'jmsuser' with groups guest to file '/PATH/TO/wildfly-8.2.1.Final/standalone/configuration/application-roles.properties'
Added user 'jmsuser' with groups guest to file '/PATH/TO/wildfly-8.2.1.Final/domain/configuration/application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS process? 
e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls.
yes/no? yes
To represent the user add the following to the server-identities definition <secret value="am1zdXNlckAxMjM=" />

Step-3). Creating a simple JMS Queue using the WildFly CLI command line utility. NOTE the JNDI name should contain “java:/jboss/exported” prefix or else the JMS queue will can not be looked up remotely.

   $ cd /PATH/TO/wildfly-8.2.1.Final/bin
   $ ./jboss-cli.sh -c
   
   [standalone@localhost:9990 /] /subsystem=messaging/hornetq-server=default/jms-queue=TestQ/:add(entries=["java:/jboss/exported/jms/queue/TestQ"],durable=false)
     {"outcome" => "success"}
     
   [standalone@localhost:9990 /] :reload
     {
       "outcome" => "success",
       "result" => undefined
     }

The generated XML snippet in the “standalone-full.xml” file will look like following:

    <jms-destinations>
          <jms-queue name="ExpiryQueue">
                <entry name="java:/jms/queue/ExpiryQueue"/>
          </jms-queue>
          <jms-queue name="DLQ">
                <entry name="java:/jms/queue/DLQ"/>
          </jms-queue>
          <jms-queue name="TestQ">
               <entry name="java:/jboss/exported/jms/queue/TestQ"/>   <!-- Added JMS Destination -->
          </jms-queue>                   
    </jms-destinations>

NOTE: The “java:/jboss/exported” JNDI prefix is needed in the JNDI name here to ensure that a remote client (a client running outside of that JVM where the JNDI name is bound) can do the lookup.

Writing WildFly JMS Client

Step-4). Create a directory somewhere in your filesystem to place the project WildFly kms client project.

  $ mkdir WildFly_JMS_Client
  $ mkdir -p WildFly_JMS_Client/src/client

Step-5). Lets write the “WildFlyJmsQueueSender.java” program which will send some jms messages to the JMS Queue “TestQ” deployed on WildFly and Lets place it inside the “WildFly_JMS_Client/src/client” as following:

package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

//jms stuff
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.IOException;

public class WildFlyJmsQueueSender {
  public final static String JMS_CONNECTION_FACTORY_JNDI="jms/RemoteConnectionFactory";
  public final static String JMS_QUEUE_JNDI="jms/queue/TestQ";
  public final static String JMS_USERNAME="jmsuser";       //  The role for this user is "guest" in ApplicationRealm
  public final static String JMS_PASSWORD="jmsuser@123";  
  public final static String WILDFLY_REMOTING_URL="http-remoting://localhost:8080";

  private QueueConnectionFactory qconFactory;
  private QueueConnection qcon;
  private QueueSession qsession;
  private QueueSender qsender;
  private Queue queue;
  private TextMessage msg;

  public static void main(String[] args) throws Exception {
    InitialContext ic = getInitialContext();
    WildFlyJmsQueueSender queueSender = new WildFlyJmsQueueSender();
    queueSender.init(ic, JMS_QUEUE_JNDI);
    readAndSend(queueSender);
    queueSender.close();
  }

  public void init(Context ctx, String queueName) throws NamingException, JMSException {
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_CONNECTION_FACTORY_JNDI);
    
    //  If you won't pass jms credential here then you will get 
    // [javax.jms.JMSSecurityException: HQ119031: Unable to validate user: null]    
    qcon = qconFactory.createQueueConnection(this.JMS_USERNAME, this.JMS_PASSWORD);   
    
    qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    qsender = qsession.createSender(queue);
    msg = qsession.createTextMessage();
    qcon.start();
  }

  public void send(String message,int counter) throws JMSException {
    msg.setText(message);
    msg.setIntProperty("counter", counter);
    qsender.send(msg);
  }

  public void close() throws JMSException {
    qsender.close();
    qsession.close();
    qcon.close();
  }

  private static void readAndSend(WildFlyJmsQueueSender wildFlyJmsQueueSender) throws IOException, JMSException {
    String line="Test Message Body with counter = ";
    for(int i=0;i<10;i++) {
          wildFlyJmsQueueSender.send(line+i,i);
          System.out.println("JMS Message Sent: "+line+i+"\n");
       }
  }

  private static InitialContext getInitialContext() throws NamingException {
     InitialContext context=null;
     try {
           Properties props = new Properties();
           props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
           props.put(Context.PROVIDER_URL, WILDFLY_REMOTING_URL);   // NOTICE: "http-remoting" and port "8080"
           props.put(Context.SECURITY_PRINCIPAL, JMS_USERNAME);
           props.put(Context.SECURITY_CREDENTIALS, JMS_PASSWORD);
           //props.put("jboss.naming.client.ejb.context", true);
           context = new InitialContext(props);	
	       System.out.println("\n\tGot initial Context: "+context);		
      } catch (Exception e) {
           e.printStackTrace();
      }
    return context;
  }
}

Step-6). Lets write the “WildFlyJmsQueueReceive.java” program which will consume messages from the JMS Queue deployed in WildFly and Lets place it inside the “WildFly_JMS_Client/src/client” as following:

package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

//jms stuff
import javax.jms.JMSException;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Message;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.IOException;

public class WildFlyJmsQueueReceive  implements MessageListener {
  public final static String JMS_CONNECTION_FACTORY_JNDI="jms/RemoteConnectionFactory";
  public final static String JMS_QUEUE_JNDI="jms/queue/TestQ";
  public final static String JMS_USERNAME="jmsuser";       //  The role for this user is "guest" in ApplicationRealm
  public final static String JMS_PASSWORD="jmsuser@123";  
  public final static String WILDFLY_REMOTING_URL="http-remoting://localhost:8080";


  private QueueConnectionFactory qconFactory;
  private QueueConnection qcon;
  private QueueSession qsession;
  private QueueReceiver qReceiver;
  private Queue queue;
  private TextMessage msg;
  private boolean quit = false;

  public static void main(String[] args) throws Exception {
    InitialContext ic = getInitialContext();
    WildFlyJmsQueueReceive wildflyJmsQueueReceive = new WildFlyJmsQueueReceive();
    wildflyJmsQueueReceive.init(ic, JMS_QUEUE_JNDI);
    System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message from QueueSender.class).");
    // Waiting until a "quit" message has been received.
    synchronized(wildflyJmsQueueReceive) {
         while (! wildflyJmsQueueReceive.quit) {
             try {
                   wildflyJmsQueueReceive.wait();
             }
             catch (InterruptedException ie) {
                   ie.printStackTrace();
             }
         }
     }
     wildflyJmsQueueReceive.close();
  }
  
  public void init(Context ctx, String queueName) throws NamingException, JMSException {
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_CONNECTION_FACTORY_JNDI);

    //  If you won't pass jms credential here then you will get 
    // [javax.jms.JMSSecurityException: HQ119031: Unable to validate user: null]    
    qcon = qconFactory.createQueueConnection(this.JMS_USERNAME, this.JMS_PASSWORD);   
    
    qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    qReceiver = qsession.createReceiver(queue);
    qReceiver.setMessageListener(this);
    qcon.start();
  }

  public void onMessage(Message msg) {
     try {
           String msgText;
           if (msg instanceof TextMessage) {
              msgText = ((TextMessage)msg).getText();
           } else {
              msgText = msg.toString();
           }
           System.out.println("\n<Msg_Receiver> "+ msgText );
           if (msgText.equalsIgnoreCase("quit")) {
             synchronized(this) {
             	 quit = true;
             	 this.notifyAll(); // Notify main thread to quit
             }
           }
      } catch (JMSException jmse) {
          jmse.printStackTrace();
     }
  }

  public void close() throws JMSException {
    qReceiver.close();
    qsession.close();
    qcon.close();
  }
  
  private static InitialContext getInitialContext() throws NamingException {
     InitialContext context=null;
     try {
           Properties props = new Properties();
           props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
           props.put(Context.PROVIDER_URL, WILDFLY_REMOTING_URL);   // NOTICE: "http-remoting" and port "8080"
           props.put(Context.SECURITY_PRINCIPAL, JMS_USERNAME);
           props.put(Context.SECURITY_CREDENTIALS, JMS_PASSWORD);
           //props.put("jboss.naming.client.ejb.context", true);
           context = new InitialContext(props);	
	   System.out.println("\n\tGot initial Context: "+context);		
      } catch (Exception e) {
           e.printStackTrace();
      }
    return context;
  }
}

Step-7). In order to run the above program easily we are going to use ANT, However users can also run the above programs manually by adding the “$JBOSS_HOME/bin/client/jboss-client.jar” in the class path of the client. Here we will be writing the following ind of “build.xml” file inside the “WildFly_JMS_Client” as following:

<project name="WildFly_Jms_Client" default="runSender">
<property name="jboss.home" value="/Users/jsensharma/NotBackedUp/Installed/wildfly-8.2.1.Final" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="src.dir" value="src" />
<property name="output.dir" value="build" />
<property name="client.jar.name" value="wildfly_jms_client.jar" />

        <!-- Client Needs the following Jar to be present in the CLASSPATH including -->
        <path id="jboss.new.client.classpath">
           <fileset dir="${jboss.home}/bin/client">
               <include name="jboss-client.jar" />
           </fileset>  
        </path>

        <target name="runSender">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}/client" destdir="${tmp.dir}"  includes="WildFlyJmsQueueSender.java" classpathref="jboss.new.client.classpath"/> 
           <copy file="${src.dir}/client/WildFlyJmsQueueSender.java" todir="${tmp.dir}/client"/>        
           <jar jarfile="${output.dir}/${client.jar.name}" basedir="${tmp.dir}" compress="true" />
           <delete dir="${tmp.dir}"/>
           <java classname="client.WildFlyJmsQueueSender" fork="true" classpathref="jboss.new.client.classpath">
               <classpath>
                  <pathelement location="${output.dir}/${client.jar.name}"/>
               </classpath>
           </java>
        </target>     

        <target name="runReceive">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}/client" destdir="${tmp.dir}"  includes="WildFlyJmsQueueReceive.java" classpathref="jboss.new.client.classpath"/> 
           <copy file="${src.dir}/client/WildFlyJmsQueueReceive.java" todir="${tmp.dir}/client"/>        
           <jar jarfile="${output.dir}/${client.jar.name}" basedir="${tmp.dir}" compress="true" />
           <delete dir="${tmp.dir}"/>
           <java classname="client.WildFlyJmsQueueReceive" fork="true" classpathref="jboss.new.client.classpath">
               <classpath>
                  <pathelement location="${output.dir}/${client.jar.name}"/>
               </classpath>
           </java>
        </target>             
</project>

Running the WildFly 8 JMS client

Step-8). We can run the above ANT based kms client project simply by setting the ANT_HOME, JAVA_HOME and PATH as following :

####### For  Unix Based OS:

$ export ANT_HOME=/PATH/TO/apache_ant_1.9.2
$ export JAVA_HOME=/PATH/TO/jdk1.8.0_60
$ export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
$ cd /PATH/TO/WildFly_JMS_Client


######## For Windows Based OS
$ set ANT_HOME=C:\PATH\TO\apache_ant_1.9.2
$ set JAVA_HOME=C:\PATH\TO\jdk1.8.0_60
$ set PATH=%JAVA_HOME%\bin;%ANT_HOME%\bin;%PATH%
$ cd C:\WildFly_JMS_Client

Step-9). In the same above command prompt, Lets run the “WildFlyJmsQueueSender.java” program which will send to messages to the “TestQ” present on WildFly 8 as following :

$ ant runSender
Buildfile: /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/build.xml

runSender:
    [mkdir] Created dir: /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp
    [javac] /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp/client
      [jar] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/build/wildfly_jms_client.jar
   [delete] Deleting directory /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp
      Nov 08, 2015 12:31:22 PM org.xnio.Xnio <clinit>
      INFO: XNIO version 3.3.0.Final
      Nov 08, 2015 12:31:22 PM org.xnio.nio.NioXnio <clinit>
      INFO: XNIO NIO Implementation Version 3.3.0.Final
      Nov 08, 2015 12:31:22 PM org.jboss.remoting3.EndpointImpl <clinit>
      INFO: JBoss Remoting version 4.0.7.Final
      
      	Got initial Context: javax.naming.InitialContext@aaf5002
      Nov 08, 2015 12:31:23 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
      INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
      Nov 08, 2015 12:31:23 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
      INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@57a37ec1, receiver=Remoting connection EJB receiver [connection=Remoting connection <e244821>,channel=jboss.ejb,nodename=banl13bca644a-3]} on channel Channel ID fb329c1b (outbound) of Remoting connection 45106993 to localhost/127.0.0.1:8080
      Nov 08, 2015 12:31:23 PM org.jboss.ejb.client.EJBClient <clinit>
      INFO: JBoss EJB Client version 2.0.1.Final
      JMS Message Sent: Test Message Body with counter = 0
      
      JMS Message Sent: Test Message Body with counter = 1
      
      JMS Message Sent: Test Message Body with counter = 2
      
      JMS Message Sent: Test Message Body with counter = 3
      
      JMS Message Sent: Test Message Body with counter = 4
      
      JMS Message Sent: Test Message Body with counter = 5
      
      JMS Message Sent: Test Message Body with counter = 6
      
      JMS Message Sent: Test Message Body with counter = 7
      
      JMS Message Sent: Test Message Body with counter = 8
      
      JMS Message Sent: Test Message Body with counter = 9
      

BUILD SUCCESSFUL
Total time: 3 seconds

Step-10). In the same above command prompt, Lets run the “WildFlyJmsQueueReceive.java” program which will receive the jms messages which are present in the “TestQ” present on WildFly 8 as following :

$ ant runReceive
Buildfile: /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/build.xml

runReceive:
    [mkdir] Created dir: /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp
    [javac] /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/build.xml:34: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 1 source file to /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp
     [copy] Copying 1 file to /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp/client
      [jar] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/build/wildfly_jms_client.jar
   [delete] Deleting directory /Users/jsensharma/NotBackedUp/MM_Tests/WildFly/WildFly_JMS_Client/tmp
      Nov 08, 2015 12:33:39 PM org.xnio.Xnio <clinit>
      INFO: XNIO version 3.3.0.Final
      Nov 08, 2015 12:33:39 PM org.xnio.nio.NioXnio <clinit>
      INFO: XNIO NIO Implementation Version 3.3.0.Final
      
      	Got initial Context: javax.naming.InitialContext@4a3e91e7
      Nov 08, 2015 12:33:39 PM org.jboss.remoting3.EndpointImpl <clinit>
      INFO: JBoss Remoting version 4.0.7.Final
      Nov 08, 2015 12:33:39 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
      INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
      Nov 08, 2015 12:33:39 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
      INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@1c7f2b3c, receiver=Remoting connection EJB receiver [connection=Remoting connection <526d2bf>,channel=jboss.ejb,nodename=banl13bca644a-3]} on channel Channel ID db01bd53 (outbound) of Remoting connection 365655d3 to localhost/127.0.0.1:8080
      Nov 08, 2015 12:33:40 PM org.jboss.ejb.client.EJBClient <clinit>
      INFO: JBoss EJB Client version 2.0.1.Final
      JMS Ready To Receive Messages (To quit, send a "quit" message from QueueSender.class).
      
      <Msg_Receiver> Test Message Body with counter = 0
      
      <Msg_Receiver> Test Message Body with counter = 1
      
      <Msg_Receiver> Test Message Body with counter = 2
      
      <Msg_Receiver> Test Message Body with counter = 3
      
      <Msg_Receiver> Test Message Body with counter = 4
      
      <Msg_Receiver> Test Message Body with counter = 5
      
      <Msg_Receiver> Test Message Body with counter = 6
      
      <Msg_Receiver> Test Message Body with counter = 7
      
      <Msg_Receiver> Test Message Body with counter = 8
      
      <Msg_Receiver> Test Message Body with counter = 9

.
.
What next …… ? See below
.
.
.
.
.

Common Issues

In this section we will discuss some common issues which you will encounter while running this project:

Missing jboss-client.jar in classpath

On the wildly client side we need to make sure that the “$JBOSS_HOME/bin/client/jboss-client.jar” is added to the classpath. Else we will see the following kind of exception:

 javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
 	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
 	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
 	at javax.naming.InitialContext.init(InitialContext.java:242)
 	at javax.naming.InitialContext.<init>(InitialContext.java:216)
 	at client.WildFlyJmsQueueSender.getInitialContext(Unknown Source)
 	at client.WildFlyJmsQueueSender.main(Unknown Source)
 Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
 	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 	at java.security.AccessController.doPrivileged(Native Method)
 	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
 	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
 	at java.lang.Class.forName0(Native Method)
 	at java.lang.Class.forName(Class.java:274)
 	at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
 	at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
 	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
  	... 5 more
 Exception in thread "main" java.lang.NullPointerException
 	at client.WildFlyJmsQueueSender.init(Unknown Source)
 	at client.WildFlyJmsQueueSender.main(Unknown Source)
 Java Result: 1

Incorrect protocol issue

If you will use incorrect protocol to communicate with WildFly then while running the client you will receive the following kind of exception on the client side. The following error indicates that user is trying to use the JBossAS7 specific “remote:” protocol in WildFly where as in WildFly it is changed to “http-remoting”.

 Exception in thread "main" javax.naming.CommunicationException: Failed to connect to any server. Servers tried: [remote://localhost:4447 (java.net.ConnectException: Connection refused)]
 	at org.jboss.naming.remote.client.HaRemoteNamingStore.failOverSequence(HaRemoteNamingStore.java:244)
 	at org.jboss.naming.remote.client.HaRemoteNamingStore.namingStore(HaRemoteNamingStore.java:149)
 	at org.jboss.naming.remote.client.HaRemoteNamingStore.namingOperation(HaRemoteNamingStore.java:130)
 	at org.jboss.naming.remote.client.HaRemoteNamingStore.lookup(HaRemoteNamingStore.java:272)
 	at org.jboss.naming.remote.client.RemoteContext.lookup(RemoteContext.java:87)
 	at org.jboss.naming.remote.client.RemoteContext.lookup(RemoteContext.java:129)
 	at javax.naming.InitialContext.lookup(InitialContext.java:411)
 	at client.WildFlyJmsQueueSender.init(Unknown Source)
 	at client.WildFlyJmsQueueSender.main(Unknown Source)
 Java Result: 1

Incorrect InitialContext creation issue

While using the Remoting based approach for JNDI lookup if users are not creating the JNDI name properly then they may see the following error, So users need to see how they are creating the InitialContext using appropriate Context Factory class and the protocols and making sure that the “jboss-client.jar” is added to the classpath of the client. So please check the above code and create the InitialContext as described above for WildFly 8.

If you are planning to use the “EJB Client API” based approach to invoke the EJBs deployed on WildFly then please have a look at : http://middlewaremagic.com/jboss/?p=2744 to know how to create the InitialContext.

If you are planning to use the “Remote-Naming project” based approach to invoke the EJBs deployed on WildFly then please have a look at : http://middlewaremagic.com/jboss/?p=2704 to know how to create the InitialContext.

 Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
 	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
 	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
 	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
 	at javax.naming.InitialContext.lookup(InitialContext.java:411)
 	at client.WildFlyJmsQueueSender.init(Unknown Source)
 	at client.WildFlyJmsQueueSender.main(Unknown Source)
      Java Result: 1

Not Passing the JMS user while getting JMS Connection

If we are not passing the jams user name and password while creation the connection we will see the following kind of exception (Notice the “user: null” issue)

So make sure to pass the JMS credentials while creation JMS Connection:

queueConnection = queueConnectionFactory(“jmsuser”, “jmsuser@123”);

WildFly Side
############
13:03:34,727 ERROR [org.hornetq.core.server] (default I/O-7) HQ224018: Failed to create session: HornetQSecurityException[errorType=SECURITY_EXCEPTION message=HQ119031: Unable to validate user: null]
	at org.hornetq.core.security.impl.SecurityStoreImpl.authenticate(SecurityStoreImpl.java:143)
	at org.hornetq.core.server.impl.HornetQServerImpl.createSession(HornetQServerImpl.java:1020)
	at org.hornetq.core.protocol.core.impl.HornetQPacketHandler.handleCreateSession(HornetQPacketHandler.java:149)
	at org.hornetq.core.protocol.core.impl.HornetQPacketHandler.handlePacket(HornetQPacketHandler.java:77)
	at org.hornetq.core.protocol.core.impl.ChannelImpl.handlePacket(ChannelImpl.java:641)
	at org.hornetq.core.protocol.core.impl.RemotingConnectionImpl.doBufferReceived(RemotingConnectionImpl.java:556)
	at org.hornetq.core.protocol.core.impl.RemotingConnectionImpl.bufferReceived(RemotingConnectionImpl.java:532)
	at org.hornetq.core.remoting.server.impl.RemotingServiceImpl$DelegatingBufferHandler.bufferReceived(RemotingServiceImpl.java:658)
	at org.hornetq.core.remoting.impl.netty.HornetQChannelHandler.channelRead(HornetQChannelHandler.java:73)
	at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:338)
	at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:324)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:153)
	at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:338)
	at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:324)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785)
	at org.xnio.netty.transport.AbstractXnioSocketChannel$ReadListener.handleEvent(AbstractXnioSocketChannel.java:435)
	at org.xnio.netty.transport.AbstractXnioSocketChannel$ReadListener.handleEvent(AbstractXnioSocketChannel.java:371)
	at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92) [xnio-api-3.3.0.Final.jar:3.3.0.Final]
	at org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66) [xnio-api-3.3.0.Final.jar:3.3.0.Final]
	at org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:88) [xnio-nio-3.3.0.Final.jar:3.3.0.Final]
	at org.xnio.nio.WorkerThread.run(WorkerThread.java:539) [xnio-nio-3.3.0.Final.jar:3.3.0.Final]


Client Side
############
 Exception in thread "main" javax.jms.JMSSecurityException: HQ119031: Unable to validate user: null
  	at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:399)
  	at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSessionInternal(ClientSessionFactoryImpl.java:891)
 	at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSessionInternal(ClientSessionFactoryImpl.java:800)
 	at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSession(ClientSessionFactoryImpl.java:337)
 	at org.hornetq.jms.client.HornetQConnection.authorize(HornetQConnection.java:719)
 	at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:762)
 	at org.hornetq.jms.client.HornetQConnectionFactory.createQueueConnection(HornetQConnectionFactory.java:181)
 	at org.hornetq.jms.client.HornetQConnectionFactory.createQueueConnection(HornetQConnectionFactory.java:176)
 	at client.WildFlyJmsQueueSender.init(Unknown Source)
 	at client.WildFlyJmsQueueSender.main(Unknown Source)
 Caused by: HornetQSecurityException[errorType=SECURITY_EXCEPTION message=HQ119031: Unable to validate user: null]
 	... 10 more
 Java Result: 1

JMS user does not have correct role

If JMS user which we created in the ApplicationRealm does not have the correct role (in our case default role “guest”) then we will see the following exception while sending/receiving messages to/from JMS destinations.

 Exception in thread "main" javax.jms.JMSSecurityException: HQ119032: User: abcd doesnt have permission=SEND on address {2}
 	at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:399)
 	at org.hornetq.core.client.impl.ClientProducerImpl.sendRegularMessage(ClientProducerImpl.java:334)
 	at org.hornetq.core.client.impl.ClientProducerImpl.doSend(ClientProducerImpl.java:304)
 	at org.hornetq.core.client.impl.ClientProducerImpl.send(ClientProducerImpl.java:135)
	at org.hornetq.jms.client.HornetQMessageProducer.doSendx(HornetQMessageProducer.java:524)
 	at org.hornetq.jms.client.HornetQMessageProducer.send(HornetQMessageProducer.java:187)
 	at client.WildFlyJmsQueueSender.send(Unknown Source)
 	at client.WildFlyJmsQueueSender.readAndSend(Unknown Source)
 	at client.WildFlyJmsQueueSender.main(Unknown Source)
 Caused by: HornetQSecurityException[errorType=SECURITY_EXCEPTION message=HQ119032: User: abcd doesnt have permission=SEND on address {2}]
 	... 9 more
 Java Result: 1

So make sure that the user (in above case “abcd”) has the proper role (like “guest” and belongs to correct realm like ApplicationRealm), better see the wildly configuration “standalone-full.xml” to see

                <security-settings>
                    <security-setting match="#">
                        <permission type="send" roles="guest"/>
                        <permission type="consume" roles="guest"/>
                        <permission type="createNonDurableQueue" roles="guest"/>
                        <permission type="deleteNonDurableQueue" roles="guest"/>
                    </security-setting>
                </security-settings>

The Source code of this demo can be found at:
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WildFly/JMS/WildFly_JMS_Client

.
.

Regards
Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.