ServiceLocator for non-EJB Business Services?
843829Jun 18 2003 — edited Jun 23 2003Hi,
I am developing an example web application using Apache Struts, Tiles, ObjectRelationalBridge and I'm trying to use J2EE design patterns as far as possible. At present I am not using an EJB Business Service layer but just 'Plain Old Java' classes (POJs).
I would like to use the Business Delegate and ServiceLocator patterns so that my Business Service layer, in future, could not only be used by non-Web clients, e.g. Swing, but also allow POJs to be 'swapped out' for EJBs without refactoring the Client Tier. This would require the ServiceLocator to instantiate, cache amd return POJs as well as EJBs (homes) from a single call, e.g. public Object getService(String id).
This raises a number of issues:
1. If the ServiceLocator is a singleton, as most implementations appear to be, then wouldn't this mean only 1 POJ service per JVM and cause an application bottleneck?
2. An alternative to issue 1 would be to have the ServiceSelector cache POJ service factories which return POJ service classes from a pool. If so then why not just use EJBs?
3. Another alternative to issue 1 is to pass an HttpSession or ServletContext object to the ServiceLocator, e.g. getInstance(HttpSession session), and cache POJs in the session or application scope, to allow a number of POJs per application. However does this create too much dependance on the Client Tier and what happens if the session expires?
4. A final alternative to issue 1 is NOT to make the ServiceLocator a singleton. This would allow the Client Tier to instantiate and possibly cache the ServiceLocator as it sees fit, e.g. a Web Tier could instantiate the ServiceLocator in the session or application scope. This would be relatively easy but what are the consequences of this and what are the benefits that would be lost?
5. Another lesser issue is what type should getService() return? Should it be an Object type thus forcing the client to make the appropriate cast? Perhaps it should be an IService type where IService is an interface that POJs and EJBs must implement? Alternatively getService() could accept a Class parameter and try and cast then return the POJ or EJB to the type of the requested class?
Any feedback would be most welcome on the above. Please bear in mind I'm relatively new to the ServiceLocator pattern and EJBs and if my explanation/rants are not clear then I'll happily expand on them.
Regards,
Joss Wright