• 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

Two session beans, one class, much confusion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused as to how env-entry values are scoped when you specify multiple session beans using the same class. Using this kind fellow's work as a starting point, I have a simple session bean defined thus:



Simple POJO, no annotations except the resource. To make sure my environment wasn't on acid (which, incidentally, consists of Sun's JVM and Glassfish V3.0.1 full), I configured this bean with the following ejb-jar.xml:



When I test the class and call SimpleBean.getSomeValue(), it predictably returns "This is the value for SimpleBean", so the injection is clearly working. However, when I wire up two beans like so:



'This is the value for AnotherBean' injected is into both OneBean and AnotherBean. I am looking up my EJB's this way:



This begs the question: can you even configure two session beans using the same class, but with different names and configuration values? I'm sure the answer is staring me in the face, but four hours of Googling has not yielded a solution. Thanks in advance for your help!

Thanks,
Dan





 
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

Dan Bonelli wrote:
However, when I wire up two beans like so:



'This is the value for AnotherBean' injected is into both OneBean and AnotherBean.


Hmm, looks like a bug to me.

Dan Bonelli wrote:

This begs the question: can you even configure two session beans using the same class, but with different names and configuration values?


Yes, you can.
 
Dan Bonelli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Jaikiran.

If I do a lookup from the IntialContext, like so:

I get the same result. When specifying multiple session beans mapping to one class like I'm doing, it seems like Glassfish overwrites someValue with the last one specified in ejb-jar. I need to do some digging in Glassfish's source code to better understand how they go about doing resource injection.

Is there another way aside from ejb-jar.xml and <env-entry> to give a configuration parameter to an EJB? The whole reason I'm even mucking about in ejb-jar.xml like this is because the EJB spec demands that EJBs have a no-argument constructor. Ideally what I'd like to do is define an annotation like so:
which is applied at the injection point, and somehow be able to get at the annotation from inside SimpleBean.
I could make that work with a little introspection if there was some way from within the SimpleBean EJB to see what class and field it had been injected into. From what I can see, that information isn't available at run-time to an EJB.

More generally, what is the standard way of setting per-instance configuration of an EJB once it's been injected? I've resorted to initializing someValue from a method like so:

This works and achieves the desired result, but it seems somewhat odd to me that the designers of the EJB spec gave us a container-managed way to inject beans via annotations, but no simple way to initialize them.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic