Client can NOT call EJB direct, but through mediator
JMS in case of MDB(Message Driven Bean)
Container provides class implementing bean's interface:
component interface &
home interface
Instance of class implementing component interface is called
EJBObject
client call EJB through two player:
EJBObject &
container
Things to remember - Inside a Bean
-
Do not directly read/write File Descriptor
-
Never try to obstain security policy of source code
-
Never use object substitute or subclass feature of java serializable protocol
-
Never pass
this as parametor or return
-
Do not implement Bean as Server-like activity e.g. listener, multicasting on a socket
-
Never create/set/access classloader
-
Never set/create security manager
-
Never try to stop JVM
-
Never try to change input/output , error stream
-
Never try to set factory that is used by Server socket, and stream handle factory that is used by URL
-
Never
-
start, stop, resume a thread
-
change priority of a thread
-
change name of a thread
Same Bean can not define two env entries with same name
Different Bean can defined same env entries with same name, as when they access from bean it get its own variable
EJB communicate with container through SessionContext & SessionBean Interface
Home interface has create() and remove() method, create() needs to be defined, but remove() is inherited from parent
Create method in EJB
|
StateFul |
StateLess |
declare |
mandatory, no argument method |
at least one create() is mandatory, e.g.
createABC(...)
createBBB(...)
|
Bean instance creation |
create a new bean |
get bean from pooling |
Common property |
argument must be serializable, primitive |
argument must be serializable,primitive |
Remove method in EJB
|
Entity |
StateFul |
StateLess |
1. Remove(PrimaryKey)
2. Remove(Handle)
|
1 |
2. will remove instance |
2. no effect, will go back to pool
|
Common |
|
Contaner event, client can not call |
Contaner event, client can not call |
There is exactly one Object for each Session Bean Type, Multi EJBObject
e.g. 100 clients have 1 HomeInterface, but 100 EJbObject
method starting with ejbMethodA...() is container callback method
method can not throw RunTimeException(...)
isdential() method of EJBObject
-
stateful bean never been true
-
stateless bean return true, if referenced the client's handled by the same homeInterface
-
EjbObject are compared by isIdentical(), NOT equals()
EJBObject's remove() doesnt have argument, remove itself
HomeInterface's remove(...) has argument, as it needs argument to determine which one to remove
HomeLocalInterface does NOT have method for Handler & metaDate( in save JVM, information of method can be abtained from reflection)
Local HomeInterface doesn't have remove(handler), remove can be done directly call the LocalEJBOBject to remove itself
Remove sessionbean client can remove via RemoteHomeInterface & remote EJBObject
LocalHomeInterface can NOT be passed to remove call
In Bean method, dont declare throw RemoteException
setEntityContext and setSessionContext are used to setup sessionBean to interact with container
For stateful bean, when user call create(), createABC() in home Interface, EJB container will call ejbCreate() or ejbCreateABC() defined in session Bean respectively
EntityContext & SessionContext
-
One difference is that the entityContext has getPrimaryKey()
-
setSessionContext() can call two method of container: get Home Interface, and JNDI access
-
following method ejbCreate().
remove method call for statelss bean is ignored
Container will not passivate if session is in Transaction
remove is not call under:
-
time out in passivate state
-
bean throw exception
-
server crashes
Entity Bean
-
If can not find single entity, throw ObjectNotFoundException
-
If can not find group entity, return empty collection
For CMP, don't need write find method
For BMP, each findXXX method in Home Interface, must have a match ejbFindXXX in Bean Class
Home Interface can implement business logic which is not entity related
for each method, you are required to write ejbHomeXXX method in Bean
Each Bean Type has its own pool
Entity bean can only use ONLY CMT demartation, can NEVER use getUserTransaction method
Only difference between EntityContext & SessionContext is getPrimaryKey() defined in EntityContext
EJB Remote interface is closely related to RMI-IIOP protocol
Parameter passed into EJB method must be RMI-IIOP conpatible
EJB Object - Container generated Remote Interface Implementation
Local Object - Container generated local interface implementation
Home Object - Container generated home interface implementation
Loca home Object - Container generated local home interface implementation
System exception - Container will process it
Application excepton - should throw back to client/ the only exception for remote EJB thrown back is REMOTE or its subClass
EJB bean is called via EJB Object, thus if your bean calls another bean, you must pass reference to your bean's EJB Object rather than reference of your bean; Just like another client, everything must go through EJB Object
When bean is passivated, it might hold reference to container-implemented object, the following object will be passivated as well
-
EJB Object reference
-
Home Object reference
-
EJB context reference
-
JNDI naming context
Stateless can define a few overloaded ejbCreate(...)
Stateful can only define one empty ejbCreate()
The biggest difference between the stateful session bean and the other bean types is that stateful session beans don't use instance pooling.