Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming local EJB, in the same Container but different ears

I'm triying to consume a Local EJB in the same Glassfish, but different ears. But Glassfish can't found the local EJB or can't consume

I read this: According to the JavaEE tutorial, the client of a @Local bean "must run in the same JVM as the enterprise bean it accesses."

In the first ear, I have the local Interface inside a jar

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

In another jar, I have the implementation

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

In the second ear, in the same Glassfish

I have the local Interface inside a jar

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

In another jar, I have the consumer

@Stateless
@LocalBean
public class BeanConsumer{
@EJB(name="MyLocalBeanImpl")
private MyLocalBean beanlocal;

with @EJB and (name="MyLocalBeanImpl") parameter the error is:

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

with @EJB and (name="my.package.MyLocalBeanImpl") parameter the error is:

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|#]***

I tried too with mappedName

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

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

but the error persist

 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 works with a JNDI like

 java:global[/<app-name>]/<module-name>/<bean-name>

I added the annotation @LocalBean to the bean MyLocalBeanImpl

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

and when I deployed Glassfish say the jndi are two

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

i'm trying with

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

It seems that Glassfish found the Local Bean, but... Glassfish have problems casting o something like this

 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__

And trying with

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

I get:

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}
like image 210
Kaltresian Avatar asked Nov 20 '11 17:11

Kaltresian


1 Answers

It is possible to inject a local EJB into another EJB in the same JVM but different ear.

The problem is that you cannot rely on the name of the bean anymore as you need to tell the consumer bean the complete JNDI name. You can find this name in the server logs during deployment; the moment the bean is bound to the JNDI directory the server shows it in the logs.

like image 151
Gonzalo Garcia Lasurtegui Avatar answered Oct 20 '22 22:10

Gonzalo Garcia Lasurtegui