Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best Practice For Accessing EJB's

 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Just still finding my way around ejb's etc so forgive me if this is no brainer to you...
I've created a stateful session ejb ok and can access it's business methods succesfully using JNDI lookup code within a JSP page.
However, my question is, if I wanted to access that same session bean on other JSP pages do I need to replicate the JNDI lookup code to all the other pages or is there any easier way (I was thinking of custom tags?) ?
Thanks in advance.
Jon
 
Alex Ioannou
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short reply :-
Write a helper class that does all the JNDI so that in jsp you simply call something like :-
EJB = EJBHelper.getEJBHome().create()
or whatever method you wish to use. I always write a helper class for each EJB as a rule of thumb.
 
Ram
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Service Locator pattern
 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies to this, much appreciated.
Alex - So to get this straight in my head, you code a new helper class for each EJB that you create ?
Any chance you could post a smaple piece of code ?
Thanks
Jon
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jon Kidd:
Thanks for the replies to this, much appreciated.
Alex - So to get this straight in my head, you code a new helper class for each EJB that you create ?
Jon

No. you have a single class with only one instance (singleton) and all your ejb's use it. it's all you need.
code... ? there's plenty
http://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.html
http://java.sun.com/blueprints/patterns/ServiceLocator.html
http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
and an improvement to service locator pattern that deals with cache issues:
http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-service.html
also, for more info:
EJB design patterns
in this book it is called EJBHomeFactory.
cheers
 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheesh, and all that is supposed to make it simpler is it ?
I'll look into it further though and give it a shot.
Thanks,
Jon
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServiceLocator is a home factory & cache. I did done something similar a few years ago. It's up and running, but I always worried about it because it does not detect stale or bad homes. Is this a risk if you cache a home for a remote server and the remote server is stopped and restarted? Your cached homes are no good, but there's nothing to tell you to get fresh ones. Or have I just not read ServiceLocator closely enough?
Edit: D'oh! There was the "verified service locator" among your links!
[ April 28, 2003: Message edited by: Stan James ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
ServiceLocator is a home factory & cache. I did done something similar a few years ago. It's up and running, but I always worried about it because it does not detect stale or bad homes. Is this a risk if you cache a home for a remote server and the remote server is stopped and restarted? Your cached homes are no good, but there's nothing to tell you to get fresh ones. Or have I just not read ServiceLocator closely enough?
Edit: D'oh! There was the "verified service locator" among your links!
[ April 28, 2003: Message edited by: Stan James ]

that's exactly what the article with the link I provided is trying to solve.
http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-service.html

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic