JNDI初始背景& EJB 3的Lookup_problem

时间:2015-01-08 10:09:53

标签: java java-ee jboss ejb ejb-3.0

这个问题很常见。我已经阅读了一些文章,但无法找到问题。我想在EJB 3.0中创建一个简单的HelloWorld程序,eclipse luna,jboss 7.1.1 Final。

这是我的豆子:

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();
}

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

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

我已经将属性文件放在了类路径中。但是当我尝试运行它时会发生这种情况:

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概念的新手。请帮我找出问题所在。在此先感谢。enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

final String moduleName = "HeloWorldEJB";

然后它应该工作。

答案 1 :(得分:0)

  1. 当您要做一个简单的示例时,请使其保持简单[KISS],您的大多数代码都是正确的,客户是我,我简化了客户代码,在客户中添加了我的注释,希望它对自己有所帮助。 ....在EJB上添加@ javax.ejb.Stateless(name =“ HelloWorldEJB”)//这是可移植的未锁定任何供应商。我相信以weblogic和glassfish为中心。
  2. 确保您的类路径中有 client .jar。这是您仅需添加用intelija或eclipse或任何其他ide创建EJB项目后的jar。
  3. 一旦成功部署EJB,我就将其部署在glassfish容器中了,运行客户端。
  4. 除了下面的代码,您不需要任何其他配置。该代码是可移植的,并且可以在大多数容器上运行,但是Surprises很有趣,请按原样运行,并让我知道.....
  5. 一旦满足以下条件,就可以根据项目要求添加其他jndi属性,然后继续.....

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

  1. 遥控器

    package 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....