• 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

JNDI and java.annotation.Resource

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize if I should ask this in the EJB or JBOSS forums, but it seemed to be a general @Resource questions. I am not sure I understand how the @Resource annotation works. I am running on java 1.7, JEE 6, and JBOSS AS 7.1.

The javax.annotation.Resources documentation indicates that the name parameter is the JNDI name of the resource. But lookups that fail using a JNDI name and thev@Resource annotation succeed when a JNDI context is obtained and used to look up the resource. For example in the code below I can successfully get a mail session if I comment out the @Resource annotation and uncomment the @PostConstruct annotation. In both cases the string I use for JNDI name is "mail/yahoo"



If I run the code as shown attempting to use the @Resource annotation jboss reports the following error:

JBAS014775: New missing/unsatisfied dependencies:
service jboss.naming.context.java.comp.EJB05EAR.CHAP05EJB.StatusMailerBean.env.mail.yahoo (missing) dependents: [service jboss.deployment.subunit."EJB05EAR.ear"."CHAP05EJB.jar".component.StatusMailerBean.START]

The configuration entry in standalone.xml for the mail session is

<mail-session jndi-name="java:/mail/yahoo" from="jeff_gaer@yahoo.com">
<smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp-yahoo">
<login name="jeff_gaer" password="xxxxxxxxxxx"/>
</mail-session>

I have a similar problem with otherJNDI lookups. I haven't used the @Resource annotation outside of the context of the examples in the "Beginning EJB3" (apress publications),
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using the lookup attribute with the full JNDI name, including the java:// prefix:
I did that recently on a project and it worked without a problem.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a common confusion. Previous discussions on this can be found here, which might help understand what those attributes mean:

https://coderanch.com/t/164146/java-EJB-SCBCD/certification/Resource#796865
https://coderanch.com/t/622076/java-EJB-SCBCD/certification/Resource-lookup

P.S: In those discussions there's a reference to the mappedName attribute. You can/should replace that by "lookup" attribute (which is available in newer versions) as Rob noted in his post.
reply
    Bookmark Topic Watch Topic
  • New Topic