EJB 3 的 JNDI 初始上下文和 Lookup_problem

JNDI Initial Context & Lookup_problem for EJB 3

这个问题很常见。我已经阅读了一些文章,但找不到问题所在。我想在 EJB 3.0、eclipse luna、jboss 7.1.1 Final.

中创建一个简单的 HelloWorld 程序

这是我的豆子:

package com.tcs.HelloWorldPack;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class HelloWorld
 */
@Stateless(mappedName="HelloWorldBean")
public class HelloWorld implements HelloWorldRemote {

    /**
     * Default constructor. 
     */
    public HelloWorld() {
        // TODO Auto-generated constructor stub

    }

    @Override
    public void displayMsg() {
        // TODO Auto-generated method stub 
        System.out.println("Hello World!!");

    }

}

这是我的远程界面:

package com.tcs.HelloWorldPack;

import javax.ejb.Local;
//import javax.ejb.Remote;
import javax.ejb.Remote;

@Remote
public interface HelloWorldRemote {

    void displayMsg();
}

这是我的客户端 运行ning 在同一台机器上:

package com.tcs.HelloWorldClient;

import java.util.Hashtable;
import java.util.Properties;

import com.tcs.HelloWorldPack.*;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class HelloWorldClient {


    public static void main(String[] args) throws NamingException {


        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        jndiProperties.put(javax.naming.Context.SECURITY_PRINCIPAL, "myUser");
        jndiProperties.put(javax.naming.Context.SECURITY_CREDENTIALS, "myPass");
//        jndiProperties.put(javax.naming.Context.PROVIDER_URL, "jnp://localhost:1099");
//        jndiProperties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        jndiProperties.put("jboss.naming.client.ejb.context", true);

        final Context context = new InitialContext(jndiProperties);

        final String appName= "HeloWorldEJBEAR";
        final String moduleName= "";
        final String distinctName ="";
        final String beanName = "HeloWorld";
        final String viewClassName = "com.tcs.HelloWorldPack.HelloWorldRemote";
        HelloWorldRemote hello = (HelloWorldRemote) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

        hello.displayMsg();


    }

}

这是我的 jboss-ejb-client.properties 文件:

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

remote.connection.default.username=myUser
remote.connection.default.password=myPass

我已将属性文件放在类路径中 also.But 当我尝试 运行 它时发生了这种情况:

Exception in thread "main" java.lang.IllegalStateException: No EJB receiver available for handling [appName:HeloWorldEJBEAR,modulename:,distinctname:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@413ded77
    at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
    at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)
    at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)
    at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)
    at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
    at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
    at com.sun.proxy.$Proxy0.displayMsg(Unknown Source)
    at com.tcs.HelloWorldClient.HelloWorldClient.main(HelloWorldClient.java:71)
Jan 08, 2015 3:34:44 PM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleEnd
INFO: Channel Channel ID de8d2aa6 (outbound) of Remoting connection 44477156 to localhost/127.0.0.1:4447 can no longer process messages

我也上传了我的目录结构。我是 EJB 概念的新手。请帮我找出问题出在哪里。提前致谢。

你的模块名是一个空字符串,但是JNDI名称中的模块名不能是空字符串。 看here

您必须将您的 ejb 模块的名称设置为 .jar,不带 .jar 后缀。

final String moduleName = "HeloWorldEJB";

那么应该可以了。

  1. 由于您打算做一个简单的示例,因此请保持简单 [KISS],您的大部分代码都是正确的,关键在于客户端,我已经简化了客户端代码,在客户端中添加了我的评论希望它自己 explanatory.And... Added on EJB @javax.ejb.Stateless(name = "HelloWorldEJB") //This is portable No Vendor locked in. Alternative is mappedName [removed from your code @Stateless(mappedName="HelloWorldBean" ) p]我认为它更以 weblogic 和 glassfish 为中心。
  2. 确保您的 client.jar 在您的 class 路径中。这是您在 intelija 或 eclipse 或任何其他 ide.
  3. 中创建 EJB 项目后必须添加的唯一 jar
  4. 一旦部署成功,我就在 glassfish 容器中部署了 EJB,运行 客户端。
  5. 除了下面的代码,您不需要任何其他配置。这段代码是可移植的,应该适用于大多数容器,但是惊喜很有趣 运行 照原样做,让我知道.....
  6. 一旦以下内容对您有效,您就可以根据您的项目要求添加其他 jndi 属性并继续......

================================

  1. 遥控器

    包 com.au.ejbs;
    导入 javax.ejb.Remote;

    @Remote
    public interface HelloWorldI {
       String displayMessage(String message);
    }
    
    
    
    
    =================================
    2. The Impl
    
    package com.au.ejbs;
    
    import javax.ejb.Remote;
    
    @javax.ejb.Stateless(name = "HelloWorldEJB")
    public class HelloWorld implements HelloWorldI {
        @Override
        public String displayMessage(String message) {
            // TODO Auto-generated method stub
            return "Returning from Remote" + message;
        }
    }
    
    
    
    ======================================
    3. The client
    
    package com.au.clients;
    
    import com.au.ejbs.HelloWorldI;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class HelloWorldT {
    
        public static void main(String[] args) throws NamingException {
            Context context = new InitialContext();
            HelloWorldI helloWorldI = (HelloWorldI)context.lookup("java:global/ejb3_2_ear_exploded/ejb/HelloWorldEJB");
            //portable syntax java:global/[ ear name]/[module name normally the jar name in my case ejb.jar within the ear, ejb3_2_ear_exploded]/name in ....javax.ejb.Stateless(name = "HelloWorldEJB")/
            System.out.println( "output " + helloWorldI.displayMessage("From Client with luv...."));
        }
    }
    
    ===================
    4. output
    output Returning from RemoteFrom Client with luv....