如何使用独立程序访问EJB 2.0

时间:2014-07-31 05:20:57

标签: java java-ee jboss ejb-3.0 ejb-2.x

我正在使用Eclipse和Jboss AS开发EJB 2.0项目。以下是代码示例:

组件接口:

package com.example.ejb;

import javax.ejb.*;
import java.rmi.RemoteException;

public interface Advice extends EJBObject {

    public String getAdvice() throws RemoteException;
}

家庭界面:

package com.example.ejb;

import java.rmi.RemoteException;

import javax.ejb.*;

public interface AdviceHome extends EJBHome {

    public Advice create() throws CreateException, RemoteException;
}

Bean实现:

package com.example.ejb;

import java.rmi.RemoteException;

import javax.ejb.*;

public class AdviceBean implements SessionBean {


private static final long serialVersionUID = 1L;

private String[] adviceStrings = {"One word: inappropriate.", 
           "You mightwant to    rethink that haricut.", "Your boss will respect "};

    public void ejbPassivate() {

            System.out.println("ejb activate");
    }

    public void ejbRemove() {

            System.out.println("ejb remove");

    }

    public void setSessionContext(SessionContext ctx) {

            System.out.println("session context");

    }

    public String getAdvice() {

            System.out.println("in get advice");

            int random = (int) (Math.random() * adviceStrings.length);
            return adviceStrings[random];
    }

    public void ejbCreate() {

            System.out.println("in get create");
    }

    public void ejbActivate() throws EJBException, RemoteException {
        System.out.println("in ejbActivate");

    }
}

ejb-jar.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"       
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar id="ejb-jar_ID">
<display-name>Ejb1</display-name>

<enterprise-beans>  
<session>
   <display-name>AdviceBean</display-name>
   <ejb-name>AdviceBean</ejb-name>
   <home>com.example.ejb.AdviceHome </home>
   <remote>com.example.ejb.Advice</remote>
   <ejb-class>com.example.ejb.AdviceBean</ejb-class>
   <session-type>Stateless</session-type>
   <transaction-type>Bean</transaction-type>
   <security-identity>
     <description></description>
     <use-caller-identity></use-caller-identity>
   </security-identity>
 </session>  
</enterprise-beans>
<ejb-client-jar>EJBProjectClient.jar</ejb-client-jar>
</ejb-jar>

我在Jboss AS中部署了这个代码,当我在“服务器”中运行时,我在JBOSS中获得的以下详细信息为JNDI启动了日志:

09:33:50,273 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor](MSC服务线程1-3)部署单元部署“EJBProject1.jar”中名为AdviceBean的会话bean的JNDI绑定为如下:

java:global/EJBProject1/AdviceBean!com.example.ejb.Advice
java:app/EJBProject1/AdviceBean!com.example.ejb.Advice
java:module/AdviceBean!com.example.ejb.Advice
java:jboss/exported/EJBProject1/AdviceBean!com.example.ejb.Advice
java:global/EJBProject1/AdviceBean!com.example.ejb.AdviceHome
java:app/EJBProject1/AdviceBean!com.example.ejb.AdviceHome
java:module/AdviceBean!com.example.ejb.AdviceHome
java:jboss/exported/EJBProject1/AdviceBean!com.example.ejb.AdviceHome

09:33:50,299 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor](MSC服务线程1-2)部署单元子部署“EJBProject.jar”部署中名为AdviceBean的会话bean的JNDI绑定“EnterpriseApplicationProject.ear”如下:

java:global/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.Advice
java:app/EJBProject/AdviceBean!com.example.ejb.Advice
java:module/AdviceBean!com.example.ejb.Advice
java:jboss/exported/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.Advice
java:global/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.AdviceHome
java:app/EJBProject/AdviceBean!com.example.ejb.AdviceHome
java:module/AdviceBean!com.example.ejb.AdviceHome
java:jboss/exported/EnterpriseApplicationProject/EJBProject/AdviceBean!com.example.ejb.AdviceHome

我尝试访问EJB的客户端代码是:

package com.example.localclient;

import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import javax.ejb.*;

import com.example.ejb.Advice;
import com.example.ejb.AdviceHome;

@SuppressWarnings("unused")
public class AdviceClient {

public static void main(String[] args) throws Exception {
    new AdviceClient().go();

}

public void go() throws Exception{

    Context ic = new InitialContext();
    Object o = ic.lookup("java:jboss/exported/EJBProject1/AdviceBean");
    AdviceHome home = (AdviceHome) PortableRemoteObject.narrow(o, AdviceHome.class);
    Advice advisor = home.create();
    System.out.println(advisor.getAdvice());
}

}

但是当我运行这个独立客户端时,我得到以下异常:

线程“main”中的异常javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者在applet参数中或在应用程序资源文件中指定类名:java.naming.factory.initial     在javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)     在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)     在javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)     在javax.naming.InitialContext.lookup(InitialContext.java:411)     在com.example.localclient.AdviceClient.go(AdviceClient.java:24)     在com.example.localclient.AdviceClient.main(AdviceClient.java:17)

似乎无法找到JNDI名称,我尝试了在JBoss开始时看到的各种JNDI名称组合(该信息是上面几段)。

我需要一些信息,我确实谷歌,但仍面临同样的问题。

1)我必须在独立客户端中提供什么JNDI名称?

2)由于客户端需要客户端jar(具有接口信息和其他详细信息),我在哪里可以看到?在Jboss的FYI我看到客户端jar为“EJBProjectClient.jar”,但无法在eclipse的Workspace中物理定位这些文件的位置。

任何帮助都非常感谢。

谢谢!

1 个答案:

答案 0 :(得分:1)

您的客户端代码在服务器以外的其他JVM中运行。

您需要的是非常简单的。您需要修改代码以使用服务器的属性而不是本地属性来启动初始上下文。

在此处查看此链接,了解有关如何执行此操作的更多详细信息: http://docs.oracle.com/javase/jndi/tutorial/basics/prepare/initial.html

您需要根据您的环境,提供商网址和上下文工厂更改以下值。

  • 提供程序URL是服务器使用IIOP端口的位置(请参阅 您的服务器设置以获取端口。)
  • 查看服务器对conetxt工厂的文件(抱歉 不知道JBoss的确切值。)

希望这有帮助。