Using both local and remote interfaces of ejb
I believe the decision of using remote or local interfaces should be determined at the architecture/design phase and use one or the other but not both.
Recently I was asked to provide both implementation and I lost my battle of choosing one or the other. It's a long story why I lost my battle. Well, here is what I did to dynamically determine which interface to use without code or configuration changes. The service consumer can pass in a parameter requesting local/remote to service locator.
First define an interface (let's call it paymentService) where you define all business methods. Make sure all the methods throw Exception. Both remote and Local interface should be extending this interface. The remote interface should override all the methods and throw RemoteException (as required by EJB spec). I really do not like this idea of forcing all the methods to throw Exception. But I could not think of a better way of doing this. The problem is the remote interface methods must throw RemoteException and the local interface must not throw RemoteException.
Now the the create() method fromt the home interface should return the "PaymentService" and not the PaymentEJBServiceLocal or PaymentEJBService (remote), that way the caller can invoke the business methods without knowing whether the service is remote or local. Though it is not required, I would have an explicit catch block for RemoteExceptions in the client code.
Now the ejb should be deployed as two ejbs (local & remote) with two distinct jndi names and the lookup method can be made conditional to return remote or local.
With this solution a client can use the local interface while another client is using remote interface. This is useful when you have both remote and local clients ant they invoke same service.

0 Comments:
Post a Comment
<< Home