从Application Client访问EJB(项目配置)

时间:2011-04-11 13:58:14

标签: java client ejb enterprise

我知道之前有人问过,但我找不到一个好的答案

所以我希望有一个调用远程ejbs的客户端应用程序。

为此,我有2个耳朵申请。 一只耳朵带有ejb模块,另一只耳朵带有应用程序客户端模块。 在客户端模块中,我尝试使用@EJB注释在Main()类中注入EJB。

EJB的application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<application 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:application="http://java.sun.com    /xml/ns/javaee/application_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns    /javaee/application_5.xsd" id="Application_ID" version="5">
  <display-name> 
      MppL4_SERVER</display-name>
      <module>
         <ejb>MppL4_SERVEREJB.jar</ejb>
      </module>
 </application>

app-client的application.xml:

<application 
  <display-name> 
      MppL4_CLIENT
  </display-name>
  <module>
     <java>MppL4_CLIENTClient.jar</java>
  </module>
  <module>
     <ejb>MppL4_SERVEREJB.jar</ejb>
  </module>
</application>

如果我在Glassfish服务器上部署双耳,我会得到:

EVERE: Exception while invoking class org.glassfish.ejb.startup.EjbDeployer load method
java.lang.RuntimeException: EJB Container initialization error
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:219)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:197)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:63)
    at org.glassfish.internal.data.ModuleInfo.load(ModuleInfo.java:175)
    at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:216)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:338)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:183)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:272)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:365)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:204)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:100)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:245)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: Error while binding JNDI name entities.PersonAPIRemote__3_x_Internal_RemoteBusinessHome__ for EJB : PersonAPI
    at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1530)
    at com.sun.ejb.containers.StatelessSessionContainer.initializeHome(StatelessSessionContainer.java:197)
    at com.sun.ejb.containers.ContainerFactoryImpl.createContainer(ContainerFactoryImpl.java:161)
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:207)
    ... 32 more
Caused by: javax.naming.NameAlreadyBoundException [Root exception is org.omg.CosNaming.NamingContextPackage.AlreadyBound: IDL:omg.org/CosNaming/NamingContext/AlreadyBound:1.0]
    at com.sun.jndi.cosnaming.ExceptionMapper.mapException(ExceptionMapper.java:75)
    at com.sun.jndi.cosnaming.CNCtx.callBindOrRebind(CNCtx.java:595)
    at com.sun.jndi.cosnaming.CNCtx.bind(CNCtx.java:620)
    at com.sun.jndi.cosnaming.CNCtx.bind(CNCtx.java:658)
    at javax.naming.InitialContext.bind(InitialContext.java:400)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.publishCosNamingObject(GlassfishNamingManagerImpl.java:224)
    at com.sun.ejb.containers.BaseContainer$JndiInfo.publish(BaseContainer.java:5482)
    at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1515)
    ... 35 more
Caused by: org.omg.CosNaming.NamingContextPackage.AlreadyBound: IDL:omg.org/CosNaming/NamingContext/AlreadyBound:1.0
    at org.omg.CosNaming.NamingContextPackage.AlreadyBoundHelper.read(AlreadyBoundHelper.java:60)
    at org.omg.CosNaming._NamingContextStub.bind(_NamingContextStub.java:67)
    at com.sun.jndi.cosnaming.CNCtx.callBindOrRebind(CNCtx.java:584)
    ... 41 more

SEVERE: Exception while loading the app
java.lang.RuntimeException: EJB Container initialization error
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:219)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:197)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:63)
    at org.glassfish.internal.data.ModuleInfo.load(ModuleInfo.java:175)
    at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:216)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:338)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:183)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:272)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:365)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:204)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:100)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:245)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: Error while binding JNDI name entities.PersonAPIRemote__3_x_Internal_RemoteBusinessHome__ for EJB : PersonAPI
    at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1530)
    at com.sun.ejb.containers.StatelessSessionContainer.initializeHome(StatelessSessionContainer.java:197)
    at com.sun.ejb.containers.ContainerFactoryImpl.createContainer(ContainerFactoryImpl.java:161)
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:207)
    ... 32 more
Caused by: javax.naming.NameAlreadyBoundException [Root exception is org.omg.CosNaming.NamingContextPackage.AlreadyBound: IDL:omg.org/CosNaming/NamingContext/AlreadyBound:1.0]
    at com.sun.jndi.cosnaming.ExceptionMapper.mapException(ExceptionMapper.java:75)
    at com.sun.jndi.cosnaming.CNCtx.callBindOrRebind(CNCtx.java:595)
    at com.sun.jndi.cosnaming.CNCtx.bind(CNCtx.java:620)
    at com.sun.jndi.cosnaming.CNCtx.bind(CNCtx.java:658)
    at javax.naming.InitialContext.bind(InitialContext.java:400)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.publishCosNamingObject(GlassfishNamingManagerImpl.java:224)
    at com.sun.ejb.containers.BaseContainer$JndiInfo.publish(BaseContainer.java:5482)
    at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1515)
    ... 35 more
Caused by: org.omg.CosNaming.NamingContextPackage.AlreadyBound: IDL:omg.org/CosNaming/NamingContext/AlreadyBound:1.0
    at org.omg.CosNaming.NamingContextPackage.AlreadyBoundHelper.read(AlreadyBoundHelper.java:60)
    at org.omg.CosNaming._NamingContextStub.bind(_NamingContextStub.java:67)
    at com.sun.jndi.cosnaming.CNCtx.callBindOrRebind(CNCtx.java:584)
    ... 41 more

WARNING: Exception while cleaning module 'org.glassfish.internal.data.EngineRef@1e493eb'java.lang.NullPointerException
java.lang.NullPointerException
    at org.glassfish.appclient.server.core.AppClientDeployer.clean(AppClientDeployer.java:265)
    at org.glassfish.internal.data.EngineInfo.clean(EngineInfo.java:114)
    at org.glassfish.internal.data.EngineRef.clean(EngineRef.java:171)
    at com.sun.enterprise.v3.server.ApplicationLifecycle$1.actOn(ApplicationLifecycle.java:224)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:342)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:183)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:272)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:365)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:204)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:100)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:245)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:662)

我的app-client主类:

public class Main {

    @EJB
    private static PersonAPIRemote personApi;

    public static void main(String[] args) {
        System.out.println("client started...");

        //Person p = new Person(3, "lololol", 456);
        personApi.service();

        System.out.println("client ended...");
    }

    /* (non-Java-doc)
     * @see java.lang.Object#Object()
     */
    public Main() {
        super();
    }

我正在使用GlassFish Tools Bundle For Eclipse作为IDE。

3 个答案:

答案 0 :(得分:2)

您只能使用容器管理的Bean(例如Servlet,会话bean等)注入EJB。

在您的情况下,您必须通过JNDI查找找到远程接口。

示例:

Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
        props.setProperty("org.omg.CORBA.ORBInitialHost", remoteHost);
        props.setProperty("org.omg.CORBA.ORBInitialPort", remotePort);

        InitialContext ic = new InitialContext(props);
        return ic.lookup(remoteInterface);

答案 1 :(得分:2)

所以我做的是:

  1. 使用JNDI

    Context ctx = new InitialContext();
    personService = (PersonApiRemote) ctx.lookup("api.PersonApiRemote");
    
  2. 将所有Glassfish jar(很多!)添加到Application Client的构建路径中。

答案 2 :(得分:-1)