为什么这个代码不能查找()JMS连接工厂?

时间:2017-08-08 23:01:03

标签: java jboss jms wildfly

我已经失败了一个多星期才学会了足够的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和密码将其打开

我已经粘贴了下面的CLASS CODE,ECLIPSE CONSOLE OUTPUT和WILDFLY CONSOLE EXCERPTS。

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

CLASS CODE

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 CONSOLE OUTPUT:

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 CONSOLE EXCEPTS:

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)

1 个答案:

答案 0 :(得分:0)

请检查ping localhost是否正在解析以纠正环回地址。我也遇到过ipv4 box上类似的问题,ping正在解决ipv6 loopback [:: 1]

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

java.naming.provider.url&#34; http-remoting:// localhost:9990&#34;