简体   繁体   English

我无法设置 jndi.properties 来访问 Jboss 5 上的远程 EJB

[英]I can't setup my jndi.properties to access remote EJBs on Jboss 5

I'm trying to setup the Jboss server "client" (version 5.1.0) to use remote EJBs from another Jboss server (10.90.0.91), but I can't do this using a jndi.properties file on the Jboss client.我正在尝试设置 Jboss 服务器“客户端”(版本 5.1.0)以使用来自另一台 Jboss 服务器(10.90.0.91)的远程 EJB,但我无法使用 Jboss 客户端上的 jndi.properties 文件执行此操作。

I can get the remote EJB using this simple code on my client:我可以在我的客户端上使用这个简单的代码来获取远程 EJB:

        InitialContext ctx = null;
        try {
            Hashtable<String, String> jndiProps = new Hashtable<String, String>();
            jndiProps.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            jndiProps.put(InitialContext.PROVIDER_URL, "jnp://10.90.0.91:1099");
            ctx = new InitialContext(jndiProps);
            return ctx.lookup(jndiName);
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }

This works fine.这工作正常。

Now I would like to setup the Jboss client with this properties.现在我想用这个属性设置 Jboss 客户端。 But if I edit the existent jndi.properties file localized on server/{application}/conf/ from:但是,如果我从以下位置编辑本地化在server/{application}/conf/上的现有 jndi.properties 文件:

# DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
java.naming.factory.initial=org.jboss.iiop.naming.ORBInitialContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

To:至:

# DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://10.90.0.91:1099

I receive some errors when I start the Jboss client (apparently, I don't know what I'm doing :)):我在启动 Jboss 客户端时收到一些错误(显然,我不知道我在做什么 :)):

2016-08-19 10:17:41,645 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Start: name=HASessionStateService state=Create
javax.naming.NameAlreadyBoundException: Default
    at org.jnp.server.NamingServer.bind(NamingServer.java:209)
    at org.jnp.server.NamingServer.bind(NamingServer.java:167)
[...]

2016-08-19 10:17:42,767 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Start: name=ProfileServiceProxyFactory state=Create
javax.naming.NameAlreadyBoundException: ProfileService
    at org.jnp.server.NamingServer.bind(NamingServer.java:209)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[...]

2016-08-19 10:17:44,778 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Start: name=jboss:service=ClientUserTransaction state=Create mode=Manual requiredState=Installed
javax.naming.NameAlreadyBoundException: UserTransaction
    at org.jnp.server.NamingServer.bind(NamingServer.java:209)
    at sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
[...]

And in the final:在决赛中:

2016-08-19 10:17:51,993 ERROR [org.jboss.system.server.profileservice.ProfileServiceBootstrap] (main) Failed to load profile: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):

DEPLOYMENTS MISSING DEPENDENCIES:
  Deployment "ProfileServiceInvocationHandler" is missing the following dependencies:
    Dependency "ProfileServiceProxyFactory" (should be in state "Configured", but is actually in state "**ERROR**")
    Dependency "ProfileServiceProxyFactory" (should be in state "Configured", but is actually in state "**ERROR**")

DEPLOYMENTS IN ERROR:
  Deployment "jboss:service=ClientUserTransaction" is in error due to the following reason(s): javax.naming.NameAlreadyBoundException: UserTransaction
  Deployment "HASessionStateService" is in error due to the following reason(s): javax.naming.NameAlreadyBoundException: Default
  Deployment "ProfileServiceProxyFactory" is in error due to the following reason(s): javax.naming.NameAlreadyBoundException: ProfileService, **ERROR**

So, I think I can't touch in already existent JNDI properties on that file.所以,我想我无法触及该文件上已经存在的 JNDI 属性。

If the jndi.properties file can't be changed because it is being used by JBoss itself, in which location can I set my JNDI lookup settings to the remote EJBs within the Jboss 5?如果 jndi.properties 文件由于 JBoss 本身正在使用而无法更改,那么我可以在哪个位置将 JNDI 查找设置设置为 Jboss 5 中的远程 EJB? How can I configure a jndi.properties file to be available in the application classpath without put the jndi.properties file inside of my WAR file?如何将 jndi.properties 文件配置为在应用程序类路径中可用而不将 jndi.properties 文件放在我的 WAR 文件中?

Thanks!谢谢!

An alternative way to do this is to configure a org.jboss.naming.ExternalContext MBean in your jboss-service.xml file:另一种方法是在 jboss-service.xml 文件中配置一个org.jboss.naming.ExternalContext MBean:

<mbean code="org.jboss.naming.ExternalContext" 
       name="jboss.jndi:service=ExternalContext,jndiName=external/server2">
    <attribute name="JndiName">external/server2</attribute>
    <attribute name="Properties">
        java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
        java.naming.provider.url=jnp://10.90.0.91:1099
        <!-- other properties as needed -->
    </attribute>
    <attribute name="InitialContext"> javax.naming.IntialContext </attribute>
    <attribute name="RemoteAccess">false</attribute>
</mbean>

Your java code to perform the lookup then becomes:您执行查找的 Java 代码将变为:

 Context initialContext = new InitialContext();
 return initialContext.lookup("external/server2/" + jndiName);

You can even navigate the remote JNDI tree using JNDIView in the local management console when you set this up.您甚至可以在设置时使用本地管理控制台中的 JNDIView 导航远程 JNDI 树。

More information can be found in org.jboss.naming.ExternalContext MBean .更多信息可以在org.jboss.naming.ExternalContext MBean 中找到。

Well, I found another solution.好吧,我找到了另一个解决方案。

I created a new file called jndi-remote.properties on the configuration directory from Jboss:我在 Jboss 的配置目录上创建了一个名为jndi-remote.properties的新文件:

{jboss_home}/server/default/conf/jndi-remote.properties

And I access the file in Jboss config directory ( System.getProperty("jboss.server.config.url") ) from Java:我从 Java 访问 Jboss 配置目录( System.getProperty("jboss.server.config.url") )中的文件:

String fileName = System.getProperty("jboss.server.config.url") + "/" + "jndi-remote.properties";

Properties properties = null;
try {
    URL url = new URL(fileName);
    if(new File(url.toURI()).exists()) { 
        properties = new Properties();
        properties.load(url.openStream());
        LOGGER.info("The file " + "jndi-remote.properties" + " was loaded from " + fileName);
    }
} catch (MalformedURLException e) {
    //throw
} catch (URISyntaxException e) {
    //throw
} catch (IOException e) {
    //throw
} 

And initialize my InitialContext:并初始化我的 InitialContext:

if (properties != null) {
    ctx = new InitialContext(properties);
}

Works :).作品:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM