• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DI with @Resource adnotation understanding

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I try to understand how the DI is working together with the @Resource annotation.
In short, I have a code snippet which looks like :

The Counter is a remote interface - an interface annotated with the @Remote adnotation.
In this case the container injects the counter bean in the variable counterBean, but how it locates this Bean.
Somebody must make a mapping between the "mihai/bussines" name and the needed bean reference and this mapping must be know on the container.

My big question is where is this mapping done ?

Regards,
Mihai

[ September 24, 2008: Message edited by: Mihai Radulescu ]
[ September 24, 2008: Message edited by: Mihai Radulescu ]
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you declare a dependency injection using annotations is the Container's responsibility to inject the corresponding entry from the environment. To do so it issues a JNDI lookup. In general, lookups of objects in the JNDI java: namespace are required to return a new instance of the requested object every time. In your example, the resource to be injected is inferred from the field class' type (Counter.class).

Refer to the following sections of the ejb core specification to get a clearer idea.

Section 16.2:


The container injects entries from the environment into the enterprise bean's fields or methods as specified by the bean's metadata annotations or the deployment descriptor.

The container makes the environment naming context available to the enterprise bean instances at runtime. The enterprise bean's instances can use the EJBContext lookup method or the JNDI interfaces to obtain the values of the environment entries.



Section 16.2.1 (Sharing of Environment Entries)


In general, lookups of objects in the JNDI java: namespace are required to return a new instance of the requested object every time. Exceptions are allowed for the following:
� The container knows the object is immutable (for example, objects of type
java.lang.String), or knows that the application can't change the state of the object.
� The object is defined to be a singleton, such that only one instance of the object may exist in the JVM.
� The name used for the lookup is defined to return an instance of the object that might be shared. The name java:comp/ORB is such a name.

In these cases, a shared instance of the object may be returned. In all other cases, a new instance of the requested object must be returned on each lookup. Note that, in the case of resource adapter connection objects, it is the resource adapter's ManagedConnectionFactory implementation that is responsible for satisfying this requirement.

Each injection of an object corresponds to a JNDI lookup. Whether a new instance of the requested object is injected, or whether a shared instance is injected, is determined by the rules described above.



The name you provided in your example is the name under which that resource is available in your bean's environment.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio

Thanks for your answer. But I still have one question :
How the container knows which bean instance must be injected. The container knows the bean name (the JNDI name of the resource) and it search it in its context, but before this somebody must map the JNDI name to a resource.

Somebody must do a mapping between the JNDI name and the resource before the DI occurs, otherwise the container can not locate the resource.
My question (still) is : where is mapping ?

Regards
M
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mihai,

I don't think the name specified in the annotation is used by the Container to get a reference to an EJB.

I'll quote a fragment form O'Reilly's book, which is the book I used:


@EJB ProcessPaymentLocal processPayment;

The specification isn't very detailed in terms of exactly how the EJB container should resolve this reference. To give you a feel for how this process works, let's see how the JBoss application server resolves this reference:
1. The only possible identifier for this EJB reference is the business interface type. The application server first looks for a unique EJB in the referencing EJB's EJB-JAR deployment that uses ProcessPaymentLocal as its local or remote interface. If more than one EJB uses the same business interface, it throws a deployment exception.
2. If the EJB-JAR is deployed as part of an Enterprise ARchive (.ear), it looks in other EJB-JARs for a unique EJB that uses the ProcessPaymentLocal interface. Again, if more than one EJB uses the same business interface, it throws a deployment exception.
3. If the EJB reference is not found in the .ear file, it looks for it in other global EJB-JAR deployments. If the beanName( ) attribute is specified, then JBoss uses the same search process, but it uses the beanName( )'s value as an additional identifier. If the mappedName( ) attribute is specified, then no search process is performed. The application server expects that a specific EJB is bound into the global JNDI under the mappedName( )'s value.



This is in section 14.2.1.5. Resolving EJB references.
[ September 24, 2008: Message edited by: Sergio Tridente ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio,

on the point 3 you (or the O'Reilly's books) say :


If the beanName( ) attribute is specified, then JBoss uses the same search process, but it uses the beanName( )'s value as an additional identifier.



This name can be specified with the name parameter from the Stateless or Statefull annotations. So if I have a bean declared like :
@Stateless(name = "myBean") then I can use the DI like :

@Resource(name = "myBean") MyInterface myBean;


Is this correct ?

And my next question is if I want to get the myBean using the JNDI lookup
which JDNI I must use ? the "java:comp/env/myBean" ?


Regards,
M
[ September 25, 2008: Message edited by: Mihai Radulescu ]
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't use the @Resource annotation for injecting EJBs. The @EJB annotation is used:

 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About JNDI name and lookup.

You have three possibilities:

1) Using the default name:



In this case the JNDI name would be "java:comp/env/com.mypackage.MyOtherBean/myBean"

2) Specifying a name when injecting the bean.


In this case, it would be "java:comp/env/myBeanName".

3) Finally, you don't inject the bean but declare the dependency by annotating MyOtherBean class. In this case the "name" and the "beanInterface" attributes are mandatory:


In this case, the JNDI name would be "java:comp/env/myBeanName".
[ September 25, 2008: Message edited by: Sergio Tridente ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio

Thanks fro your detailed answer, but I still have 2 questions.

The first question is about the @Resource and the EJB.
In the "EJB in action" in on Chapterv 5, sub-chapter 5.2.1. I found the following information about the @Resource


....
However the (Resource) annotation can be also used for e-mail server resources, environment entries, Orb references or even EJB instances.
....



My second question is about the JNDI lock up done by hand. Here I mean something like :


can I use this kind of look up from a POJO client ? In this case the String argument for the lookup method will be the JNDI name descibed by you in the previous posts.
I keep trying this but I get always a NammingException.

Regards,
M
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mihai Radulescu:
even EJB instances.



I did not know about that. I fail to see any reference to that in the ejb core specification. Section 16.5.1.1 (Injection of EJB References) states:


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 the local home interface or remote home interface of a session bean or entity bean.




Originally posted by Mihai Radulescu:
can I use this kind of look up from a POJO client ? In this case the String argument for the lookup method will be the JNDI name descibed by you in the previous posts.
I keep trying this but I get always a NammingException.



You certainly can use lookup for getting a reference to a session bean's remote interface. However, when we talk about clients that not reside in the same application server you need to use another mechanism to specify the JNDI name of your bean.

For example, in JBoss the default JNDI name of your bean's interface would be "MyBeanClass/remote" (for the bean's remote interface). In Glassfish it would be "com.mypackage.MyInterface", if MyInterface is a remote interface of the MyBeanClass' bean.
Mapping a session bean to a JNDI name is done by the Deployer (role). How it would be done, depends on the Application Server implementation. For example, in Glassfish you can use the "META-INF/sun-ejb-jar.xml" descriptor:



Where:
- The name specified in ejb-name corresponds to the name specified in @Stateless(name="MyBeanClass"), @Stateful(name="MyBeanClass") or the default name of the bean (the unqualified class name).
- The name specified in jndi-name is the name you would use to perform a JNDI lookup of your bean's interface, in this case it would be "ejb/MyBeanNameForJNDI".

Please, understand that the name you specify in the @Stateless/@Stateful annotations is NOT necessarily the JNDI name (but it is in JBoss). Section 19.2 of the ejb core specification states:


Enterprise bean�s name. A logical name is assigned to each enterprise bean in the ejb-jar file. There is no architected relationship between this name and the JNDI name that the Deployer will assign to the enterprise bean. The Bean Provider can specify the enterprise bean�s name in the ejb-name element. If the enterprise bean�s name is not explicitly specified in metadata annotations or in the deployment descriptor, it defaults to the unqualified name of the bean class.



I hope this explanation would give you a better idea of what's going on. Nevertheless, I recommend you to read the ejb core specification, in this case chapters 16 and 19.
[ September 25, 2008: Message edited by: Sergio Tridente ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio,

and thanks for your explanations, now I think I got an overview picture - or at least I hope. I am on beginning whit by EJB and I don't have to much knowledge/experince about - that why my questions are a little bit to easy but this I think will change.

For the moment I read the "EJB in action" by Debu Panda and paralel with this I try to "brush" a little the specifications.
Do you think that the issue with bean DI with Resource is a failure in the book ?

Regards,
M
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if it is possible to use @Resource to get a reference to an EJB, maybe by specifying mappedName and defining the reference name in your Container's specific configurations files. But that's not the point: it is not defined in the specifications, so from the exam point of view you should not care about that possibility. However, if you are curious and have the time, I encourage you to try it by yourself: knowledge cannot hurt (only what people do with knowledge).

About your questions being a "little bit easy", I do not agree. I was not able to find the answer to those questions until I went through the specification several times, specifically chapter 19 which I have read three days before the exam!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic