问题
I'm a newbie to EJB3. I want to know that how client know the EJB bean implementation is in remote or local. When i access the bean using InitialContext in client class i want to know wether that bean is local or remote? Sorry if I'm asking stupid question?
回答1:
The type of interface is determined via annotations.
These can be put next to Interface class declaration:
@Local
- declares a local business interface@Remote
- declares a remote business interfaceThen when an EJB extends such interfaces, it uses the interface as a local/remote view. If it extends multiple interfaces, it has multiple views.
These can be put next to EJB class declaration:
@Local(com.example.LocalInterfaceClass)
- declares a local business interface@Remote(com.example.SomeRemoteInterfaceClass)
- declares a remote business interface@LocalBean
- declares a no-interface view (the full bean definition used as an interface)If multiple of above annotations are used in combination, the EJB has multiple interface views. If all are ommitted, the bean defaults to a no-interface view.
You can use JDK inbuilt annotation processing to process annotations during compile time (via javax.annotation.processing classes and javac commandline options). E.g. you could generate code or set options/switches.
You can use reflection to determine annotations at runtime.
Probably cleanest and simplest of all not to have dynamic lookup & behaviour, but just to commit to either Local or Remote for each client and hard-code the appropriate behaviour.
回答2:
There are two different interfaces available when you are writing an EJB. One is remote and one is Local. Remote, as it name suggests is for remote client that want to remotely call (or fire) functions and get some results. On the other hand Local is designed to be used in a local environment, for example in a case if another EJB or even a POJO in your system is using that. The usage would be the same as when you want to use an EJB using its Remote interface. However, it has less headache for the server to handle that. That's the only reason you might want to use a Local interface instead of Remote interface. Local interface is not local to JVM but local like an other POJO class.
Local client view cannot be accessed:
When an EJB or web component is packaged in a different application's EAR packages. When a web component is deployed in a web container, and EJBs are deployed in an EJB container, and these containers are separated (even if they are running on the same machine)
These are main factors in considering a Local or Remote interface:
Client: if your client is not a web component or another bean do not use Local Beans & Beans: Are the beans loosely coupled? then it is a good idea to use Local Scalability: Remote is always better for scalability Local interfaces are recommended for entity beans, which helps with performance issue.
More to read:
http://onjava.com/pub/a/onjava/2004/11/03/localremote.html
http://www.conceptgo.com/gsejb/ov06.html
回答3:
Using @Remote of your interface you can use as Remote interface
@Remote
public interface Cart {
}
Now, Implement this interface to EJB bean.
@Stateful
public class CartBean implements Cart {
}
Similarly using @Local annotation you can use as Local interface.
来源:https://stackoverflow.com/questions/17399117/how-does-client-know-the-ejb-bean-implementation-is-remote-or-local-in-ejb3