为什么简单的JMS客户端抛出不可能的NullPonterExcepton - 来自Session createConsumer()

时间:2017-08-22 21:26:04

标签: java jms wildfly-10

任何人都可以告诉我为什么我的JMS Sessoin的createConsumer()抛出一个NullPointerException文档说它没有'扔?

我已经挣扎了几个星期才得到一个简单的Java类,它可以建立一个JMS连接并与WildFly交换消息......并且惨遭失败。

它的Eclipse Neon和Wildfly 10.(曾经和WF-8一起工作的工人阶级默认为Hornert。现在我猜WF-10对ArtemisMQ的反对。)

请在下面找到:

ECLIPSE CONSOLE OUTPUT with STACK TRACE

STANDALONE JAVE客户代码

JARS在建筑路径上累积 (我怀疑所有这些都是必需的,但是已经被认为是关于WF-10的这个和早期问题的徘徊)

感谢您的帮助。

注意:代码对于打印行以确定控制台输出的格式非常疯狂,这种方式毫无疑问会引发错误。底部粘贴的许多不相关的方法只是我通常从我的实用程序类导入的帮助程序。但我粘贴它们,因为目标是留下一个远程Java客户端消息WF-10的直接工作示例,所以我必须粘贴帮助程序,因此这段代码将是独立的。即未来新手可以剪切和粘贴的东西,并有一个工作的起点。

但是,从main()中删除所有打印代码会使这些7"简单"线。)

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);

帮助!

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客户代码

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在建筑路径上累积起来

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

已添加2017-08-23

用于配置服务器端队列的WF CLI命令

[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>

2 个答案:

答案 0 :(得分:1)

我有类似的问题(但在isDestination()中,服务器是Wildfly 11 Final)并通过用org.wildfly替换我的POM文件中的所有JMS和EE相关依赖项来解决它:wildfly-jms-client-bom :11.0.0.Final

答案 1 :(得分:1)

将Artemis版本升级到2.6.2。为我工作。看来旧版本有该错误:https://issues.jboss.org/browse/JBEAP-14006