This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Retrieve reference to (all the) session beans at runtime with EJB3 and JBoss?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm testing some new ideas and I have a situation where I want to dynamically retrieve beans and invoke methods in those beans.

I played before with dynamic delegate and stuff so I know how to invoke the methods dynamically. What I need need is get all the session beans dynamically without have to use @EJB of lookup with hardcode interfaces/names.

To give you an idea lets say I got 20 session beans in total. Of those 20 there are 10 are located in package com.xx.yy. Of those 10 beans only 5 contain the method foo().I want to get the references to those 10 beans dynamically. So when I add another bean to the project and deploy it my code (in the package com.xx.yy) will still invoke the methods on that beans without a change to the code.

After I got all the references I will call the getMethods and check if it contains the method foo() and invoke it.

Anyone know if this is possible ?

 
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeroen Bs wrote:
After I got all the references I will call the getMethods and check if it contains the method foo() and invoke it.



You mean you are going to invoke them using Java reflection? If yes, then any reason why you want those classes to be EJBs? (just trying to get a better picture of what you are doing)
 
Jeroen Bs
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i'm just experimenting a bit. I am playing around with reflection and yes they need to be EJB because they are already in use. The foo() in this case is a sort of check/monitoring function which I want to implement to the EJB. I want to invoke the method every X minutes. Now I can just add the EJB reference to my check code but it would be nice if my check could could fetch all the bean references itself and call the foo() methode if its implemented.

The idea is that if a add a EJB with foo() it will be invoked (or if I add the foo() to an existing EJB) without having to think about adding it to the check code.

I know it looks a bit overkill (or pointless ;-)) but its a test project and want to try some new ideas thats why I was wondering if it was possible at all to get the beans on-the-fly.

Edit: Oh and I am aware of the performance impact but thats not an issue
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which specific version of JBoss AS are you using? With JBoss AS-5 you can do this in a application server specific way. If you are looking for some portable way of doing this, then i can't think of any easy way out.
 
Jeroen Bs
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The platform itself won't change and will only be used on EJB3 with JBoss AS 5.1.0 GA. But a more portable solution would be nicer, but it isn't needed. I don't need complete code just some help in the right direction or some code snippets
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeroen Bs wrote:Of those 20 there are 10 are located in package com.xx.yy.



If they have a common JNDI path, you could use Context.listBindings(String) to get everything in a given subcontext.

http://java.sun.com/j2se/1.5.0/docs/api/javax/naming/Context.html#listBindings(java.lang.String)

For example, if you have 3 beans bound to the following names:

- ejb/com/xx/yy/SessionBeanA
- ejb/com/xx/yy/SessionBeanB
- ejb/com/xx/yy/SessionBeanC

You could do something like this to get them:



(Please note that I haven't actually tried this... you might have to play around with it a bit)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!