You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by "borislav.iordanov@emerson.com" <bo...@emerson.com> on 2014/11/17 21:43:27 UTC

using local ejb reference in the embedded container

Hi all,

I'm new to OpenEJB and I'm trying to learn with a small made up example.
Specifically, I have 1 stateless bean that I want to give a local ejb
reference and be able to perform a JNDI lookup on that reference. So I have
a Java interface, its implementation, an META-INF/ejb-jar.xml and a main
program.  My problem is that the lookup by the local reference name fails.
Here are my short files:

DataRepo.java

@Local
public interface DataRepo
{
    List<Point> getInterestingPoints();
}

DataRepoImpl.java

@Stateless( name = "dataRepo" )
@TransactionManagement( value = TransactionManagementType.CONTAINER )
@TransactionAttribute( value = TransactionAttributeType.REQUIRED )
@LocalBean
public class DataRepoImpl implements DataRepo
{
    @Override
    public List<Point> getInterestingPoints()
    {
        return null;
    }
}

main program:

{
        Properties p = new Properties();
        final Context context =
EJBContainer.createEJBContainer(p).getContext();
        DataRepo repo =
(DataRepo)context.lookup("java:comp/env/ejb/TheRepo"));
}

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee">
<enterprise-beans>
    <session>
        <ejb-name>dataRepo</ejb-name>
        <ejb-local-ref>
            <ejb-ref-name>ejb/TheRepo</ejb-ref-name>
            <local>optimus.DataRepo</local>
        </ejb-local-ref>
    </session>
</enterprise-beans>
</ejb-jar>

I can find the ejb from its auto-constructed "java:global/..." name. I'm
also sure that ejb-jar.xml is taken into consideration because when I
purposefully introduce an error in it OpenEJB complains.

What am I doing wrong? Why is my ejb-ref-name ignored here?

Thanks much in advance!

Best,
Boris



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by Jean-Louis Monteiro <jl...@tomitribe.com>.
No problem.
If you are in a learning stage, we have some pretty simple but very good
and focused examples.
For example the simplest possible example you can find with a stateless is
http://tomee.apache.org/examples-trunk/simple-stateless/README.html

We have plenty more of those depending on what you want to achieve, we can
probably drive you more in.

Jean-Louis

--
Jean-Louis Monteiro
http://twitter.com/jlouismonteiro
http://www.tomitribe.com

On Mon, Nov 17, 2014 at 10:34 PM, borislav.iordanov@emerson.com <
borislav.iordanov@emerson.com> wrote:

> Thanks Jean-Louis, those are good to know! I think I picked that code up
> from
> somewhere where they thought it more didactic to show what's available.
>
> I did notice the JNDI names output of OpenEJB. However, for some reason I
> didn't think it gave the complete picture and that there were some
> available
> bindings not traced out.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672901.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: using local ejb reference in the embedded container

Posted by "borislav.iordanov@emerson.com" <bo...@emerson.com>.
Thanks Jean-Louis, those are good to know! I think I picked that code up from
somewhere where they thought it more didactic to show what's available. 

I did notice the JNDI names output of OpenEJB. However, for some reason I
didn't think it gave the complete picture and that there were some available
bindings not traced out.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672901.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by Jean-Louis Monteiro <jl...@tomitribe.com>.
Hey Borislav,

a couple of additional notes if you want.
As Romain mentioned, the spec only authorize global JNDI names from the
EJBContainer global context. Check out the logs. OpenEJB/TomEE outputs the
JNDI names so that you don't have to guess.

Regarding your code, you are defining default annotations. I mean,
container managed transaction is the default so you don't need to add it,
as well as the REQUIRED transaction behavior.
The ejb-jar.xml is no more required since EJB 3.0 in most of the cases.

Even the local interface is not required if you don't plan to have more
implementations.
So basically, having just @Stateless should work.

Jean-Louis

--
Jean-Louis Monteiro
http://twitter.com/jlouismonteiro
http://www.tomitribe.com

On Mon, Nov 17, 2014 at 9:56 PM, Romain Manni-Bucau <rm...@gmail.com>
wrote:

> Hi
>
> EJBContainer.createEJBContainer(p).getContext(); only provides global
> naming by spec
>
>
> Romain Manni-Bucau
> @rmannibucau
> http://www.tomitribe.com
> http://rmannibucau.wordpress.com
> https://github.com/rmannibucau
>
>
> 2014-11-17 21:43 GMT+01:00 borislav.iordanov@emerson.com
> <bo...@emerson.com>:
> > Hi all,
> >
> > I'm new to OpenEJB and I'm trying to learn with a small made up example.
> > Specifically, I have 1 stateless bean that I want to give a local ejb
> > reference and be able to perform a JNDI lookup on that reference. So I
> have
> > a Java interface, its implementation, an META-INF/ejb-jar.xml and a main
> > program.  My problem is that the lookup by the local reference name
> fails.
> > Here are my short files:
> >
> > DataRepo.java
> >
> > @Local
> > public interface DataRepo
> > {
> >     List<Point> getInterestingPoints();
> > }
> >
> > DataRepoImpl.java
> >
> > @Stateless( name = "dataRepo" )
> > @TransactionManagement( value = TransactionManagementType.CONTAINER )
> > @TransactionAttribute( value = TransactionAttributeType.REQUIRED )
> > @LocalBean
> > public class DataRepoImpl implements DataRepo
> > {
> >     @Override
> >     public List<Point> getInterestingPoints()
> >     {
> >         return null;
> >     }
> > }
> >
> > main program:
> >
> > {
> >         Properties p = new Properties();
> >         final Context context =
> > EJBContainer.createEJBContainer(p).getContext();
> >         DataRepo repo =
> > (DataRepo)context.lookup("java:comp/env/ejb/TheRepo"));
> > }
> >
> > <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee">
> > <enterprise-beans>
> >     <session>
> >         <ejb-name>dataRepo</ejb-name>
> >         <ejb-local-ref>
> >             <ejb-ref-name>ejb/TheRepo</ejb-ref-name>
> >             <local>optimus.DataRepo</local>
> >         </ejb-local-ref>
> >     </session>
> > </enterprise-beans>
> > </ejb-jar>
> >
> > I can find the ejb from its auto-constructed "java:global/..." name. I'm
> > also sure that ejb-jar.xml is taken into consideration because when I
> > purposefully introduce an error in it OpenEJB complains.
> >
> > What am I doing wrong? Why is my ejb-ref-name ignored here?
> >
> > Thanks much in advance!
> >
> > Best,
> > Boris
> >
> >
> >
> > --
> > View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896.html
> > Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: using local ejb reference in the embedded container

Posted by "borislav.iordanov@emerson.com" <bo...@emerson.com>.
Ah, that explains. I thought local meant local to the J2EE component which in
my case I interpreted to be the module containing the ejb-jar.xml. 

Anyway, your solution works and it will do perfectly well for my purposes.

Thanks again for the quick problem resolution!

Boris



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672925.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Ok,

the ejb-local-ref in a session bean is local to the sessoin bean. This means:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1">
  <enterprise-beans>
    <session>
      <ejb-name>dataRepo</ejb-name>
    </session>
    <session>
      <ejb-name>MainEJB</ejb-name>
      <ejb-local-ref>
        <ejb-ref-name>ejb/TheRepo</ejb-ref-name>
        <local>optimus.DataRepo</local>
      </ejb-local-ref>
    </session>
  </enterprise-beans>
</ejb-jar>

works (that said since you have a custom "run" bean (MainEJB) you can
also get it injected in this bean without any xml config.

This is different from webapp descriptors (web.xm) where the
ejb-local-ref is global to the webapp (to be more generic the
reference is in the scope of the parent tag ie the webapp or the
session bean in which it is defined).





Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-18 0:05 GMT+01:00 borislav.iordanov@emerson.com
<bo...@emerson.com>:
> Here it is:
>
> https://github.com/bolerio/embeddedejb
>
> Thank you!
> Boris
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672908.html
> Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by "borislav.iordanov@emerson.com" <bo...@emerson.com>.
Here it is:

https://github.com/bolerio/embeddedejb

Thank you!
Boris



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672908.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by Jean-Louis Monteiro <jl...@tomitribe.com>.
Could you share this simple example on github for example so that we can
have a look quickly?
It's easier usually.

Thanks
Jean-Louis

--
Jean-Louis Monteiro
http://twitter.com/jlouismonteiro
http://www.tomitribe.com

On Mon, Nov 17, 2014 at 10:44 PM, borislav.iordanov@emerson.com <
borislav.iordanov@emerson.com> wrote:

> Oops, apologies! The last statement of my last post is false: in fact put
> the
> wrong EJB name in  ejb-jar.xml DOES lead to an excpetion:
>
> Caused by: java.lang.NullPointerException: name cannot be null
>         at
> org.apache.openejb.core.TempClassLoader.loadClass(TempClassLoader.java:106)
>         at
> org.apache.openejb.core.TempClassLoader.loadClass(TempClassLoader.java:79)
>         at
>
> org.apache.openejb.config.AnnotationDeployer$ProcessAnnotatedBeans.deploy(AnnotationDeployer.java:2299)
>         at
>
> org.apache.openejb.config.AnnotationDeployer$ProcessAnnotatedBeans.deploy(AnnotationDeployer.java:1819)
>         at
>
> org.apache.openejb.config.AnnotationDeployer.deploy(AnnotationDeployer.java:360)
>         at
>
> org.apache.openejb.config.ConfigurationFactory$Chain.deploy(ConfigurationFactory.java:401)
>         at
>
> org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:962)
>         at
>
> org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:314)
>         ... 7 more
>
> Otherwise, I simply get a name not found:
>
> javax.naming.NameNotFoundException: Name "comp/env/ejb/TheRepo" not found.
>         at
> org.apache.openejb.core.ivm.naming.IvmContext.federate(IvmContext.java:199)
>         at
> org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext.java:151)
>         at
> org.apache.openejb.core.ivm.ContextHandler.lookup(ContextHandler.java:51)
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672903.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: using local ejb reference in the embedded container

Posted by "borislav.iordanov@emerson.com" <bo...@emerson.com>.
Oops, apologies! The last statement of my last post is false: in fact put the
wrong EJB name in  ejb-jar.xml DOES lead to an excpetion:

Caused by: java.lang.NullPointerException: name cannot be null
	at
org.apache.openejb.core.TempClassLoader.loadClass(TempClassLoader.java:106)
	at
org.apache.openejb.core.TempClassLoader.loadClass(TempClassLoader.java:79)
	at
org.apache.openejb.config.AnnotationDeployer$ProcessAnnotatedBeans.deploy(AnnotationDeployer.java:2299)
	at
org.apache.openejb.config.AnnotationDeployer$ProcessAnnotatedBeans.deploy(AnnotationDeployer.java:1819)
	at
org.apache.openejb.config.AnnotationDeployer.deploy(AnnotationDeployer.java:360)
	at
org.apache.openejb.config.ConfigurationFactory$Chain.deploy(ConfigurationFactory.java:401)
	at
org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:962)
	at
org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:314)
	... 7 more

Otherwise, I simply get a name not found:

javax.naming.NameNotFoundException: Name "comp/env/ejb/TheRepo" not found.
	at
org.apache.openejb.core.ivm.naming.IvmContext.federate(IvmContext.java:199)
	at
org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext.java:151)
	at
org.apache.openejb.core.ivm.ContextHandler.lookup(ContextHandler.java:51)



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672903.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by "borislav.iordanov@emerson.com" <bo...@emerson.com>.
Hi,

That seems like a promising approach, but I wasn't able to get it to work. 
Here is the actual code:

            MainEJB mainEJB =
(MainEJB)context.lookup("java:global/embeddedejb/MainEJB");
            mainEJB.go(new Runnable() {
                public void run()
                {
                    Context initCtx = null;
                    try
                    {
                        initCtx = new InitialContext();
                        DataRepo repo =
(DataRepo)initCtx.lookup("java:comp/env/ejb/TheRepo");
                    }
                    catch (NamingException e)
                    {
                        e.printStackTrace();
                    }
                }
            });

I should not that those are the only JNDI objects traced out at startup
time:

INFO - Jndi(name="java:global/embeddedejb/dataRepo!optimus.DataRepoImpl")
INFO - Jndi(name="java:global/embeddedejb/dataRepo!optimus.DataRepo")
INFO - Jndi(name="java:global/embeddedejb/dataRepo")
INFO - Jndi(name="java:global/embeddedejb/MainEJB!optimus.MainEJB")
INFO - Jndi(name="java:global/embeddedejb/MainEJB")

I don't see my custom ejb/TheRepo anywhere. My ejb-jar.xml is under
target/classes/META-INF where target/classes is the Maven compilation output
folder. If I modify ejb-jar.xml to not be valid XML any longer, I get an
exception from openejb. However, if I modify it to misspell the EJB I'm
trying to refer (for example, instead of <ejb-name>dataRepo</ejb-name>, I
put <ejb-name>dataRepo2</ejb-name>), I don't get any errors! 

Thanks a lot!
Boris




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672902.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

a workaround can be to have an EJB:

@Singleton @Lock(READ)
public class NamingEjb {
  public void run(fnal Runnable run) {
     run.run();
  }
}


and execute your code as a runnable with it. Then it will be executed
in the EJB JNDI context and it should work


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-17 22:06 GMT+01:00 borislav.iordanov@emerson.com
<bo...@emerson.com>:
> Hi Romain,
>
> Thanks for the lightening quick answer! I hadn't realized that. Is there an
> alternative if I want to run a simple main program with local references
> available for lookup? I actually also tried bootstrapping with 'new
> InitialContext' instead of EJBContainer.createEJBContainer, but with same
> result.
>
> My goal is first to setup a testing environment for an existing application,
> but then see if it can work as a production environment as well. The local
> ejb references end up being accessed by the Spring framework.
>
> Thanks!
> Boris
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672898.html
> Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by "borislav.iordanov@emerson.com" <bo...@emerson.com>.
Hi Romain,

Thanks for the lightening quick answer! I hadn't realized that. Is there an
alternative if I want to run a simple main program with local references
available for lookup? I actually also tried bootstrapping with 'new
InitialContext' instead of EJBContainer.createEJBContainer, but with same
result. 

My goal is first to setup a testing environment for an existing application,
but then see if it can work as a production environment as well. The local
ejb references end up being accessed by the Spring framework.

Thanks!
Boris



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896p4672898.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: using local ejb reference in the embedded container

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

EJBContainer.createEJBContainer(p).getContext(); only provides global
naming by spec


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-17 21:43 GMT+01:00 borislav.iordanov@emerson.com
<bo...@emerson.com>:
> Hi all,
>
> I'm new to OpenEJB and I'm trying to learn with a small made up example.
> Specifically, I have 1 stateless bean that I want to give a local ejb
> reference and be able to perform a JNDI lookup on that reference. So I have
> a Java interface, its implementation, an META-INF/ejb-jar.xml and a main
> program.  My problem is that the lookup by the local reference name fails.
> Here are my short files:
>
> DataRepo.java
>
> @Local
> public interface DataRepo
> {
>     List<Point> getInterestingPoints();
> }
>
> DataRepoImpl.java
>
> @Stateless( name = "dataRepo" )
> @TransactionManagement( value = TransactionManagementType.CONTAINER )
> @TransactionAttribute( value = TransactionAttributeType.REQUIRED )
> @LocalBean
> public class DataRepoImpl implements DataRepo
> {
>     @Override
>     public List<Point> getInterestingPoints()
>     {
>         return null;
>     }
> }
>
> main program:
>
> {
>         Properties p = new Properties();
>         final Context context =
> EJBContainer.createEJBContainer(p).getContext();
>         DataRepo repo =
> (DataRepo)context.lookup("java:comp/env/ejb/TheRepo"));
> }
>
> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee">
> <enterprise-beans>
>     <session>
>         <ejb-name>dataRepo</ejb-name>
>         <ejb-local-ref>
>             <ejb-ref-name>ejb/TheRepo</ejb-ref-name>
>             <local>optimus.DataRepo</local>
>         </ejb-local-ref>
>     </session>
> </enterprise-beans>
> </ejb-jar>
>
> I can find the ejb from its auto-constructed "java:global/..." name. I'm
> also sure that ejb-jar.xml is taken into consideration because when I
> purposefully introduce an error in it OpenEJB complains.
>
> What am I doing wrong? Why is my ejb-ref-name ignored here?
>
> Thanks much in advance!
>
> Best,
> Boris
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.n4.nabble.com/using-local-ejb-reference-in-the-embedded-container-tp4672896.html
> Sent from the TomEE Users mailing list archive at Nabble.com.