Wednesday, May 12, 2010

EJB 2.0 Chapter 02 The Client View


Local home has fewer methods than Remote Home.

Client View

Remote Session Bean Client can remove a bean using the bean's home, but a local client cannot.

JNDI - Java naming and directory Interface.
API for accessing naming and directory services.
Organizes things in virtual directory tree. Each level of a tree is either another virtual directory (called context) or an object.

javax.naming.InitialContext - is entry point in JNDI Tree.

Doing Lookup:
Context ic = new InitialContext();
Object o = ic.lookup("Advisor");

If the deployer assigned additional context to the bean, by naming if at deploy time "bar/Advisor", then the lookup code will change to

Object o = ic.lookup("bar/Advisor");
AdviceHome home = (AdviceHome)o;

Naming forces the object returned from the JNDI lookup to be absolutely positively something that implements the home interface i.e. in place of above lookup code we should use

AdviceHome home = (AdviceHome)PortableRemoteObject.narrow(o, AdviceHome.class);

Narrow is used only when the home Interface is remote. The reason is connected to CORBA.

For EJB, the communication between the server and the client is based on RMI (both remote and local interfaces, in fact, do implements thejava.rmi.Remote interface).
The underlying protocol that it is used for the communication is IIOP (I think 1.2), that is part of CORBA standards. It is normally used to describe this communication system using the Java RMI over IIOP.

IIOP has not been designed for Java, but for generic languages, and this means that there are some limitations. Some languages, in fact, do not have the concept of casting.
Java RMI-IIOP provides a mechanism to narrow the the Object you have received from from your lookup, to the appropriate type. This is done through the
javax.rmi.PortableRemoteObject class and, more specifically, using the narrow() method.

Just a note: when you are using the new EJB 2.0 Local Client API, you should be able to do a direct/explicit cast from the looked up Object, to the interface you need.


Casting Vs Narrowing
Casting is object polimorphism with a cast, the object doen't change, but the way you refer the object class changes. With narrowing, you must get actually a different object.

  1. aWire protocol of CORBA.
  2. Can propagate both Transaction and Security information, and can't be sent with a non-IIOP remote method call, so IIOP lets container inter-operate with other servers, including one that isn't Java-based.
  3. All EJBs must be IIOP Compliant.
Home create() method must throw CreateException, RemoteException

Stateless create(): stateless bean can have only one no arg create() method, but stateful canhae many create methods (overloaded). The name of create method in stateful bean must begin with create(i.e. createAccount() etc).

Remote: This interface does not have any method.

Home interface

public interface EJBHome {
EJBMetaData getEJBMetaData() HomeHandle getHomeHandle() void remove(Handle handle) void remove(Object primaryKey)
public interface AdviceHome exetends EJBHome{
public void create();

Container implements the Home interface and matching sub-class.
AdviceHomeImpl - Class of actual remote object and
AdviceHomeImpl_stub - Class of Home object stub.

SessionBean object - getPrimaryKey()
RemoteException - If Remote
EJBException - If Local

EJBObject interface
Object getPrimaryKey()
getEJBHome() -> No need to do JNDI lookup if you have EJBObject.
getHandle()-> Serialize and use again.
remove()->Finished with bean.
isIdentical(Object o) -> To know if two references are of same bean.

Handle Interface
getEJBObject() -> Handle gives the EJBObject but its not the same object of the bean. It has to narrow to the RemoteObject class.

Handle h = this.restoreTheHandle();
Object o = h.getEJBObject();
Shopping cart = (Shopping)PortableRemoteObject.narrow(o, Shopping.class);

Client should have authorization to call methods on the bean. So if a handle is given to someone else, its not a security issue.

Stateless -> True if two Remote EJB objects from same home.
Stateful -> False for any two unique stubs even if from same bean.
Entity -> True for Stubs referring to two entities with same primary key.

equals() vs isIdentical()
equals() method compares two objects on the same Heap, whereas isIdentical() compares two Remote objects on the Server.

JNDI lookups are expensive. Some overhead is served while using this method.

Compiler Law and EJB Spec Law
No Stubs -> While local client view.
Handle -> Remote only.
Local -> No stub no handle. both related to Remote. Local can simply use Java reflection methods and getEJBMetadata.only required for Remote Client.

Two remove in Remote Home
One Remove in RemoteObject
Local clients dont have Handle so local home dont have method remove(handle).

Only create() and remove () must declare exception in Local Client Interface.

RemoteException -> All remote methods can throw (Checked)
EJBException -> All Local Client interface methods can throw(Unchecked). Also used in local interface methods in place of RemoteException (which is used in Remote).

No comments:

Post a Comment

Amazon Best Sellors

Weblogic - Server Creation - Issues

Datasource creation using python script.  Python Script was failing after creating Datasource. From UI Admin Console, if I try to activat...