Consuming local EJB, in the same Container but different ears
Asked Answered
N

1

9

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}
Nephritic answered 20/11, 2011 at 17:24 Comment(3)
The "Local" in "Local EJB" means "local to your application". Either merge the EARs or make it a remote EJB.Calle
In the Book "EJB3 Developer Guide" says: Note that we have prefixed the interface definition with the @Remote annotation. This indicates to the EJB container that this bean may be invoked by a remote client. By remote, we mean a client that does not reside in the same Java Virtual Machine (JVM) as the container. The good news is that we can dispense with JNDI altogether if the client runs from within an application client container (ACC).Nephritic
Check one of the methods to solve your problem: #13512528Septime
R
4

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.

Removal answered 20/11, 2011 at 18:53 Comment(4)
Thanks Gonzalo. GlassFish works with a JNDI like java:global[/<app-name>]/<module-name>/<bean-name> I added the annotation @LocalBean to the bean MyLocalBeanImpl i'm trying with @EJB(lookup="java:global/myfirstear/myejbs/MyLocalBeanImpl!my.package.MyLocalBeanImpl") It seems that Glassfish found the Local Bean, but...Nephritic
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 ....Nephritic
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__Nephritic
Have you tried just /<app-name>]/<module-name>/<bean-name>? If you can also update the thread question that would be great, it's not easy to read an exception in a comment ;)Removal

© 2022 - 2024 — McMap. All rights reserved.