1

Can anyone tell me why my JMS Sessoin's createConsumer () throws a NullPointerException documentation says it doesn' throw?

I've been struggling for weeks to get a simple Java Class that can establish a JMS connection and exchange messages with WildFly ... and failing miserably.

Its Eclipse Neon and Wildfly 10. (The working class I once had with WF-8 defaulted to Hornert. Now I guess WF-10 defaluts to ArtemisMQ.)

Please find below:

ECLIPSE CONSOLE OUTPUT WITH STACK TRACE

STANDALONE JAVE CLIENT CODE

JARS ACCUMULATED SO FAR IN THE BUILD PATH (I doubt all are needed, but have been picked up as a wandered through posts about this and earlier problems with WF-10)

Thanks for any help.

NOTE: The code has an insane about of printing lines to format the Console output in a way that leaves no no doubt where the error is thrown. And the many irrelevant methods pasted at the bottom are just helpers I normally just import from my utility classes. But I pasted them in, because the goal is to leave behind a straight forward WORKING example of a remote Java client messaging WF-10, so I had to paste the helpers in so this code will be free standing. I.e. something a future newbie can just cut and paste and have a working starting point.

But stipping out all the printing code from main () leaves these 7 "simple" lines.)

Context ctx = new InitialContext(ENV); // Set Wildfly Initial Context
ConnectionFactory factory = (ConnectionFactory)ctx.lookup("jms/RemoteConnectionFactory");
Connection jmsConnection = factory.createConnection(
    ENV.getProperty(Context.SECURITY_PRINCIPAL),
    ENV.getProperty(Context.SECURITY_CREDENTIALS));
jmsConnection.start();
Session jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue jmsSendToClientQueue = (Queue) ctx.lookup("jms/queue/sendToClientQueue");
MessageConsumer jmsMsgConsumer = jmsSession.createConsumer(jmsSendToClientQueue, JMS_SELECTOR);

Help!

ECLIPSE CONSOLE OUTPUT WITH STACK TRACE

TestArtemisJmsWF10.main ()                   beg

  TestArtemisJmsWF10.main ()                   Requesting InitialContext with:
        CONNECTION VARIABLES
        key: java.naming.provider.url         value: http-remoting://localhost:8080
        key: java.naming.factory.initial      value: org.jboss.naming.remote.client.InitialContextFactory
        key: java.naming.security.principal   value: jmsUser
        key: java.naming.security.credentials value: jmsUser123!

  TestArtemisJmsWF10.main ()                   InitialContext ok: javax.naming.InitialContext@4bbfb90a

  TestArtemisJmsWF10.main ()                   Lookup ConnectionFactory
  TestArtemisJmsWF10.main ()                   JNDI lookUp name: "jms/RemoteConnectionFactory"
  TestArtemisJmsWF10.main ()                   ConnectionFactory: Ok: 
                                               [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory]

  TestArtemisJmsWF10.main ()                   JMS Connection: Ok:
                                               [org.apache.activemq.artemis.jms.client.ActiveMQConnection@6f03482]

  TestArtemisJmsWF10.main ()                   Starting jmsConnection & initiating Session
  TestArtemisJmsWF10.main ()                   Session ok - ActiveMQSession->ClientSessionImpl [name=b7cc0e2b-877b-11e7-a483-3ca9f4beb648, username=jmsUser, closed=false, factory = org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl@35fc6dc4, metaData=(jms-session=,)]@9d5509a

  TestArtemisJmsWF10.main ()                   Lookup Queue
  TestArtemisJmsWF10.main ()                   JNDI lookUp name: "jms/queue/sendToClientQueue"
  TestArtemisJmsWF10.main ()                   Queue secured: org.apache.activemq.artemis.jms.client.ActiveMQQueue

  TestArtemisJmsWF10.main ()                   Calling createConsumer() with selector: selector='AAAAA'

  TestArtemisJmsWF10.main ()                   Caught: java.lang.NullPointerException
                                               when Creating JMS Message Consumer
                                               Msg   : null
TestArtemisJmsWF10.main ()                   end - error
java.lang.NullPointerException
    at org.apache.activemq.artemis.jms.client.ActiveMQDestination$TYPE.isTemporary(ActiveMQDestination.java:397)
    at org.apache.activemq.artemis.jms.client.ActiveMQDestination.isTemporary(ActiveMQDestination.java:308)
    at org.apache.activemq.artemis.jms.client.ActiveMQSession.createConsumer(ActiveMQSession.java:367)
    at org.apache.activemq.artemis.jms.client.ActiveMQSession.createConsumer(ActiveMQSession.java:350)
    at org.america3.gotest.xtra.TestArtemisJmsWF10.main(TestArtemisJmsWF10.java:65)

STANDALONE JAVA CLIENT CODE

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;

public class TestArtemisJmsWF10 {

  static final Properties ENV = new Properties() {
    private static final long serialVersionUID = 1L;
    {
      put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); 
      put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
      put(Context.SECURITY_PRINCIPAL, "jmsUser"); 
      put(Context.SECURITY_CREDENTIALS, "jmsUser123!"); 
    }
  };
  static final String JMS_SELECTOR = "selector=\'AAAAA\'";

  public static void main (String args[]) {
    boolean p = true; String iAmM = null; String s = "  "; String errAt = "";
    if (p) iAmM = TestArtemisJmsWF10.getIAm(Thread.currentThread().getStackTrace());
    if (p) P(iAmM + "beg\n");
    if (p) P(s + iAmM + "Requesting InitialContext with:");
    if (p) P(printInitialContextProperties(8, ENV));
    try {
      errAt = "Requesting InitialContext";
      Context ctx = new InitialContext(ENV); // Set Wildfly Initial Context
      if (p) P("\n" + s + iAmM + "InitialContext ok: " + ctx);

      errAt = "LookingUp  ConnectionFactory";
      if (p) P("\n" + s + iAmM + "Lookup ConnectionFactory");
      if (p) P(s + iAmM + "JNDI lookUp name: \"jms/RemoteConnectionFactory\"");
      ConnectionFactory factory = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
      if (p) P(s + iAmM + "ConnectionFactory: Ok: " );
      if (p) P(s + makeMargin(iAmM.length()) + "[" + factory.getClass().getName() +"]");

      errAt = "Requesting Connection";
      Connection jmsConnection = factory.createConnection(
          ENV.getProperty(Context.SECURITY_PRINCIPAL),
          ENV.getProperty(Context.SECURITY_CREDENTIALS));
      if (p) P("\n" + s + iAmM + "JMS Connection: Ok:");
      if (p) P(s + makeMargin(iAmM.length()) + "[" + jmsConnection + "]");

      if (p) P("\n" + s + iAmM + "Starting jmsConnection & initiating Session");
      errAt = "Starting Connection";
      jmsConnection.start();
      errAt = "Creating Session";
      Session jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      if (p) P(s + iAmM + "Session ok - " + jmsSession);

      errAt = "Looking Up Queue";
      if (p) P("\n" + s + iAmM + "Lookup Queue");
      if (p) P(s + iAmM + "JNDI lookUp name: \"jms/queue/sendToClientQueue\"");
      Queue jmsSendToClientQueue = (Queue) ctx.lookup("jms/queue/sendToClientQueue");
      if(p) P(s + iAmM + "Queue secured: " + jmsSendToClientQueue.getClass().getName());

      errAt = "Creating JMS Message Consumer";
      if(p) P("\n" + s + iAmM + "Calling createConsumer() with selector: " + JMS_SELECTOR);
      MessageConsumer jmsMsgConsumer = jmsSession.createConsumer(jmsSendToClientQueue, JMS_SELECTOR);
      if(p) P(s + iAmM + "Consumer secured: " + jmsMsgConsumer);
      if (p) P(iAmM + "end");
  } catch (Exception e) {
      if(p) P("\n" + s + iAmM + "Caught: "+ e.getClass().getName()
          + "\n" + s + makeMargin(iAmM.length()) + "when " + errAt);
      if(p) P(s + makeMargin(iAmM.length()) + "Msg   : " + e.getMessage());
      if(p) P(iAmM + "end - error");
      if (p) e.printStackTrace();
    }
  }

  static public String printInitialContextProperties (int leader, Properties env) {
    int maxLength = 0;
    String s = padRight("", leader, ' ');
    for (Object keyO: env.keySet()) {
      String key = (String) keyO;
      maxLength = 1+Math.max(maxLength, key.length());
    }
    StringBuffer sb = new StringBuffer ();
    sb.append(s + "CONNECTION VARIABLES");
    for (Object keyO: env.keySet()) {
      String key = padRight((String)keyO, maxLength, ' ');
      sb.append("\n" + s + "key: " + key + "value: " + env.get(keyO));
    }
      return sb.toString();
    }

  static public void P (String s) {
    System.out.println(s);  
  }

  static private String getIAm (StackTraceElement[] elements) {
    StringBuffer sb = new StringBuffer ();
    sb.append(getClassMethodName(elements));
    sb.append(" ()");
    return padRight (sb.toString(), 45, ' ') ;
  }

  static public String getClassMethodName (StackTraceElement[] elements) {
    /*
     * This method finds the 1st org.america3 element from the top
     * of the stack
     */
    String className = null;
    for (int i = 0; i < elements.length; i++) {
      className = elements[i].getClassName ();
      if (className.startsWith ("org.america3")) {
        int end = className.lastIndexOf ('.');
        return className.substring (end + 1) + "." + elements[i].getMethodName ();
      } else {
       continue;
      }
    }
  return "no project method found in elements beginning with org.america3" ;
  }

  static public String padRight(String s, int width, char c){
    if (s == null) return "Null String";
    if(s.length() >= width){
      return s;
    } else {
      StringBuffer sb = new StringBuffer();
      sb.append (s);
      for(int i = 0; i < (width - s.length()); i++){
        sb.append(c);
      }
      return sb.toString();
    }
  }

  static public String makeMargin (int padWidth) {
    StringBuffer sb = new StringBuffer ();
    for (int i = 0; i < padWidth; i ++) {
      sb.append(' ');
    }
    return sb.toString();
  }

}

JARS ACCUMULATED SO FAR IN THE BUILD PATH

xnio-api-3.4.6.Final.jar    
xnio-nio-3.4.3.Final.jar

slf4j-log4j12-1.8.0-alpha2.jar    
apache-logging-log4j.jar

artemis-commons-2.2.0.jar    
artemis-core-client-2.2.0.jar    
artemis-jms-client-2.2.0.jar    
artemis-selector-2.2.0.jar

commons-beanutils-1.9.2.jar    
commons-logging-1.1.1.jar    
commons-collections-3.2.1.jar

netty-all-4.1.9.Final.jar

javax.json-1.0.2.jar    
javax.jms-3.1.2.2.jar    
javax.mail-1.5.0.jar    
javax.jms-api-2.0.jar

jboss-client.jar    
jboss-logging-3.1.4.GA.jar    
jboss-ejb-client-1.0.19.final.jar    
jboss-marshalling-1.3.15.GA.jar    
jboss-remoting-4.0.7.Final.jar    
jboss-remote-naming-2.0.1.Final.jar    
jboss-remoting-3.2.7.ga.jar    
jboss-marshalling-river-1.3.14.GA.jar

hornetq-jms-client.jar

ADDED 2017-08-23

WF CLI COMMAND USED TO CONFIGURE SERVER SIDE QUEUE

[standalone@localhost:9990 /] /subsystem=messaging-activemq/server=default/jms-queue=SendToClientQue
ue:add(entries=[java:jboss/exported/jms/queue/sendToClientQueue]}
{"outcome" => "success"}
[standalone@localhost:9990 /]

STANDALONE-FULL.XML JMS MESSAGING SUBSYSTEM

<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
    <server name="default">
        <security enabled="false"/>
        <security-setting name="#">
            <role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
        </security-setting>
        ... 
        <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
        <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
        <jms-queue name="SendToServerQueue" entries="java:jboss/exported/jms/queue/sendToServerQueue"/>
        <jms-queue name="SendToClientQueue" entries="java:jboss/exported/jms/queue/sendToClientQueue }"/>
        <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
        <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
        <pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
    </server>
</subsystem>
George
  • 509
  • 2
  • 9
  • 25
  • It looks like ActiveMQ's implementation of a JMS `Session` (or more accurately an `ActiveMQDestination`) throws the null pointer exception. – Jason Aug 22 '17 at 22:12
  • What is the object stored into `jmsSendToClientQueue` and how did it get into JNDI? It appears to be misconfigured. – user207421 Aug 23 '17 at 00:47
  • jmsSendToClientQueue is a JMS Queue configured in WildFly's standalone-full.xml file using WF's CLI.: I amended the prior post to include the CLI command. And relevant excerpts from the standalone-full.xml messaging subsystem. – George Aug 24 '17 at 01:37

2 Answers2

1

I had a similar problem (but in isDestination(), server was Wildfly 11 Final) and worked around it by replacing all the JMS and EE related dependencies in my POM file with org.wildfly:wildfly-jms-client-bom:11.0.0.Final

idarwin
  • 607
  • 4
  • 19
1

Upgrading Artemis versions to 2.6.2. worked for me. It looks like the old(er) versions had that bug: https://issues.jboss.org/browse/JBEAP-14006

  • This is one of those problems where I ended up changing stuff until it stopped happening without the time to figure out what really caused it. But along that way, I upgraded to 2.6.2 so I guess it could have been the last straw that broke the problem. – George Aug 23 '18 at 19:10