【问题标题】:Why doesn't this code lookup() JMS Connection Factory?为什么这段代码不查找() JMS 连接工厂?
【发布时间】:2018-06-19 15:25:14
【问题描述】:

一个多星期以来,我没能学到足够的 JBoss/Wildfly 10 来编写一个能够获得到 Wildfly 的 JMS 连接的 java 类。谁能指出我正确的方向?

该类在与 Wildfly 相同的计算机上运行的 Eclipse 上执行。 (从技术上讲,这是一个远程连接?)

错误是:

javax.naming.CommunicationException    
Failed to connect to any server. 
Servers tried: [http-remoting://localhost:9990 
//Servers tried: [http-remoting://localhost:8080 depending on Context.PROVIDER_URL
(java.io.IOException: Unknown service name)]

我用这个命令启动 Wildfly:

<wildfly-10.1.0.Final>\bin\standalone.bat --server-config=standalone-full.xml

Wildfly 命令窗口摘录(下)确认使用了standalone-full.xml。

还有一个绑定到 jndi 名称的工厂:“java:jboss/exported/jms/RemoteConnectionFactory”

Wildfly 控制台从

打开
http://localhost:9990

代码中显示的用户 ID 和密码打开它

我在下面粘贴了类代码、ECLIPSE CONSOLE 输出和 WILDFLY CONSOLE 摘录。

我没有想法。希望有人能指出我正确的方向

类代码

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import java.util.HashMap;
import java.util.Properties;
import org.hornetq.jms.client.HornetQJMSConnectionFactory;
import static org.america3.gotest.shared.tools.Utility.padRight;

public class PostedCode implements ConnectionValues {
  private Context ctx = null;
  private HornetQJMSConnectionFactory factory = null;
  private Properties ENV_LOCAL = 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:9990");
    //put(Context.PROVIDER_URL, 
    //    "http://localhost:9990"); This produces identical failures
    //put(Context.PROVIDER_URL, 
    //    "http-remoting://localhost:8080"); This produces identical failures
      put(Context.SECURITY_PRINCIPAL, 
          "jmsUser");  
      put(Context.SECURITY_CREDENTIALS, 
          "jmsPW123!");  
    //put("jboss.naming.client.ejb.context", 
    //    true); This made no difference either  
    }
  };

  public static void main (String args[]) {
    PostedCode test = new PostedCode ();
    // Set Wildfly Initial Context
    test.ctx = test.getInitialContetext(); 
    if (test.ctx == null) {return;} else {System.out.println("\nInitialContext not null: " + test.ctx);}
    // Print list of IntialContxt
    test.listCtx (test.ctx);
    // Set JMS ConnectionFacytory
    test.factory = (HornetQJMSConnectionFactory)test.getFactory(test.ctx);
    if (test.factory == null) {return;} else {System.out.println("ConnectionFactory not null: " + test.factory);}
    }

  private Context getInitialContetext () {
    pProps(ENV_LOCAL); // Prints properties to console
    try {
      return new InitialContext(ENV_LOCAL); 
    } catch (Exception e) {
      System.out.println("\ngetInitialContext() caught: " + e.getClass().getName());
      System.out.println("                    msg   :    " + e.getMessage());
      return null;
    }
  }

  private HornetQJMSConnectionFactory getFactory (Context ctx) {
    try {
      return (HornetQJMSConnectionFactory) ctx.lookup("java:jboss/exported/jms/RemoteConnectionFactory");
    } catch (Exception e) {
      System.out.println("\ngetFactory() caught: " + e.getClass().getName());
      System.out.println("             msg   :    " + e.getMessage());
      e.printStackTrace();
      return null;
    }
  }

  static private void pProps (Properties p) {
    StringBuffer sb = new StringBuffer ();
    sb.append("Properties used to obtain IntialContext"+"\n");
    for (Object key: p.keySet()) {
      String pKey = padRight ("  " + (String)key,40,' ');
      sb.append(pKey);
      sb.append("\"" + p.get(key) + "\"" + "\n");
    }
    System.out.println(sb);
  }

  private void listCtx (Context ctx) {
    HashMap<String, String> map = new HashMap<String, String>();
    try {
      System.out.println("Listing InitialContext NameClassPair Enumeration:");
      NamingEnumeration<NameClassPair> list = this.ctx.list("");
      System.out.println("Enumerator: " + list);
      while (list.hasMoreElements()) {
        NameClassPair next = list.next();
        String name = next.getName();
        String jndiPath = "name: " + name;
        map.put(name, jndiPath);
      }
      System.out.println("map.size(): " +map.size());
    } catch (Exception e) {
      System.out.println("\nlistCtx(): caught: " + e.getClass().getName());
      System.out.println("           msg   :    " + e.getMessage());
    }
  }
}

ECLIPSE 控制台输出:

Properties used to obtain IntialContext
  java.naming.provider.url              "http-remoting://localhost:9990"
  java.naming.factory.initial           "org.jboss.naming.remote.client.InitialContextFactory"
  java.naming.security.principal        "jmsUser"
  java.naming.security.credentials      "jmsPW123!"

Aug 08, 2017 11:03:03 AM org.xnio.Xnio <clinit>
INFO: XNIO version 3.4.6.Final
Aug 08, 2017 11:03:03 AM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.4.3.Final
Aug 08, 2017 11:03:03 AM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.7.Final

InitialContext not null: javax.naming.InitialContext@6108b2d7

Listing InitialContext NameClassPair Enumeration:
listCtx(): caught: javax.naming.CommunicationException
           msg   :    Failed to connect to any server. Servers tried: [http-remoting://localhost:9990 (java.io.IOException: Unknown service name)]
           //msg   :    Failed to connect to any server. Servers tried: [http-remoting://localhost:8080 (java.io.IOException: Unknown service name)] depending on Context.PROVIDER_URL

getFactory() caught: javax.naming.CommunicationException
             msg   :    Failed to connect to any server. Servers tried: [http-remoting://localhost:9990 (java.io.IOException: Unknown service name)]
           //msg   :    Failed to connect to any server. Servers tried: [http-remoting://localhost:8080 (java.io.IOException: Unknown service name)] depending on Context.PROVIDER_URL

javax.naming.CommunicationException: Failed to connect to any server. Servers tried: [http-remoting://localhost:9990 (java.io.IOException: Unknown service name)]
    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(Unknown Source)
    at org.america3.gotest.xtra.PostedCode.getFactory(PostedCode.java:55)
    at org.america3.gotest.xtra.PostedCode.main(PostedCode.java:38)

WILDFLY 控制台除外:

Calling "C:\ProgramFilesGeo\Wildfly\wildfly-10.1.0.Final\bin\standalone.conf.bat"
Setting JAVA property to "C:\Program Files\Java\jdk1.8.0_121\bin\java"
====================================================
  JBoss Bootstrap Environment
…
JAVA_OPTS: "-Dprogram.name=standalone.bat -Xms64M -Xmx512M -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman"
=====================================================
…
10:48:03,475 INFO  [org.wildfly.extension.messaging-activemq] 
(ServerService Thread Pool -- 73) WFLYMSGAMQ0002: 
Bound messaging object to jndi name java:jboss/exported/jms/RemoteConnectionFactory
…
10:48:04,111 INFO  [org.jboss.as] 
(Controller Boot Thread) WFLYSRV0051: 
Admin console listening on http://127.0.0.1:9990    …
10:48:04,113 INFO  [org.jboss.as] 
(Controller Boot Thread) WFLYSRV0025: 
WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 3714ms - Started 486 of 741 services (428 services are lazy, passive or on-demand)

【问题讨论】:

  • 我不认为远程端口与控制台端口相同。
  • 谢谢 Nicholas,但我也厌倦了 8080。同样的失败。 (我在上面编辑了原文,让大家知道已经尝试过了。)想不出任何其他端口。

标签: java jboss jms wildfly


【解决方案1】:

请检查 ping localhost 是否解析到正确的环回地址。我在 ipv4 盒子上也遇到了类似的问题,其中 ping 正在转为 ipv6 loopback[::1]

在以下属性中尝试 127.0.0.1 而不是 localhost

java.naming.provider.url "http-remoting://localhost:9990"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-28
    • 2017-03-13
    • 2017-03-31
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多