Beruflich Dokumente
Kultur Dokumente
• These are long-lived; they exist across client sessions, are shared by multiple clients, and
remain alive even after a restart of the server or other failure.
• Typically, an Entity EJB object represents a set of data in persistent storage.
• These methods analogous to the methods of the same name in the SessionBean Interface.
• ejbPassivate() is called before an Entity object is swapped out and ejbActivate() is called
after the Entity Object is swapped in.
ejbLoad() and ejbStore()
• These operations allow the Entity bean to synchronize its state with an underlying
database.
• Under bean managed persistence the developer has to write the coding in these methods.
• Under container managed persistence no coding is needed for these methods.
• setEntityContext() passes an object that inherits from EJBContext() to the EJB instance.
• The container invokes the ejbRemove() method when a client has called the remove()
method of the bean’s home interface.
ejbFindByPrimaryKey()
(Ex: when the customer bean is removed, the data will be deleted from the
underlying database.)
o Clients can interact with EJB either through CORBA or through RMI.
Name : <filename>
Enterprise-Bean:True
• At the deployment time, the EJB container must read the ejb-jar file, create
implementations for the home and remote interfaces.
• Reads the deployment-descriptor and ensures that the bean has what it wants and ad the
bean’s property settings to the environment so that they’re available to the bean at
runtime.
• The client can use either RMI or CORBA to connect to the EJB
• The client looks up the EJB’s home interface using a naming service (JNDI or COS)
• Invokes a find() or create() method on the home interface to get a reference to an EJB
object.
• And then uses the EJB object as if it were an ordinary object.
Roles in EJB
Roles in EJB
• EJB specification gives a clear description of each of these roles and their associated
responsibilities.
b) Deployer
• The deployer takes an ejb-jar file and installs it into an EJB container.
• The deployer’s task begins with the receipt of an ejb-jar file, and ends with the
installation of the ejb-jar file in the container.
• This task is typically handled by the System Administrator in MIS world.
c) Application Assembler
• An EJB Server Provider provides a server that can contain EJB containers.
• The EJB 1.0 specification does not describe the interface between the server and the
container, so the EJB server provider and the EJB container provider will likely be one
and the same for any given product.
• The diagram shows the relationships among the EJB server, the EJB container, and the
EJB bean.
• An EJB server provides services to an EJB container and an EJB container provides
services to an Enterprise Java Bean.
• The role of the EJB server provider is similar to that of a database system vendor.
• System Administrators are responsible for the day-to-day operation of the EJB server.
• Their responsibilities include keeping security information up-to-date and monitoring the
performance of the server.
Session Beans
Session Beans
• These are generally tied to the lifetime of a given client session.
• They are relatively short-lived.
• The stateful session objects are created in response to a single client’s request,
communicate exclusively with a single client and die when the client no longer needs
them.
• A session bean is required to implement the javax.ejb.SessionBean interface.
• Al of the methods declared in this interface are callback methods; that is they are
invoked by the container to notify the Session EJB that some event has occurred.
ejbPassivate()
• Container calls this method to swap the EJB class out to “semi-persistent” storage.
• The storage is “semi-persistent” because Session EJBs do not carry the same sort of
guarantee of long life as Entity EJBs do.
• If the EJB container crashes or restarts, the session beans that existed before the
crash or restart are destroyed.
• Session bean also have an associated timeout value; after the timeout expires, they
are destroyed.
• If none of these events happens the session bean can be sapped back in at some later
time.
ejbActivate()
ejbRemove()
• This callback method in invoked by the container as a result of a client calling the
remove() method of a bean’s home or remote interface.
• The client’s invocation of the remove() method notifies the container that this
particular session bean instance should be destroyed.
setSessionContext(SessionContext ctx)
getCallerIdentity()
• This method is used to get a java.security.Identity object that represents the identity
of the client.
• It can be used in concert with isCallerInRole() to allow the EJB object to perform
authorization checks before performing an operation.
getEJBHome()
getEnvironment()
getRollbackOnly()
isCallerInRole()
• This boolean method can be used to test whether the caller has assumed a given role.
• An EJB developer might specify several different roles that can be played by the
users of the class.
setRollbackOnly()
• The method must have a void return type. The container will pass the remote
interface back to the client.
• The number and type of arguments to the ejbCreate() method must exactly match
the number and type of arguments in the corresponding create() method declared in
the home interface.
The following steps are involved in the creation of a new Session Bean:
• A stateless session bean is one that doesn’t keep track of any information from one
method call to another – essentially, a bean with no instance variables. (Ex:
CalculateInterest Bean)
• Any two instances of a stateless bean are equivalent.
• A container can easily handle a stateless Session bean, because instances of a
stateless bean can be created and destroyed at will, without worrying about their
state at the time of their destruction.
• A stateful session bean, is a bean that changes state during a conversation – that is, a
bean that has instance variables. (Ex: Shopping Cart Bean)
• The life cycle of a stateful bean is more complex for the container than the stateless
bean.
• Because, the container have to maintain the state of the stateful session bean during
swap out and swap in process.
• According to the EJB specification, EJB Objects are not allowed to have static
variables.
afterBegin()
beforeCompletion()
afterCompletion()
• The container calls this method after a transaction has been completed.
• Its boolean argument will be set to true if the transaction was committed
successfully, and false if the transaction was rolled back.
• This interface must be implemented in the stateful session bean because, the state of
instance variables is not transactional.
• That is, if the instance variables in a session bean are modified during the course of
a transaction and the transaction is then rolled back, the instance variables will not
be automatically reset to their original state prior to the transaction.
• To reset the instance variables, the SessionSynchronization interface is needed and
use the afterCompletion() callback to read the state variables back out of the
database.
• Note that, a stateless session bean cannot implement this interface, because, it has no
state, there’s no point in trying to synchronize its state with the database.
Enterprise Beans
• The home interface serves as the initial point of contact for a client. When the client
uses a naming service to lookup an EJB bean, the naming service returns a reference
to an object that implements the bean’s home interface.
• The home interface requires implementation of three methods namely,
getEJBMetaData(), remove(Handle handle) and remove(Object primarykey).
getEJBMetaData()
• Builder tools can use the information in the EJBMetaData class to generate wrapper
classes to invoke methods on the bean.
remove(Handle handle)
remove(Object primaryKey)
• This method removes an EJB object by its primary key. (Only an Entity bean can
have a primary key)
The EJBHome interface does not provide any methods by which a client can create new
beans or look up existing ones. Instead, the EJB bean developer must write the method
signatures for these tasks in their own interfaces.
getEJBHome()
• This method returns a reference to an object implementing the EJB object’s home
interface.
getHandle()
getPrimaryKey()
• This method returns the primary key used by the EJB object. Although it exists on
the interfaces for both session and entity beans, it really makes sense to use this
method only on an entity bean; session beans do not expose a primary key to the
outside world, and indeed need not have a primary key at all.
isIdentical()
• This method provides a way to test whether two EJB object references refer to an
identical object. It is mainly useful for Entity Beans.
Two Entity beans that are from the same home interface and have the same primary key are
considered identical.
The two references in this method do not necessarily point to the same object in memory, but
all of these instances are considered to have the same identity.
remove()
• This method allows the caller to remove the remote EJB object.
Note: To create a remote interface, simply create an interface that inherits from EJBObject and
add the method signatures of the methods. And there is no need to implement this remote
interface. Instead the container generates a proxy object that interacts with remote clients and
passes their method calls to the bean.
The EnterpriseBean Interface
• The top-level type in the EJB Class hierarchy is the EnterpriseBean interface.
• This merely serves as a common point of ancestry for the Session and Entity beans,
and requires that its subinterfaces be serializable. The transaction attribute logically
belong with the Enterprise Bean interface, even though they’re not associated
directly with it. Either a Session or an Entity Bean can be assigned any of the six
available transaction attributes.
EJB Containers
EJB Containers
• It is an environment in which EJB execute.
• Its primary role is to serve as a buffer between an EJB and the outside world
• The container is having high degree of flexibility in servicing client requests.
• The container provides following services:
o Support for transactions
o Support for management of multiple instances
o Support for persistence
o Support for security
• The container generally provides support for managing and monitoring EJBs as
well as for version management.
1) TX_NOT_SUPPORTED
2) TX_BEAN_MANAGED
3) TX_REQUIRED
4) TX_SUPPORTS
5) TX_REQUIRES_NEW
6) TX_MANDATORY
• TX_NOT_SUPPORTED means that the bean does not support any transactions, and
can’t be used from within a transaction. If the client attempts to use the bean from
within a transaction, the client’s transaction will remain suspended until the bean
completes its operation; the client’s transaction will then resume.
• In TX_BEAN_MANAGED, the bean manages its own transaction. This option is the
only type of transaction support in which the bean can directly manipulate the
transaction.
• For TX_REQUIRED mode, if the client has a transaction open when it invokes the
bean’s method, the bean runs within the client’s transaction context. Otherwise, the
container starts a new transaction.
• TX_SUPPORTS means that if the client has a transaction in progress when it invokes
the bean’s methods, the client’s transaction is used. Otherwise no new transaction is
created.
• In TX_REQUIRES_NEW, the bean always starts a new transaction (even if a
transaction is already in progress). If a transaction is underway, the container
temporarily suspends it until the bean’s transaction finishes.
• For TX_MANDATORY, the bean requires that the client have a transaction open
before the client attempts to use the bean. If no transaction is available, an error
occurs.
i) Instance Passivation
• This means that the container shares instances of EJB beans among multiple clients
• To increase the efficiency the container instantiate fewer copies of a given bean to
service requests. This not possible in all the cases.
• Specially, the container must provide one session bean per client otherwise
conversational state can’t be tracked by the containers.
• This may be used when an EJB needs to load some state information from a
database when it is created.
• EJB State information can be cached in some readily available place and this speeds
up the loading process.
v) Optimized method invocations
• These are employed to combat overhead costs associated with remote invocation
• Whether the client of an EJB is a remote client or another EJB in the same
container, the APIs used to gain access to it are always the same.
• A typical EJB interaction is as follows:
o Use a naming service to locate the EJB’s home interface
o Make a call on the EJB’s home interface to gain access to its remote interface.
o Make calls to the business methods against the remote interface.
• Contain short circuit the remote protocol, when both the beans live in the same
container.
• Support for persistent EJBs (Entity Beans) is not mandatory in the 1.x version of
the EJB specification, but it will become mandatory, but it will become mandatory
in the 2.0 version.
• Persistence means that the state of an object is saved to some sort of persistent
storage (typically a file or a database) allowing the object to be read in and used at
some later time, instead of being recreated each time.
• The shifting of responsibility for transaction management, concurrency, security
and persistence to the container means that the bean developer can concentrate on
the business rules of his/her application.
• The main mechanism used in EJB security is the access control list (ACL).
• An ACL is a list of persons or groups that are allowed to access particular pieces of
functionality.
• Only those users who are included in the ACL will be able to access the ACL
protected beans.
EJB Servers
The diagram presents a high-level view of the EJB architecture, showing the various
relationships between the various components.
This illustrates the Key Features of the EJB Architecture
EJB’s Role
--[if !supportLists]-->1. <!--[endif]-->EJB Specifies an execution environment
<!--[if !supportLists]-->• <!--[endif]-->The container is responsible for
providing services to EJB.
<!--[if !supportLists]-->• <!--[endif]-->An EJB is a Java class that implements
either the Session Bean or the Entity Bean Interface.
<!--[if !supportLists]-->• <!--[endif]-->The container provides services such as
support for transactions, support for persistence, and management of
multiple instances of a given bean.
<!--[if !supportLists]-->• <!--[endif]-->The container does not allow the bean to
be accessed directly from the client; rather, the container provides a proxy
object for each bean.
<!--[if !supportLists]-->• <!--[endif]-->This approach gives the container a high
degree of freedom in managing the beans and insulates the bean themselves
from the outside world.
--[if !supportLists]-->2. <!--[endif]-->EJB exists in the middle-tier
<!--[if !supportLists]-->• <!--[endif]-->EJB, like other middle-tier components
are used to encapsulate business rules.
<!--[if !supportLists]-->• <!--[endif]-->A typical EJB consists of methods that
encapsulate business logic.
<!--[if !supportLists]-->• <!--[endif]-->A remote client can invoke these
methods, which typically result in the updating of a database.
--[if !supportLists]-->3. <!--[endif]-->EJB supports transaction processing
<!--[if !supportLists]-->• <!--[endif]-->An EJB container must support
transactions
<!--[if !supportLists]-->• <!--[endif]-->When the bean is deployed, the container
reads this information and provides necessary support.
<!--[if !supportLists]-->• <!--[endif]-->Possible types of transaction support:
<!--[if !supportLists]--> i. <!--[endif]-->Requiring that the client
provide an open transaction context.
<!--[if !supportLists]--> ii. <!--[endif]-->Requiring that the container
start a new transaction when the bean is invoked.
<!--[if !supportLists]--> iii. <!--[endif]-->Requiring that the bean be
allowed to manage its own transaction.
--[if !supportLists]-->4. <!--[endif]-->EJB can maintain state
<!--[if !supportLists]-->• <!--[endif]-->CGI scripts are handy because they
work against a well-defined interface.
<!--[if !supportLists]-->• <!--[endif]-->A CGI application has no built-in
mechanism to store state information.
<!--[if !supportLists]-->• <!--[endif]-->Each invocation of a CGI application is
therefore a fresh start.
<!--[if !supportLists]-->• <!--[endif]-->If the developer would like to build an
application with several screens that handles complex input, CGI is not
suitable.
<!--[if !supportLists]-->• <!--[endif]-->EJB can maintain state across several
method invocations.
<!--[if !supportLists]-->• <!--[endif]-->The EJB container keeps track of any
state information that a bean needs.
<!--[if !supportLists]-->• <!--[endif]-->Stateless bean can also be created.