This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
How to access the EJB (session beans) from a swing application keeping code reuse in mind? EJBs are created as the same functionality needs to be web enabled also. I do not want to access the EJB as a web service from client / swing application. Any thoughts or any useful links?
Ok let me give you a small example. Lets say that you have three entity beans (a) user (stores login details) (b) employee (c) department. Each of the above bean has its own home and component interface. You want to expose few methods in these three interfaces to the client. Rather than the client perform a lookup and identifying which interface he needs to use to call a business method, we hide all the complexity in a single business component that that contains and centralizes complex interactions between lower-level business components.
ie., in this case I would create a single stateless session bean and expose the component methods in it.
The above class (SessionFacde) is a stateless session which means that you still need to create a home and component interface. You have now consolidated all the business functions into one big class but still clients have to do all the lookup to get to it. Do you want your clients to know that you are using EJB? No, ofcourse not.
So we need one more level of abstraction. We need a delegate. The delegate will be the contact point for the client. The delegate will perform all the JNDI lookup and call the appropriate business function.
So, your delegate class (remember, its not a session / entity bean) - its a plain old java class will look something like:
Sounds better. But wait!! Whats with all the duplicate code in the DummyDelegate. Wouldn't it be great if all the jndi lookup is handled in one place? Yeah, probably. So here is a better Delegate class
and the servicelocator will be responsible for all the JNDI lookup. How good is that?