• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Resource injection for env entry in the field

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

I have a doubt about injecting the env entry into EJB3.

In EJB 2.1

ejb-jar.xml :


Then in the code :


But in EJB 3 we do not need ejb-jar.xml so that we could use annotation to inject the env entry in the code itself,so we can write the code as follows in one of the beans :



Please let me know that both of these are equivalent?

Also i was going through the specification and came to know that by default the env declared as above will be mapped as package.className/fieldName in the bean's jndi name. But when i try to access the field with that name i am getting NameNotFoundException.

Please hele me to correct the problem why the environment entry is not found?

I also tried to use name in the resource annotation and then in the business method tried to look up with java:comp/env/nameGivenInAnnoatation but again getting the same error.

Or is it mandantory that i should provide ejb-jar.xml file when i use JNDI api ?

I am using Glassfish V3 app server.

Complete bean code i was trying:
 
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
Which application server and which version of it are you using? Also please post the entire exception stacktrace.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have already mentioned the application server:

Glassfish V3

The exception stack trace:



Client code:



I bundled them as ear file and ran the client.I am using Netbeans 6.8 IDE.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me now?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your resource type is declared as int primitive. Could you try Integer type?
 
Deena Jeyachandran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please disregard my last post. Thats incorrect.
 
Deena Jeyachandran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you define @Resource, you are declaring the resource reference to use directly in you class. So I think you don't there is need for jndi lookup. You can use it as a instance variable directly.

If I am correct, I have a question.
If you are declaring the value of variable directly in the class, why do you want to use Resource annotation. It could be a simple instance variable.




 
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
@Siva : did you place the env-entry node in the right place ? It must be placed in the node corresponding your bean. Also it must follow the ENC rules package + class / field.
 
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
@Siva : please consider also this example :

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@Siva : did you place the env-entry node in the right place ? It must be placed in the node corresponding your bean. Also it must follow the ENC rules package + class / field.



I did not create DD at all.

i was going through the specification and came to know about the default naming convention used by the container when Resource or EJB annotation is used.

So as per the specification this "envEntry" should be named as 'java:comp/env/com.scbcd.ejbs.StatefulBeanExample/envEntry'

But when i look up using this name,i am getting NameNotFoundException.

But if i use DD and override the value then i am getting the output from both of PostConstructor and Business method with overriden value but if i remove DD then only PostConstructor works.

With the below DD i am gettin 100 at both the methods but without this, business method failed:





If I am correct, I have a question.
If you are declaring the value of variable directly in the class, why do you want to use Resource annotation. It could be a simple instance variable.



I know that it is of no use to declare Resource annotation at env-entry with the value specified at the EJB class,but i created to test that the the value at the DD overrides this or not.

Thanks.
 
Jaikiran Pai
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
Looking at your JNDI lookup code, I would have expected it to pass. However, I am confused about your Client code. What is it? An application client? Because I see that you are injecting the bean in that Client (which looks like some standalone piece of code).
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is Application Client.

I am using Netbeans IDE where i have created EJB and AppClient Project and bundled them into EAR and deployed the EAR into Glassfish v3.


@Siva : please consider also this example



It doesn't work either.


I tried the below code and the output was:



Output :

INFO: ACDEPL104: Java Web Start services stopped for the app client SCBCDEAR5/SCBCDEAR5-app-client.jar
INFO: Portable JNDI names for EJB StatefulBeanExample : [java:global/SCBCDEAR5/SCBCDEJB5/StatefulBeanExample!com.scbcd.ejbs.StatefulBeanRemote, java:global/SCBCDEAR5/SCBCDEJB5/StatefulBeanExample!com.scbcd.ejbs.StatefulBeanExample]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB StatefulBeanExample : [com.scbcd.ejbs.StatefulBeanRemote#com.scbcd.ejbs.StatefulBeanRemote, com.scbcd.ejbs.StatefulBeanRemote]
INFO: ACDEPL103: Java Web Start services started for the app client SCBCDEAR5/SCBCDEAR5-app-client.jar (contextRoot: /SCBCDEAR5/SCBCDEAR5-app-client)
INFO: SCBCDEAR5 was successfully deployed in 1,062 milliseconds.
INFO: ACDEPL104: Java Web Start services stopped for the app client SCBCDEAR5/SCBCDEAR5-app-client.jar
INFO: Portable JNDI names for EJB StatefulBeanExample : [java:global/SCBCDEAR5/SCBCDEJB5/StatefulBeanExample!com.scbcd.ejbs.StatefulBeanRemote, java:global/SCBCDEAR5/SCBCDEJB5/StatefulBeanExample!com.scbcd.ejbs.StatefulBeanExample]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB StatefulBeanExample : [com.scbcd.ejbs.StatefulBeanRemote#com.scbcd.ejbs.StatefulBeanRemote, com.scbcd.ejbs.StatefulBeanRemote]
INFO: ACDEPL103: Java Web Start services started for the app client SCBCDEAR5/SCBCDEAR5-app-client.jar (contextRoot: /SCBCDEAR5/SCBCDEAR5-app-client)
INFO: SCBCDEAR5 was successfully deployed in 687 milliseconds.
INFO: ACDEPL104: Java Web Start services stopped for the app client SCBCDEAR5/SCBCDEAR5-app-client.jar
INFO: Portable JNDI names for EJB StatefulBeanExample : [java:global/SCBCDEAR5/SCBCDEJB5/StatefulBeanExample!com.scbcd.ejbs.StatefulBeanRemote, java:global/SCBCDEAR5/SCBCDEJB5/StatefulBeanExample!com.scbcd.ejbs.StatefulBeanExample]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB StatefulBeanExample : [com.scbcd.ejbs.StatefulBeanRemote#com.scbcd.ejbs.StatefulBeanRemote, com.scbcd.ejbs.StatefulBeanRemote]
INFO: ACDEPL103: Java Web Start services started for the app client SCBCDEAR5/SCBCDEAR5-app-client.jar (contextRoot: /SCBCDEAR5/SCBCDEAR5-app-client)
INFO: SCBCDEAR5 was successfully deployed in 953 milliseconds.
INFO: postConstruct called
INFO: Business method 100
INFO: java:comp/env/com.scbcd.ejbs.StatefulBeanExample: com.sun.enterprise.naming.impl.JavaURLContext
INFO: java:comp/env/: com.sun.enterprise.naming.impl.JavaURLContext



envEntry variable is not binding anywhere?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To my knowledge

@Resource int envEntry=99;

works as follows: If there is a corresponding entry in DD then the value is taken from DD, otherwise the default is taken. But in contrast to other resources no entry in the beans ENC is created. Therefore the lookup fails. If you inject for example

@PersistenceContext private EntityManager em;

the following returns an entity manager object:

ic.lookup("java:comp/env/com.scbcd.ejbs.StatefulBeanExample/em");
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ejb-spec (section 16.4.1.3, page 413) says:"To support this case, the container must only inject a value for the environment entry if the application
assembler or deployer has specified a value to override the default value. The env-entry-value
element in the deployment descriptor is optional when an injection target is specified. If the element is
not specified, no value will be injected. In addition, if the element is not specified, the named resource is
not initialized in the naming context, and explicit lookups of the named resource will fail."
So it is clear that if env-entry-value is not specified it doesn't bind to the naming context.
But the below statement in the spec (section 16.4.1.2, page 408): "This may be done by means of the EJBContext lookup method or by direct use of the
JNDI interfaces. The environment entries are declared by the Bean Provider by means of annotations on
the bean class or in the deployment descriptor."
This is bit contradicting to the previous statement as, if env entries are not specified in DD it is not accessible though context lookups.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

umakanth godavarthy wrote:This is bit contradicting to the previous statement as, if env entries are not specified in DD it is not accessible though context lookups.


Yes, I agree. But environment entries are the only exception from this rule. An explanation might be:

Entries in the ENC (the ejb's jndi subdirectory) are created during deployment. When a bean instance is created the
container performs a JNDI lookup on the ENC and assigns the retrieved object to the injection target (e.g. the annotated
instance variable).

Now assume there is no entry in the ENC. What should the container do ?

The simplest solution would be to allow default values. That works pretty fine with types that are independent of the
container's environment, such as environment entry types (String, Integer, etc). But what should be the default value of

@PersistenceContext EntityManager em; ?

It had to be something like

@PersistenceContext EntityManager em = new HibernateEntityManagerImpl();

But then the bean code depends on the container's environment, breaking EJB's philosophy of platform-independence.
Therefore default values are no option for such types. So for these types we need entries in ENC (in contrast to
environment entry types).

On the other hand, if we have

@Resource int = 3;

(and no DD entry) it would surely be possible for the container (in principal) to generate a corresponding entry in ENC like
it must be done for the other types. But what would be the usage of it ?

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for helping me to understand this..

It seems that i need to go through specification word by word instead of line by line......
 
reply
    Bookmark Topic Watch Topic
  • New Topic