About Question enthuware.oce-ejbd.v6.2.383 :

Moderator: admin

Post Reply
ETS User

About Question enthuware.oce-ejbd.v6.2.383 :

Post by ETS User »

I don't understand this question. I thought session beans are bound to 'java:[scope]/appName/moduleName/beanName'. But in this question it appears that 'yourBeanRef' is bound to 'java:comp/env'.

Which areas do I need to study to fully understand this question?

Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by admin »

A bean may be bound to multiple names as governed by the specification section titled "Global JNDI Access". These names can be used by any other bean to locate a bean in a portable way.

Further, a bean reference may be set into another bean's context (using deployment descriptor or @EJB annotation) and in that case the other bean can look it up it its own context. All the such name in the bean's context start with java:comp/env.
16.5.1.1 Injection of EJB References
The Bean Provider uses the EJB annotation to annotate a field or setter property method of the bean
class as a target for the injection of an EJB reference. The reference may be to a session bean’s business
interface or to a session bean’s no-interface view or to the local home interface or remote home interface of a session bean or entity bean.
The following example illustrates how an enterprise bean uses the EJB annotation to reference another
enterprise bean. The enterprise bean reference will have the name
java:comp/env/com.acme.example.ExampleBean/myCart in the referencing bean’s
naming context, where ExampleBean is the name of the class of the referencing bean and
com.acme.example its package. The target of the reference must be resolved by the Deployer,
unless there is only one session bean component within the same application that exposes a client view
type which matches the EJB reference.
package com.acme.example;
@Stateless public class ExampleBean implements Example {
...
@EJB private ShoppingCart myCart;
...
}
If you like our products and services, please help us by posting your review here.

Ravi Bansal

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by Ravi Bansal »

I have a question regarding the @EJB Usage

Is it possible to call inject an EJB using @EJB annotation , which is residing on different App Server than where Caller EJB is residing.

For example AppServer server1 has an EJB A
AppServer server2 has an EJB B

I want to inject B into EJB A using @EJB annotation (without using any look up logic) , where will I give the host name information for AppServer 2 ? I dont see any attribute in EJB which I can use to provide that.
is the only workaround to have Jndi.properties in class path of EJB A ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by admin »

Usually app servers have the capability to configure remote objects. So the deployer configures EJB A in the app server 2 and ties it to a JNDI name, which is used by EJB B. Thus, when EJB B uses @EJB annotation, EJB A in injected (because the deployer has already configured EJB A to a name in app server 2)

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ravi Bansal

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by Ravi Bansal »

Thanks Paul,

But I am little confused , looking at your answer . Question was how to call (or inject) EJB B reference in AppServer 1 when its originally deployed on AppServer 2 . But your answer seems to be the other way around. Thats OK. I got some idea .

Anywaz, can you please explain more briefly by citing example of any App Server ? That will be of real hep.
Also , that means we don't have anything in EJB Specs for this type of scenario . I think , we have a way to specify the Appserver location using props in initial context when doing JNDI look up . Dont we have
something similar available when getting EJB reference using injection. ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by admin »

It has nothing to do with the specification. It is specific to a container. For example, when you deploy an ejb in glassfish, you can include glassfish-ejb-jar.xml in which you can bind the name to a remote ejb. For example:

Code: Select all

<glassfish-ejb-jar>
 <enterprise-beans>
   <ejb>
     <ejb-name>Bean B</ejb-name>
     <ejb-ref>
        <ejb-ref-name>ejb/SomeOtherBean</ejb-ref-name>
        <jndi-name>protocol:host:port#java:global/anotherApp/SomeOtherBean</jndi-name>
       </ejb-ref>
   </ejb>
 </enterprise-beans>
</glassfish-ejb-jar>
Above, we are using someotherbean from another container and tying it to BeanB. To do so, we are using the remote server host and port in the jndi name. The host and port are specific to a container. So you have look at the specification of that container to learn what port and protocol to use.
If you like our products and services, please help us by posting your review here.

rkbansal83
Posts: 33
Joined: Sat Nov 24, 2012 8:52 am
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by rkbansal83 »

Thanks Paul.

Its clear now.

I think same can be achieved by lookup attribute of @EJB annotation.

aab

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by aab »

Hallo,

back to the Q59.

The right answer is: "Object obj = ic.lookup("java:comp/env/yourBeanRef");"
But why should be the answer "Object obj = ic.lookup("java:comp/env/YourBean");" wrong? With this lookup we get a new reference to YourBean.

Thanks!

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by himaiMinh »

But why should be the answer "Object obj = ic.lookup("java:comp/env/YourBean");" wrong?
We don't lookup a bean by its beanName attribute in @EJB. We look up a bean by its name attribute.
The name in @EJB is the name of the environment entry for lookup .
The beanName is the name of the bean the bean, that will be injected ,refers to.

nikitos
Posts: 21
Joined: Mon Oct 24, 2016 6:55 am
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by nikitos »

code will not compile.
InitialContext constructor throws checked exception.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-ejbd.v6.2.383 :

Post by admin »

You are right. Although it does not impact the answer (because compilation error is not one of the options), it has been updated to include throws clause.

thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests