无法创建无状态EJB / NamingException?

时间:2014-05-01 12:56:35

标签: java-ee glassfish ejb-3.0 jndi

你好,我是EJB的新手,但是我试图在很长一段时间内找到解决这个问题的方法 - 我有以下设置:

使用@Remote注释的接口:

@Remote
public interface IGeneralManagementBean {

     // some other methods    

    /**
     * Helper method to clear the cache of the IPriceManagementBean
     */
    public void clearPriceCache();
}

具体类:

@Stateless
public class GeneralManagementBean implements IGeneralManagementBean,Serializable {

    @PersistenceContext(unitName="dst")
    private EntityManager em;

    @EJB //some dependent classes below
    private IPriceManagementBean pmb;

    @Override
    public void clearPriceCache() {
        System.out.println("Reached it"); // ******* never reached! *******
       pmb.clearCache();
    }
}

JUnit测试摘要:

    public abstract class AbstractEJBTest {
    protected InitialContext ctx;
    protected JdbcConnection jdbcConnection;
    protected ITestingBean testingBean;
    protected IGeneralManagementBean managementBean;

    protected JDBCTestUtil jdbcTestUtil;

    @Before
    public void init() throws Exception {

        ctx = new InitialContext();
        jdbcConnection = new JdbcConnection();

        JdbcHelper.cleanTables(jdbcConnection);

        testingBean = lookup(ctx, TestingBean.class);
        managementBean = lookup(ctx, GeneralManagementBean.class);


        testingBean.insertTestData();

        jdbcTestUtil = new JDBCTestUtil(jdbcConnection);

            System.out.println("Abstract init"); // ******* is reached! *******
    }
}

JUnit测试:

public class Test_GeneralManagementBean extends AbstractEJBTest {

    @Before
    public void setUp() {
            System.out.println("Before Clear"); // ******* is reached! *******
            managementBean.clearPriceCache();
            System.out.println("After Clear"); // ******* is never reached! *******
    }
}

我在JUnit测试中遇到以下异常(已在Setup中):

 <testcase time="0.09" classname="dst.ass2.ejb.Test_GeneralManagementBean" name="testAddPrice">
    <error message="javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB" type="javax.ejb.EJBException">javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
    at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
    at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
    at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
    at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
    at $Proxy298.clearPriceCache(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor130.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:144)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:174)
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1624)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1486)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:990)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:214)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:742)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:539)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2324)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
    at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
    at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
    ... 21 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
    at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
    at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
    ... 23 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=dst.ass2.ejb.session.GeneralManagementBean/pmb,Remote 3.x interface =dst.ass2.ejb.management.interfaces.IPriceManagementBean,ejb-link=null,lookup=,mappedName=,jndi-name=dst.ass2.ejb.management.interfaces.IPriceManagementBean,refType=Session into class dst.ass2.ejb.session.GeneralManagementBean: Lookup failed for &apos;java:comp/env/dst.ass2.ejb.session.GeneralManagementBean/pmb&apos; in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171)
    at com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1694)
    at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:494)
    ... 25 more
Caused by: javax.naming.NamingException: Lookup failed for &apos;java:comp/env/dst.ass2.ejb.session.GeneralManagementBean/pmb&apos; in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for &apos;Remote ejb-ref name=dst.ass2.ejb.session.GeneralManagementBean/pmb,Remote 3.x interface =dst.ass2.ejb.management.interfaces.IPriceManagementBean,ejb-link=null,lookup=,mappedName=,jndi-name=dst.ass2.ejb.management.interfaces.IPriceManagementBean,refType=Session&apos; .  Actual (possibly internal) Remote JNDI name used for lookup is &apos;dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean&apos; [Root exception is javax.naming.NamingException: Lookup failed for &apos;dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean&apos; in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean not found]]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:599)
    ... 29 more
Caused by: javax.naming.NamingException: Exception resolving Ejb for &apos;Remote ejb-ref name=dst.ass2.ejb.session.GeneralManagementBean/pmb,Remote 3.x interface =dst.ass2.ejb.management.interfaces.IPriceManagementBean,ejb-link=null,lookup=,mappedName=,jndi-name=dst.ass2.ejb.management.interfaces.IPriceManagementBean,refType=Session&apos; .  Actual (possibly internal) Remote JNDI name used for lookup is &apos;dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean&apos; [Root exception is javax.naming.NamingException: Lookup failed for &apos;dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean&apos; in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean not found]]
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:191)
    at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1109)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:776)
    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:744)
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:169)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
    ... 32 more
Caused by: javax.naming.NamingException: Lookup failed for &apos;dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean&apos; in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:186)
    ... 37 more
Caused by: javax.naming.NameNotFoundException: dst.ass2.ejb.management.interfaces.IPriceManagementBean#dst.ass2.ejb.management.interfaces.IPriceManagementBean not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)
    ... 40 more
</error>

知道这个例外的原因是什么?命名不起作用还是我可能遗漏了远程注释的内容?

应用程序在Win 7环境中(成功)部署在Glassfish上,但测试失败。

2 个答案:

答案 0 :(得分:0)

嗯,我认为这是一个“没有很好地思考它”的错误。我用@Remote注释了IPriceManagementBean接口,导致异常。

答案 1 :(得分:0)

是的,通常你必须使用@Local或@Remote注释注释要尝试注入的接口类。您还在为bean(bean池,引用等)使用xml配置吗?