无法在 JBoss 的上下文中从客户端应用程序获取远程 ejb

Posted

技术标签:

【中文标题】无法在 JBoss 的上下文中从客户端应用程序获取远程 ejb【英文标题】:Unable to get remote ejb from client application in the context of JBoss 【发布时间】:2016-01-19 23:13:32 【问题描述】:

客户端应用

package com.jeet.client;

import java.util.Properties;


import javax.naming.Context;
import javax.naming.InitialContext; 
import javax.naming.NamingException;
import com.jeet.interfaces.HelloWorldRemoteBean;
import com.jeet.org.HelloWorldBean;

public class EJBApplicationClient 

    public static void main(String[] args) 
        HelloWorldRemoteBean bean = doLookup();
        System.out.println(bean.sayHello()); // 4. Call business logic
    

    private static HelloWorldRemoteBean doLookup() 


        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        props.put(Context.PROVIDER_URL, "jnp://127.0.0.1:8080");


        HelloWorldRemoteBean bean = null;
        try
        InitialContext ctx = new InitialContext(props);

        System.out.println("connecting.."); 
            String lookupName = getLookupName();

            bean = (HelloWorldRemoteBean) ctx.lookup(lookupName);
            System.out.println("done"); 
         catch (NamingException e) 
            e.printStackTrace();
        
        return bean;
    

    private static String getLookupName() 
/* 
The app name is the EAR name of the deployed EJB without .ear suffix. 
Since we haven't deployed the application as a .ear, 
the app name for us will be an empty string
*/
        String appName = "";

        /* The module name is the JAR name of the deployed EJB 
        without the .jar suffix.
        */
        String moduleName = "HelloWorldSessionBean";

/*AS7 allows each deployment to have an (optional) distinct name. 
This can be an empty string if distinct name is not specified.
*/
        String distinctName = "";

        // The EJB bean implementation class name
        String beanName = HelloWorldBean.class.getSimpleName();

        // Fully qualified remote interface name
        final String interfaceName = HelloWorldRemoteBean.class.getName();

        // Create a look up string name
        String name = "ejb:" + appName + "/" + moduleName + "/" + 
            distinctName    + "/" + beanName + "!" + interfaceName;

        return name;
    

输出:正在连接..

我没有得到任何输出... JBOSS 服务器正在运行并且 EJB 已经部署.. Jbossall-client jar 被导入.. 我是 EJB 新手……需要帮助吗? 配置也没有问题...

野蝇日志

04:20:56,353 INFO  [org.jboss.modules] (main) JBoss Modules version 1.3.3.Final
04:20:56,648 INFO  [org.jboss.msc] (main) JBoss MSC version 1.2.2.Final
04:20:56,761 INFO  [org.jboss.as] (MSC service thread 1-2) JBAS015899: WildFly 8.2.1.Final "Tweek" starting
04:20:58,794 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found HelloWorldSessionBean.jar in deployment directory. To trigger deployment create a file called HelloWorldSessionBean.jar.dodeploy
04:20:58,795 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found EjbComponent.jar in deployment directory. To trigger deployment create a file called EjbComponent.jar.dodeploy
04:20:58,828 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS015888: Creating http management service using socket-binding (management-http)
04:20:59,041 INFO  [org.xnio] (MSC service thread 1-1) XNIO version 3.3.0.Final
04:20:59,051 INFO  [org.xnio.nio] (MSC service thread 1-1) XNIO NIO Implementation Version 3.3.0.Final
04:20:59,244 INFO  [org.jboss.remoting] (MSC service thread 1-1) JBoss Remoting version 4.0.7.Final
04:20:59,319 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 32) JBAS010280: Activating Infinispan subsystem.
04:20:59,552 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
04:20:59,604 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 47) JBAS017502: Undertow 1.1.8.Final starting
04:20:59,617 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 45) JBAS013171: Activating Security Subsystem
04:20:59,703 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 31) WFLYIO001: Worker 'default' has auto-configured to 4 core threads with 32 task threads based on your 2 available processors
04:20:59,740 INFO  [org.jboss.as.connector.logging] (MSC service thread 1-2) JBAS010408: Starting JCA Subsystem (IronJacamar 1.1.9.Final)
04:20:59,745 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017502: Undertow 1.1.8.Final starting
04:20:59,746 INFO  [org.jboss.as.security] (MSC service thread 1-2) JBAS013170: Current PicketBox version=4.0.21.Final
04:20:59,761 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) JBAS010417: Started Driver service with driver-name = h2
04:20:59,790 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 47) JBAS017527: Creating file handler for path C:\Jboss8.2.1\wildfly-8.2.1.Final/welcome-content
04:20:59,851 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) JBAS017525: Started server default-server.
04:20:59,854 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) JBAS017531: Host default-host starting
04:20:59,861 INFO  [org.jboss.as.jsf] (ServerService Thread Pool -- 38) JBAS012615: Activated the following JSF Implementations: [main]
04:20:59,900 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 40) JBAS011800: Activating Naming Subsystem
04:20:59,946 WARN  [org.jboss.as.txn] (ServerService Thread Pool -- 46) JBAS010153: Node identifier property is set to the default value. Please make sure it is unique.
04:21:00,030 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
04:21:00,462 INFO  [org.jboss.as.naming] (MSC service thread 1-2) JBAS011802: Starting Naming Service
04:21:00,471 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-2) JBAS015400: Bound mail session [java:jboss/mail/Default]
04:21:00,502 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) JBAS015012: Started FileSystemDeploymentService for directory C:\Jboss8.2.1\wildfly-8.2.1.Final\standalone\deployments
04:21:00,502 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "EjbComponent.jar" (runtime-name: "EjbComponent.jar")
04:21:00,506 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "HelloWorldSessionBean.jar" (runtime-name: "HelloWorldSessionBean.jar")
04:21:00,856 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016002: Processing weld deployment EjbComponent.jar
04:21:00,901 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017519: Undertow HTTP listener default listening on localhost/127.0.0.1:8080
04:21:00,909 INFO  [org.jboss.weld.deployer] (MSC service thread 1-4) JBAS016002: Processing weld deployment HelloWorldSessionBean.jar
04:21:01,301 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-4) HV000001: Hibernate Validator 5.1.3.Final
04:21:01,774 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-4) JNDI bindings for session bean named HelloWorldBean in deployment unit deployment "HelloWorldSessionBean.jar" are as follows:

    java:global/HelloWorldSessionBean/HelloWorldBean!com.jeet.org.HelloWorldBean
    java:app/HelloWorldSessionBean/HelloWorldBean!com.jeet.org.HelloWorldBean
    java:module/HelloWorldBean!com.jeet.org.HelloWorldBean
    java:global/HelloWorldSessionBean/HelloWorldBean!com.jeet.interfaces.HelloWorldRemoteBean
    java:app/HelloWorldSessionBean/HelloWorldBean!com.jeet.interfaces.HelloWorldRemoteBean
    java:module/HelloWorldBean!com.jeet.interfaces.HelloWorldRemoteBean
    java:jboss/exported/HelloWorldSessionBean/HelloWorldBean!com.jeet.interfaces.HelloWorldRemoteBean

04:21:01,850 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-3) JNDI bindings for session bean named LibrarySessionBean in deployment unit deployment "EjbComponent.jar" are as follows:

    java:global/EjbComponent/LibrarySessionBean!com.jeet.org.LibrarySessionBeanRemote
    java:app/EjbComponent/LibrarySessionBean!com.jeet.org.LibrarySessionBeanRemote
    java:module/LibrarySessionBean!com.jeet.org.LibrarySessionBeanRemote
    java:jboss/exported/EjbComponent/LibrarySessionBean!com.jeet.org.LibrarySessionBeanRemote
    java:global/EjbComponent/LibrarySessionBean
    java:app/EjbComponent/LibrarySessionBean
    java:module/LibrarySessionBean

04:21:01,907 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
04:21:02,100 INFO  [org.jboss.ws.common.management] (MSC service thread 1-2) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.3.2.Final
04:21:02,217 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016005: Starting Services for CDI deployment: EjbComponent.jar
04:21:02,255 INFO  [org.jboss.weld.Version] (MSC service thread 1-2) WELD-000900: 2.2.6 (Final)
04:21:02,326 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016008: Starting weld service for deployment EjbComponent.jar
04:21:02,397 INFO  [org.jboss.weld.deployer] (MSC service thread 1-4) JBAS016005: Starting Services for CDI deployment: HelloWorldSessionBean.jar
04:21:02,474 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016008: Starting weld service for deployment HelloWorldSessionBean.jar
04:21:03,859 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "HelloWorldSessionBean.jar" (runtime-name : "HelloWorldSessionBean.jar")
04:21:03,859 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "EjbComponent.jar" (runtime-name : "EjbComponent.jar")
04:21:04,017 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
04:21:04,019 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
04:21:04,019 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.1.Final "Tweek" started in 8101ms - Started 330 of 390 services (98 services are lazy, passive or on-demand)

【问题讨论】:

您使用的是什么版本的 JBoss?无论如何,jnp: URL 中的端口 8080 看起来都是错误的。 我正在使用 Jboss wildfly..Jboss 在端口 8080 上运行.. 试试props.put(Context.PROVIDER_URL,"http-remoting://127.0.0.1:8080"); 而不是props.put(Context.PROVIDER_URL, "jnp://127.0.0.1:8080"); 试过..但它抛出连接异常.. 向我们展示异常:) 【参考方案1】:

对于远程 EJB,您需要:

    ejb-client.jar 添加到您的客户端应用程序而不是 ejb.jarejb-client.jar 包含所有接口——远程和本地

    使用如下属性配置InitialContext

    java.naming.factory.initial = org.jnp.interfaces.NamingContextFactory
    java.naming.provider.url    = jnp://host:port/[jndi_path] ( in your case jnp://localhost:1099/ 1099 is the port default but you can modify it in jboss config)
    java.naming.factory.url.pkgs = org.jboss.naming:org.jnp.interfaces
    jboss.naming.client.ejb.context = true
    

    可选:

    java.naming.security.principal=user
    java.naming.security.credentials=password
    

我建议您使用文件属性来获取所有 JNDI 远程和本地以及另一个文件以进行查找。示例:您需要与您的应用程序共享一个配置文件夹,您创建 4 个文件:

jndi_local.properties jndi_remote.properties lookup_local.properties lookup_remote.properties

此类文件的示例:

jndi_local.properties

java.naming.factory.url.pkgs = org.jboss.naming:org.jnp.interfaces

jndi_Remote.properties

java.naming.factory.initial = org.jnp.interfaces.NamingContextFactory
java.naming.provider.url    = jnp://localhost:1099/
java.naming.factory.url.pkgs = org.jboss.naming:org.jnp.interfaces
jboss.naming.client.ejb.context = true

#optional:
#java.naming.security.principal=user
#java.naming.security.credentials=password

lookup_local.properties

interfaceFacadeLocal(with notation @local) extend interfaceFacade where interfaceFacade with not notation local or remote)
interfaceFacade=java:global/aplicationName/nameejbjar/FacadeBean!org.client.me.ejb.facade.local.interfaceFacadeLocal

lookup_remote.properties

interfaceServiceFacade=ejb/aplicationName/ejb-client.jar/ServiceBean#org.server.me.ejb.ServiceRemoteBean

有关为客户端 EJB 配置上下文的更多信息,请参阅here。

【讨论】:

以上是关于无法在 JBoss 的上下文中从客户端应用程序获取远程 ejb的主要内容,如果未能解决你的问题,请参考以下文章

我无法在 JBOSS EAP 7.1 中从 EAR 或 WAR 设置系统属性

我可以在 spring 应用程序上下文中从 jpa 获取 jdbc 数据源吗?

如何在 jBoss 中启用 CORS

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

当jboss 7服务器启动时,在jndi查找中获取appname

以编程方式确定JBoss 5.1中的JNDI根上下文/ ear名称