Access EJB from Application Client (project configuration)

有些话、适合烂在心里 提交于 2019-12-12 06:36:35

问题


I know this has been asked before, but i can't find a good answer

So I want to have an client application that calls remote ejbs.

For this i have 2 ear applications. One ear with the ejb module, and another ear with the application client module. In the client module i am trying to inject in the Main() class the EJB with @EJB annotation.

application.xml for the EJB:

<?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>

application.xml for the app-client:

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

If i deploy both ears on the Glassfish server i get:

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)

My app-client main class:

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

I am using GlassFish Tools Bundle For Eclipse as an IDE.


回答1:


You can only inject EJB with container managed Beans i.e. Servlet, Session beans etc.

In your case you will have to locate Remote Interface through a JNDI lookup.

Example:

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



回答2:


So what i done is:

  1. Use JNDI

    Context ctx = new InitialContext();
    personService = (PersonApiRemote) ctx.lookup("api.PersonApiRemote");
    
  2. Add all Glassfish jars (which are a lot!) to the Application Client's build path.




回答3:


Go through these sides

http://javahowto.blogspot.com/2007/06/simple-ejb-3-application-hand-made.html

http://javahowto.blogspot.com/2007/07/simple-ejb-3-servlet-application.html

Classes of referenced EJB project not found in (the referencing) Web project



来源:https://stackoverflow.com/questions/5622385/access-ejb-from-application-client-project-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!