You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Edison Xu <xe...@gmail.com> on 2012/09/21 07:20:14 UTC

Difference of initializing context

Hi,

for initializing the context, basically there are two ways:
1. Context ctxt = new InitialContext(p);
2. Context ctxt = EJBContainer.createEJBContainer(map).getContext();

What's the difference of using this two way of getting context? 
I thought it should be the same. But some different result coming:
1. When using createEJBContainer, OpenEJB cannot load jndi.properties for
testing automatically. Instead, I have to put everything in the map as the
parameter.
Is it possible to load jndi.properties automatically by using this way?

2. In my test project (you can find it  here
<https://github.com/EdisonXu/Test/tree/master/ejb-intro>  ), I used same way
to get the context and bind the injection. 
But unfortunately, project message-client doesn't work while project
message-server works fine.
I didn't see any difference on the usage, but why doe one work and another
doesn't?
Am I making some mistakes?




--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
jndi.properties is managed by the JVM
(see com.sun.naming.internal.ResourceManager#getInitialEnvironment)

so i don't think we should add it in the EJBContainer.

But you can probably re-use this method:
EJBContainer.createEJBContainer(ResourceManager.getInitialEnvironment(new
Properties());

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: http://rmannibucau.wordpress.com*
*LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*




2012/9/24 Clare <se...@mail.ustc.edu.cn>

> Hi
> In Edison's project.
> 1.I use @ManagedBean instead of @LocalClient.
> 2.I bind like this...
>     Properties p = new Properties();
>     p.put("openjpa.jdbc.SynchronizeMappings",
> "buildSchema(ForeignKeys=true)");
>     p.put(Context.INITIAL_CONTEXT_FACTORY,
> "org.apache.openejb.client.LocalInitialContextFactory");
>     // ctxt = new InitialContext(p);
>     p.put(OpenEjbContainer.Provider.OPENEJB_ADDITIONNAL_CALLERS_KEY,
> getClass().getName());
>     EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
>
> It works but seems that it still can not load the jndi.properties, am I
> making some mistakes?
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657616.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Difference of initializing context

Posted by Clare <se...@mail.ustc.edu.cn>.
Hi
In Edison's project.
1.I use @ManagedBean instead of @LocalClient.
2.I bind like this...
    Properties p = new Properties();
    p.put("openjpa.jdbc.SynchronizeMappings",
"buildSchema(ForeignKeys=true)");
    p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
    // ctxt = new InitialContext(p);
    p.put(OpenEjbContainer.Provider.OPENEJB_ADDITIONNAL_CALLERS_KEY,
getClass().getName());
    EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
 
It works but seems that it still can not load the jndi.properties, am I
making some mistakes?



--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657616.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
On trunk localclient works for both normally ;)
Le 23 sept. 2012 05:26, "Anthony Fryer" <ap...@hotmail.com> a écrit :

> You don't need the application-client.xml file if you use the @ManagedBean
> annotation on your unit test classes instead of @LocalClient.  Using
> @ManagedBean also means @Inject will work in your test cases and you can
> re-use the same container across mutlipe junit tests.
>
> Using @LocalClient you bind using this code...
>
> Properties p = new Properties();
> p.put(Context.INITIAL_CONTEXT_FACTORY,
> "org.apache.openejb.client.LocalInitialContextFactory");
> InitialContext ctx = new InitialContext(p);
> ctx.bind("inject", this);
>
>
> Using @ManagedBean you have to bind like this...
>
> ejbContainer.getContext().bind("inject", this);
>
> I recommend replacing @LocalClient with @ManagedBean, get rid of
> application-client.xml and changing your test cases to use
> ejbContainer.getContext().bind("inject", this);
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657604.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Difference of initializing context

Posted by Anthony Fryer <ap...@hotmail.com>.
You don't need the application-client.xml file if you use the @ManagedBean
annotation on your unit test classes instead of @LocalClient.  Using
@ManagedBean also means @Inject will work in your test cases and you can
re-use the same container across mutlipe junit tests.

Using @LocalClient you bind using this code...

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
InitialContext ctx = new InitialContext(p);
ctx.bind("inject", this);


Using @ManagedBean you have to bind like this...

ejbContainer.getContext().bind("inject", this);

I recommend replacing @LocalClient with @ManagedBean, get rid of
application-client.xml and changing your test cases to use
ejbContainer.getContext().bind("inject", this);




--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657604.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
FYI:  https://issues.apache.org/jira/browse/OPENEJB-1901  (just commited)

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: http://rmannibucau.wordpress.com*
*LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*




2012/9/21 Romain Manni-Bucau <rm...@gmail.com>

> PS: another way is to annotate javax.annotation.ManagedBean the test class
> instead of LocalClient
>
>
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau*
> *Blog: http://rmannibucau.wordpress.com*
> *LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*
>
>
>
>
> 2012/9/21 Romain Manni-Bucau <rm...@gmail.com>
>
>> maybe try this instead of @BeforeClass and @Before (need some clen up but
>> just to share the idea):
>>
>> https://gist.github.com/3760928
>>
>> *Romain Manni-Bucau*
>> *Twitter: @rmannibucau*
>> *Blog: http://rmannibucau.wordpress.com*
>> *LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*
>>
>>
>>
>>
>> 2012/9/21 Romain Manni-Bucau <rm...@gmail.com>
>>
>>> please update your github then say how to reproduce quickly ("go to ...
>>> and run clean test) and what you expect (if you can save me some minutes
>>> avoiding to look all the code ;))
>>>
>>> i'll try to have a quick look this evening
>>>
>>> *Romain Manni-Bucau*
>>> *Twitter: @rmannibucau*
>>> *Blog: http://rmannibucau.wordpress.com*
>>> *LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*
>>>
>>>
>>>
>>>
>>> 2012/9/21 Edison Xu <xe...@gmail.com>
>>>
>>>> Previously, I put the application-client.xml under
>>>> message-server/src/test/java/resources/META-INF folder.
>>>> Actually, it should be under message-server/src/test/resources/META-INF
>>>> folder.
>>>>
>>>> I'm not sure whether the container can read it. But it is working fine
>>>> then.
>>>>
>>>> Now I correct it and add ejb-jar.xml under
>>>> message-server/src/main/java/resources/META-INF folder, but it's not
>>>> working
>>>> anymore....
>>>> The situation has been totally reversed after I did same thing for
>>>> message-client.
>>>> Message-client is working.... Message-server is not....
>>>> OMG~
>>>>
>>>> WARN - Injection data not found in JNDI context:
>>>>
>>>> jndiName='comp/env/com.edison.test.beans.MessageDbManagerTest/transactionalCaller',
>>>> target=com.edison.test.beans.MessageDbManagerTest/transactionalCaller
>>>> !
>>>>
>>>> This is the warn from message-server. It seems this Inner class hasn't
>>>> been
>>>> loaded. I tried to add the ejb-jar.xml under test/resources/META-INF,
>>>> but no
>>>> good either.
>>>>
>>>> If I remove application-client.xml and jndi.properties both,
>>>> message-server
>>>> is working...
>>>>
>>>> I'm really confused.. :(
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657572.html
>>>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>>>
>>>
>>>
>>
>

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
PS: another way is to annotate javax.annotation.ManagedBean the test class
instead of LocalClient



*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: http://rmannibucau.wordpress.com*
*LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*




2012/9/21 Romain Manni-Bucau <rm...@gmail.com>

> maybe try this instead of @BeforeClass and @Before (need some clen up but
> just to share the idea):
>
> https://gist.github.com/3760928
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau*
> *Blog: http://rmannibucau.wordpress.com*
> *LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*
>
>
>
>
> 2012/9/21 Romain Manni-Bucau <rm...@gmail.com>
>
>> please update your github then say how to reproduce quickly ("go to ...
>> and run clean test) and what you expect (if you can save me some minutes
>> avoiding to look all the code ;))
>>
>> i'll try to have a quick look this evening
>>
>> *Romain Manni-Bucau*
>> *Twitter: @rmannibucau*
>> *Blog: http://rmannibucau.wordpress.com*
>> *LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*
>>
>>
>>
>>
>> 2012/9/21 Edison Xu <xe...@gmail.com>
>>
>>> Previously, I put the application-client.xml under
>>> message-server/src/test/java/resources/META-INF folder.
>>> Actually, it should be under message-server/src/test/resources/META-INF
>>> folder.
>>>
>>> I'm not sure whether the container can read it. But it is working fine
>>> then.
>>>
>>> Now I correct it and add ejb-jar.xml under
>>> message-server/src/main/java/resources/META-INF folder, but it's not
>>> working
>>> anymore....
>>> The situation has been totally reversed after I did same thing for
>>> message-client.
>>> Message-client is working.... Message-server is not....
>>> OMG~
>>>
>>> WARN - Injection data not found in JNDI context:
>>>
>>> jndiName='comp/env/com.edison.test.beans.MessageDbManagerTest/transactionalCaller',
>>> target=com.edison.test.beans.MessageDbManagerTest/transactionalCaller
>>> !
>>>
>>> This is the warn from message-server. It seems this Inner class hasn't
>>> been
>>> loaded. I tried to add the ejb-jar.xml under test/resources/META-INF,
>>> but no
>>> good either.
>>>
>>> If I remove application-client.xml and jndi.properties both,
>>> message-server
>>> is working...
>>>
>>> I'm really confused.. :(
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657572.html
>>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>>
>>
>>
>

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
maybe try this instead of @BeforeClass and @Before (need some clen up but
just to share the idea):

https://gist.github.com/3760928

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: http://rmannibucau.wordpress.com*
*LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*




2012/9/21 Romain Manni-Bucau <rm...@gmail.com>

> please update your github then say how to reproduce quickly ("go to ...
> and run clean test) and what you expect (if you can save me some minutes
> avoiding to look all the code ;))
>
> i'll try to have a quick look this evening
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau*
> *Blog: http://rmannibucau.wordpress.com*
> *LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*
>
>
>
>
> 2012/9/21 Edison Xu <xe...@gmail.com>
>
>> Previously, I put the application-client.xml under
>> message-server/src/test/java/resources/META-INF folder.
>> Actually, it should be under message-server/src/test/resources/META-INF
>> folder.
>>
>> I'm not sure whether the container can read it. But it is working fine
>> then.
>>
>> Now I correct it and add ejb-jar.xml under
>> message-server/src/main/java/resources/META-INF folder, but it's not
>> working
>> anymore....
>> The situation has been totally reversed after I did same thing for
>> message-client.
>> Message-client is working.... Message-server is not....
>> OMG~
>>
>> WARN - Injection data not found in JNDI context:
>>
>> jndiName='comp/env/com.edison.test.beans.MessageDbManagerTest/transactionalCaller',
>> target=com.edison.test.beans.MessageDbManagerTest/transactionalCaller
>> !
>>
>> This is the warn from message-server. It seems this Inner class hasn't
>> been
>> loaded. I tried to add the ejb-jar.xml under test/resources/META-INF, but
>> no
>> good either.
>>
>> If I remove application-client.xml and jndi.properties both,
>> message-server
>> is working...
>>
>> I'm really confused.. :(
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657572.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>
>

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
please update your github then say how to reproduce quickly ("go to ... and
run clean test) and what you expect (if you can save me some minutes
avoiding to look all the code ;))

i'll try to have a quick look this evening

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: http://rmannibucau.wordpress.com*
*LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*




2012/9/21 Edison Xu <xe...@gmail.com>

> Previously, I put the application-client.xml under
> message-server/src/test/java/resources/META-INF folder.
> Actually, it should be under message-server/src/test/resources/META-INF
> folder.
>
> I'm not sure whether the container can read it. But it is working fine
> then.
>
> Now I correct it and add ejb-jar.xml under
> message-server/src/main/java/resources/META-INF folder, but it's not
> working
> anymore....
> The situation has been totally reversed after I did same thing for
> message-client.
> Message-client is working.... Message-server is not....
> OMG~
>
> WARN - Injection data not found in JNDI context:
>
> jndiName='comp/env/com.edison.test.beans.MessageDbManagerTest/transactionalCaller',
> target=com.edison.test.beans.MessageDbManagerTest/transactionalCaller
> !
>
> This is the warn from message-server. It seems this Inner class hasn't been
> loaded. I tried to add the ejb-jar.xml under test/resources/META-INF, but
> no
> good either.
>
> If I remove application-client.xml and jndi.properties both, message-server
> is working...
>
> I'm really confused.. :(
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657572.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Difference of initializing context

Posted by Edison Xu <xe...@gmail.com>.
Previously, I put the application-client.xml under
message-server/src/test/java/resources/META-INF folder.
Actually, it should be under message-server/src/test/resources/META-INF
folder.

I'm not sure whether the container can read it. But it is working fine then.

Now I correct it and add ejb-jar.xml under
message-server/src/main/java/resources/META-INF folder, but it's not working
anymore....
The situation has been totally reversed after I did same thing for
message-client.
Message-client is working.... Message-server is not....
OMG~

WARN - Injection data not found in JNDI context:
jndiName='comp/env/com.edison.test.beans.MessageDbManagerTest/transactionalCaller',
target=com.edison.test.beans.MessageDbManagerTest/transactionalCaller
!

This is the warn from message-server. It seems this Inner class hasn't been
loaded. I tried to add the ejb-jar.xml under test/resources/META-INF, but no
good either.

If I remove application-client.xml and jndi.properties both, message-server
is working...

I'm really confused.. :(




--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657572.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hmm, what do you call wrong place?

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: http://rmannibucau.wordpress.com*
*LinkedIn: http://www.linkedin.com/pub/romain-manni-bucau/43/544/956*




2012/9/21 Edison Xu <xe...@gmail.com>

> Hi,
>
> I found a strange issue. The application-client.xml in message-server is
> put
> into a wrong place, but it's working.
> After I fixed it and placed it to the correct place
> test/resources/META-INF/, I found there's no such file in message-client
> project, so I copy one to the same folder of message-client.
> And the amazing thing happen, test in message-client is working now.....
>
> Then I remove the file from message-server, and delete the installed
> package
> under .m2/repository/ folder either, I run test of message-server again,
> and
> it's still working...
>
> This is really strange.
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657569.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Difference of initializing context

Posted by Edison Xu <xe...@gmail.com>.
Hi, 

I found a strange issue. The application-client.xml in message-server is put
into a wrong place, but it's working. 
After I fixed it and placed it to the correct place
test/resources/META-INF/, I found there's no such file in message-client
project, so I copy one to the same folder of message-client. 
And the amazing thing happen, test in message-client is working now.....

Then I remove the file from message-server, and delete the installed package
under .m2/repository/ folder either, I run test of message-server again, and
it's still working...

This is really strange.



--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657569.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Edison Xu <xe...@gmail.com>.
Can you pls check the test project here?
https://github.com/EdisonXu/Test/tree/master/ejb-intro

Same way to get Context, but it's working for message-server, not for
message-client.

Thanks,

Edison



--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657567.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Maybe share your sample to show us limitations in a unit test?
Le 21 sept. 2012 08:03, "Edison Xu" <xe...@gmail.com> a écrit :

> Thanks for your reply.
> I'm a little confused. I thought they should be the same thing essentially.
> But the differences are there.
>
> Did I make some mistake? Or it's the limitation of current version?
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657562.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Difference of initializing context

Posted by Edison Xu <xe...@gmail.com>.
Thanks for your reply.
I'm a little confused. I thought they should be the same thing essentially. 
But the differences are there.

Did I make some mistake? Or it's the limitation of current version?




--
View this message in context: http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559p4657562.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Difference of initializing context

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi, the context of EJBContainer is the one of the spec (java:global...)
while new InitialContext is pur 'historical' one.
 Le 21 sept. 2012 07:20, "Edison Xu" <xe...@gmail.com> a écrit :

> Hi,
>
> for initializing the context, basically there are two ways:
> 1. Context ctxt = new InitialContext(p);
> 2. Context ctxt = EJBContainer.createEJBContainer(map).getContext();
>
> What's the difference of using this two way of getting context?
> I thought it should be the same. But some different result coming:
> 1. When using createEJBContainer, OpenEJB cannot load jndi.properties for
> testing automatically. Instead, I have to put everything in the map as the
> parameter.
> Is it possible to load jndi.properties automatically by using this way?
>
> 2. In my test project (you can find it  here
> <https://github.com/EdisonXu/Test/tree/master/ejb-intro>  ), I used same
> way
> to get the context and bind the injection.
> But unfortunately, project message-client doesn't work while project
> message-server works fine.
> I didn't see any difference on the usage, but why doe one work and another
> doesn't?
> Am I making some mistakes?
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Difference-of-initializing-context-tp4657559.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>