简体   繁体   中英

EJB Container initialization error : Error while binding JNDI name?

Spent all day trying to get this to work with no success. Any ideas how this error can be solved?

Here are the three projects that are associated and their structures: 在此处输入图片说明

BasicSessionEJB.java

package ejbModule.test;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class BasicSessionEJB
 */
@Stateless( mappedName="ejb/BasicSessionEJB" )
public class BasicSessionEJB implements BasicSessionEJBRemote {

    /**
     * Default constructor. 
     */
    public BasicSessionEJB() {
        // TODO Auto-generated constructor stub
    }
    public String getMessage() {
        return "Hello, EJB Wolrd!";
   }

}

BasicServlet.java

package src;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.*;
/**
 * Servlet implementation class BasicServlet
 */


public class BasicServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB private ejbModule.test.BasicSessionEJBRemote basicSessionEJB;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try{
            PrintWriter out = response.getWriter();
            out.println("Servlet Message:" + basicSessionEJB.getMessage());
        } catch(Exception e){
             throw new ServletException (e);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try{
            PrintWriter out = response.getWriter();
            out.println("Message:" + basicSessionEJB.getMessage());
        } catch (Exception e){
             throw new ServletException (e);
        }
    }



}

BasicSessionEJBRemote.java

package ejbModule.test;

import javax.ejb.Remote;

@Remote
public interface BasicSessionEJBRemote {
    String getMessage();
}

EJBDesktopClient.java

package appClientModule.test;

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

public class EJBDesktopClient {

    public EJBDesktopClient() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       try {
           Context ctx = new InitialContext();
           ejbModule.test.BasicSessionEJBRemote bean = (ejbModule.test.BasicSessionEJBRemote) ctx.lookup("ejb/BasicSessionEJB");
           System.out.println("Message: " + bean.getMessage());
       } catch (NamingException e) {
           e.printStackTrace();
       }
    }

}

The error log

2016-04-24T01:36:18.285+0100|Severe: Exception while loading the app
2016-04-24T01:36:18.287+0100|Severe: The web application [unknown] created a ThreadLocal with key of type [org.glassfish.pfl.dynamic.codegen.impl.CurrentClassLoader$1] (value [org.glassfish.pfl.dynamic.codegen.impl.CurrentClassLoader$1@77b3a7d8]) and a value of type [org.glassfish.web.loader.WebappClassLoader] (value [WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
2016-04-24T01:36:18.297+0100|Severe: Exception while loading the app : EJB Container initialization error
java.lang.RuntimeException: Error while binding JNDI name ejb/BasicSessionEJB for EJB BasicSessionEJB
    at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1620)
    at com.sun.ejb.containers.StatelessSessionContainer.initializeHome(StatelessSessionContainer.java:190)
    at com.sun.ejb.containers.StatelessContainerFactory.createContainer(StatelessContainerFactory.java:63)
    at org.glassfish.ejb.startup.EjbApplication.loadContainers(EjbApplication.java:221)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:291)
    at org.glassfish.ejb.startup.EjbDeployer.load(EjbDeployer.java:99)
    at org.glassfish.internal.data.ModuleInfo.load(ModuleInfo.java:206)
    at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:313)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:496)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.NameAlreadyBoundException: Use rebind to override
    at com.sun.enterprise.naming.impl.TransientContext.doBindOrRebind(TransientContext.java:322)
    at com.sun.enterprise.naming.impl.TransientContext.bind(TransientContext.java:257)
    at com.sun.enterprise.naming.impl.TransientContext.bind(TransientContext.java:266)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.bind(SerialContextProviderImpl.java:85)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.bind(LocalSerialContextProviderImpl.java:94)
    at com.sun.enterprise.naming.impl.SerialContext.bind(SerialContext.java:640)
    at com.sun.enterprise.naming.impl.SerialContext.bind(SerialContext.java:657)
    at javax.naming.InitialContext.bind(InitialContext.java:429)
    at javax.naming.InitialContext.bind(InitialContext.java:429)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.publishObject(GlassfishNamingManagerImpl.java:212)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.publishObject(GlassfishNamingManagerImpl.java:196)
    at com.sun.ejb.containers.BaseContainer$JndiInfo.publish(BaseContainer.java:4952)
    at com.sun.ejb.containers.BaseContainer.initializeHome(BaseContainer.java:1607)
    ... 46 more

0

I know this is an old thread but I think it's still very important. The solution is quite easy, in the pom.xml file of the web module you need to add the next text

<scope>provided<scope>

this is due because you already have deployed an EJB so when yo try to do it with servlet it will copy it generating the error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM