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

问题描述 投票:0回答:2

谁能告诉我为什么我的 JMS Sessoin 的 createConsumer () 会抛出 NullPointerException 文档说它不会抛出?

我几周来一直在努力获得一个简单的 Java 类,该类可以建立 JMS 连接并与 WildFly 交换消息......但惨败。

它的 Eclipse Neon 和 Wildfly 10。(我曾经使用 WF-8 时的工作级别默认为 Hornert。现在我猜 WF-10 默认为 ArtemisMQ。)

请参阅以下内容:

带有堆栈跟踪的 ECLIPSE 控制台输出

独立 JAVE 客户端代码

迄今为止在构建路径中积累的 JAR (我怀疑所有这些都是必要的,但已在有关此问题以及 WF-10 早期问题的帖子中被挑选出来)

感谢您的帮助。

注意: 该代码具有疯狂的打印行来格式化控制台输出,从而毫无疑问地抛出错误。粘贴在底部的许多不相关的方法只是我通常从实用程序类中导入的帮助程序。但我将它们粘贴了进去,因为目标是留下一个远程 Java 客户端消息传递 WF-10 的直接工作示例,所以我必须粘贴帮助程序,以便此代码将是独立的。 IE。未来的新手只需剪切和粘贴即可拥有一个工作起点。

但是去掉 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 控制台输出

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)

独立的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();
  }

}

迄今为止在构建路径中积累的 JAR

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 /]

独立完整.XML JMS 消息传递子系统

<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>
java jms activemq-artemis wildfly-10
2个回答
1
投票

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


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.