使用本地EJB,在同一个Container但不同的耳朵

时间:2011-11-20 17:24:25

标签: java java-ee ejb ejb-3.0 glassfish-3

我正在试图在同一个Glassfish中使用本地EJB,但不同的耳朵。但Glassfish无法找到本地EJB或无法使用

我读到了这个: 根据JavaEE教程,@ local bean的客户端必须在与其访问的企业bean相同的JVM中运行。“

在第一只耳朵,     我在jar里面有本地接口

@Local
public interface MyLocalBean {
  int getNumber(int num3);
} 

在另一个jar中,我有实现

@Stateless
@LocalBean
public class MyLocalBeanImpl implements MyLocalBean,Serializable{
     public MyLocalBeanImpl() {}
     public int getNumber(int num3){......

在第二只耳朵, 在同一个Glassfish

我在jar中有本地接口

@Local
public interface MyLocalBean {
int getNumber(int num3);
 } 

在另一个罐子里,我有消费者

@Stateless
@LocalBean
public class BeanConsumer{
@EJB(name="MyLocalBeanImpl")
private MyLocalBean beanlocal;
带有@EJB和(name =“MyLocalBeanImpl”)参数的

错误是:

Cannot resolve reference Local ejb-ref name=MyLocalBeanImpl,Local 3.x 
interface =my.package.MyLocalBean,ejb-link=null,lookup=,
mappedName=,jndi-name=,refType=Session|#]

使用@EJB和(name =“my.package.MyLocalBeanImpl”)参数,错误是:

Cannot resolve reference Local ejb-ref name=my.package.MyLocalBeanImpl,
Local 3.x interface =my.package.MyLocalBean,ejb-link=null,
lookup=,mappedName=,jndi-name=,refType=Session|#]***

我也尝试使用mappedName

    @Stateless(name="MyLocalBeanImpl",mappedName ="MyLocalBeanImpl")
    public class MyLocalBeanImpl implements MyLocalBean{

@EJB(name="MyLocalBeanImpl",mappedName ="MyLocalBeanImpl")
private MyLocalBean beanlocal;

但错误仍然存​​在

 Cannot resolve reference Local ejb-ref name=MyLocalBeanImpl,
 Local 3.x interface =my.package.MyLocalBean,ejb-link=null,
 lookup=,mappedName=MyLocalBeanImpl,jndi-name=,refType=Session

GlassFish适用于像

这样的JNDI
 java:global[/<app-name>]/<module-name>/<bean-name>

我将注释@LocalBean添加到bean MyLocalBeanImpl

@Stateless
@LocalBean
public class MyLocalBeanImpl implements MyLocalBean{
     public MyLocalBeanImpl() {}
     public int getNumber(int num3){......

当我部署Glassfish时说jndi是两个

  Portable JNDI names for EJB OtroBean
  java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl  
  and
  java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBean

我正在尝试

 @EJB(lookup="java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl")

Glassfish似乎找到了Local Bean,但是...... Glassfish在构建像这样的东西时遇到了问题

 javax.naming.NamingException
 javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
 at     
 com.sun.ejb.containers.StatelessSessionContainer
 ._getContext(StatelessSessionContainer.java:454)
 .....
 Caused by: javax.ejb.EJBException: 
 javax.ejb.CreateException: Could not create stateless EJB
 ....
 Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: 
 Exception attempting to inject Local ejb-ref name=my.package.BeanConsumer/beanlocal,
 Local 3.x interface =my.package.MyLocalBean,ejb-link=null,
 lookup=java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl,
 mappedName=,jndi-name=,refType=Session into class my.package.BeanConsumer: 
 Can not set my.package.MyLocalBean field my.package.BeanConsumer.beanlocal 
 to   my.package.beans.__EJB31_Generated__MyLocalBeanImpl__Intf____Bean__

尝试

 @EJB(lookup="java:global/myfirstear/myejbs/MyLocalBeanImpl")

我明白了:

throws javax.naming.NamingException
 javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB

 Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: 
 Exception  attempting to inject Local ejb-ref   
 name=my.package.BeanConsumer/beanlocal,
 Local 3.x  interface =my.package.MyLocalBean,
 ejb-link=null,
 lookup=java:global/myfirstear/myejbs/MyLocalBeanImpl,
 mappedName=,jndi-name=,refType=Session 
 into class  my.package.BeanConsumer: 
 Lookup failed for   'java:comp/env/my.package.BeanConsumer/beanlocal' 
 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}

1 个答案:

答案 0 :(得分:3)

可以将本地EJB注入同一JVM中的另一个EJB,但不同的是。

问题在于您不能再依赖bean的名称,因为您需要告诉使用者bean完整的JNDI名称。您可以在部署期间在服务器日志中找到此名称;当bean绑定到JNDI目录时,服务器会在日志中显示它。