Java Beans + Ejb
Java Beans + Ejb
1.0 INTRODUCTION
Software has been evolving at a tremendous speed since its inception. It has gone through
various phases of development from low level language implementation to high level
language implementation to non procedural program models. The designers always make
efforts to make programming easy for users and near to the real world implementation.
Various programming models were made public like procedural programming, modular
programming and non procedural programming model. Apart from these models reusable
component programming model was introduced to put programmers at ease and to utilise
the reusability as its best. Reusable Component model specifications were adopted by
different vendors and they came with their own component model solutions.
1.1 OBJECTIVES
JavaBeans will look a plain Java class written with getters and setters methods. It's logical
to wonder: “What is the difference between a Java Bean and an instance of a normal Java
class?” What differentiates Beans from typical Java classes is introspection. Tools that
recognize predefined patterns in method signatures and class definitions can "look inside"
a Bean to determine its properties and behaviour.
A Bean’s state can be manipulated at the time it is being assembled as a part within a
larger application. The application assembly is referred to as design time in contrast to
run time. For this scheme to work, method signatures within Beans must follow a certain
pattern, for introspection tools to recognise how Beans can be manipulated, both at design
time, and run time.
In effect, Beans publish their attributes and behaviours through special method signature
patterns that are recognised by beans-aware application construction tools. However, you
need not have one of these construction tools in order to build or test your beans. Pattern
signatures are designed to be easily recognised by human readers as well as builder tools.
One of the first things you’ll learn when building beans is how to recognise and construct
methods that adhere to these patterns.
Not all useful software modules should be Beans. Beans are best suited to software
components intended to be visually manipulated within builder tools. Some functionality,
however, is still best provided through a programatic (textual) interface, rather than a
visual manipulation interface. For example, an SQL, or JDBC API would probably be
better suited to packaging through a class library, rather than a Bean.
• Introspection: Builder tools discover a Bean’s features (ie its properties, methods,
and events) by a process known as INTROSPECTION. Beans supports introspection
in two ways:
6
Introduction to
Java Beans twosub-types (namely getter methods and setters methods). Interface methods are often used to support
event handling.
Beans can also be used just like any other Java class, manually (i.e., by hand
programming), due to the basic Bean property, “Persistence”. Following are the two
ways:
• Simply instantiate the Bean class just like any other class.
• If you have a customised Bean (through some graphic tool) saved into a serialised
file (say mybean.ser file), then use the following to create an instance of the
Customised Bean class...
try {
MyBean mybean = (MyBean)
Beans.instantiate(null, "mybean");
} catch (Exception e) {
}
• Connecting Events: Beans, being primarily GUI components, generate and respond
to events. The bean generating the event is referred to as event source and the bean
listening for (and handling) the event is referred to as the event listener.
• Bean Properties: Bean properties can be categorised as follows...
1) Simple Property are basic, independent, individual prperties like width, height,
and colour.
3) Bound Property is a property that alerts other objects when its value changes.
7
EJB and XML
4) Constrained Property differs from Bound Property in that it notifies other objects of an impending
change. Constrained properties give the notified objects
the power to veto a property change.
Accessor Methods
1. Simple Property :
If, a bean has a property named foo of type fooType that can be read and written, it should
have the following accessor methods:
2. Indexed Property :
3. Bound Property :
Getter and setter methods for bound propery are as described above based on whether
it is simple or indexed. Bound properties require certain objects to be notified when
they change. The change notification is accomplished through the generation of a
PropertyChangeEvent (defined in java.beans). Objects that want to be notified of a
property change to a bound property must register as listeners. Accordingly, the bean
that's implementing the bound property supplies methods of the form:
The preceding listener registeration methods do not identify specific bound properties.
To register listeners for the PropertyChangeEvent of a specific property, the following
methods must be provided:
4. Constrained Property :
The previously discussed methods used with simple and indexed properties also apply
Introduction to
Java Beans to the constrained properties. In addition, the following event registeration methods provided:
The classes and packages in the java.beans package can be categorised into three types
(NOTE: following is not the complete list).
1) Design Support
Classes - Beans, PropertyEditorManager, PropertyEditorSupport
Interfaces - Visibility, VisibilityState, PropertyEditor, Customizer
2) Introspection Support.
Classes - Introspector, SimpleBeanInfo, BeanDescriptor, EventSetDescriptor,
FeatureDescriptor, IndexedPropertyDescriptor, MethodDescriptor,
ParameterDescriptor, PropertyDescriptor
Interfaces - BeanInfo
The primary goal of a EJB is WORA (Write Once Run Anywhere). Enterprise JavaBeans
takes a high-level approach to building distributed systems. It frees the application
developer and enables him/her to concentrate on programming only the business logic
while removing the need to write all the “plumbing” code that's required in any enterprise
application. For example, the enterprise developer no longer needs to write code that
handles transactional behaviour, security, connection pooling, networking or threading.
The architecture delegates this task to the server vendor.
9
EJB and XML
1.5 EJB ARCHITECTURE
The Enterprise JavaBeans spec defines a server component model and specifies, how to
create server-side, scalable, transactional, multiuser and secure enterprise-level
components. Most important, EJBs can be deployed on top of existing transaction
processing systems including traditional transaction processing monitors, Web, database
and application servers.
• EJB clients: EJB client applications utilise the Java Naming and Directory Interface
(JNDI) to look up references to home interfaces and use home and remote EJB
interfaces to utilise all EJB-based functionality.
• EJB home interfaces (and stubs): EJB home interfaces provide operations for
clients to create, remove, and find handles to EJB remote interface objects.
Underlying stubs marshal home interface requests and unmarshal home interface
responses for the client.
• EJB remote interfaces (and stubs): EJB remote interfaces provide business-specific
client interface methods defined for a particular EJB. Underlying stubs marshal
remote interface requests and unmarshal remote interface responses for the client.
• EJB implementations: EJB implementations are the actual EJB application
components implemented by developers to provide any application-specific business
method invocation, creation, removal, finding, activation, passivation, database
storage, and database loading logic.
• Container EJB implementations (skeletons and delegates): The container manages
the distributed communication skeletons used to marshal and unmarshal data sent to
and from the client. Containers may also store EJB implementation instances in a pool
and use delegates to perform any service-management operations related to a
particular EJB before calls are delegated to the EJB implementation instance.
Some of the advantages of pursuing an EJB solution are:
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface HelloObject extends EJBObject {
public String sayHello() throws RemoteException;
}
10
Introduction to
Java Beans
Remote Interface
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
Bean Implementation
import java.rmi.RemoteException;
import javax.ejb.*;
11
EJB and XML
Deployment Descriptor
<ejb-jar>
<description>HelloWorld deployment descriptor</description>
<display-name>HelloWorld</display-name>
<enterprise-beans>
<session>
<description> HelloWorld deployment descriptor
</description>
<display-name>HelloWorld</display-name>
<ejb-name>HelloWorld</ejb-name>
<home>HelloWorldHome</home>
<remote>HelloWorld</remote>
<ejb-class>HelloWorldBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HelloWorld</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
EJB Client
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.transaction.UserTransaction;
import javax.rmi.PortableRemoteObject;
(HelloWorldHome)PortableRemoteObject.narrow(objref,
HelloWorldHome.class);
HelloWorld myHelloWorld = home.create();
String message = myHelloWorld.sayHello();
System.out.println(message);
} catch (Exception e) {
System.err.println(" Erreur : " + e);
System.exit(2);
}
}
}
12
Introduction to
1.7 EJB TYPES Java Beans
EJBs are distinguished along three main functional roles. Within each primary role, the
EJBs are further distinguished according to subroles. By partitioning EJBs into roles, the
programmer can develop an EJB according to a more focused programming model than,
if, for instances such roles were not distinguished earlier. These roles also allow the EJB
container to determine the best management of a particular EJB based on its programming
model type.
• Session Bean
• Entity Beans
• Message-driven Beans
• Stateful Session Beans: A stateful session bean is a bean that is designed to service
business processes that span multiple method requests or transaction. To do this, the
stateful bean retains the state for an individual client. If, the stateful bean’s state is
changed during method invocation, then, that same state will be available to the same
client upon invocation.
ejbCreate
• Does not exist: In this state, the bean instance simply does not exist.
• Ready state: When EJB Server is first started, several bean instances are created and
placed in the Ready pool. More instances might be created by the container as and
when needed by the EJB container
13
EJB and XML
1.7.1.2 Life Cycle of a Stateful Session Bean The Figure 2 shows the
setSessionContext ejbRemove
ejbActivate
ejbCreate Passive
• Does not exist: In this state, the bean instance simply does not exist.
• Ready state: A bean instance in the ready state is tied to a particular client and
engaged in a conversation.
• Passive state: A bean instance in the passive state is passivated to conserve resources.
1.7.1.3 Required Methods in Session Bean
ejbPassivate():
This method is called for, just before the session bean is passivated and releases any
resource that bean might be holding.
ejbActivate():
This method is called just for, before the session bean is activated and acquires the
resources that it requires.
ejbRemove():
This method is called for, by the ejb container just before the session bean is removed
from the memory.
14
Introduction to
Java Beans 1.7.1.4 The use of a Session Bean In general, one should use a session bean if the following circumstances
hold:
• At any given time, only one client has access to the bean instance.
• The state of the bean is not persistent, existing only for a short period and therefore.
Stateful session beans are appropriate if, any of the following conditions are true:
• The bean’s state represents the interaction between the bean and a specific client.
• The bean needs to hold information about the client across method invocations.
• The bean mediates between the client and the other components of the application,
presenting a simplified view to the client.
• Behind the scenes, the bean manages the work flow of several enterprise beans.
To improve performance, one might choose a stateless session bean if, it has any of these
traits:
• The bean’s state has no data for a specific client.
• In a single method invocation, the bean performs a generic task for all clients. For
example, you might use a stateless session bean to send a promotional email to
several registered users.
1.7.2 Entity Bean
Entity EJBs represent persistent objects. Their lifetimes is not related to the duration of
interaction with clients. In nearly all cases, entity EJBs are synchronised with relational
databases. This is how persistence is achieved. Entity EJBs are always shared amongst
clients. A client cannot get an entity EJB to itself. Thus, entity EJBs are nearly always
used as a scheme for mapping relational databases into object-oriented applications.
An important feature of entity EJBs is that they have identity—that is, one can be
distinguished from another. This is implemented by assigning a primary key to each
instance of the EJB, where ‘primary key’ has the same meaning as it does for database
management. Primary keys that identify EJBs can be of any type, including programmer
defined classes.
There are two type of persistence that entity EJB supports. These persistence types are:
15
EJB and XML
1.7.2.1 Life Cycle of an Entity Bean
setEntityContext unsetEntityContext
ejbHome
Pooled
ejbCreate
ejbPostCreate ejbRemove
remove
ejbActivate
ejbPassivate
ejbLoad
findByPrimaryKe
y ejbFind
Business Methods Ready
ejbStore
• Does not exist: In this state, the bean instance simply does not exist.
• Pooled state: When the EJB server is first started, several bean instances are created
and placed in the pool. A bean instance in the pooled state is not tied to a particular
data, that is, it does not correspond to a record in a database table. Additional bean
instances can be added to the pool as needed, and a maximum number of instances
can be set.
• Ready state: A bean instance in the ready state is tied to a particular data, that is, it
represents an instance of an actual business object.
setEntityContext():
This method is called for, if a container wants to increase its pool size of bean instances,
then, it will instantiate a new entity bean instance. This method associates a bean with
context information. Once this method is called for, then, the bean can access the
information about its environment
16
Introduction to
Java Beans ejbFind(..): This method is also known as the Finder method. The Finder method locates one or more
existing entity bean data instances in underlying persistent store.
ejbHome(..):
The Home methods are special business methods because they are called from a bean in
the pool before the bean is associated with any specific data. The client calls for, home
methods from home interface or local home interface.
ejbCreate():
This method is responsible for creating a new database data and for initialising the bean.
ejbPostCreate():
There must be one ejbPostCreate() for each ejbCreate(). Each method must accept the
same parameters. The container calls for, ejbPostCreate() right after ejbCreate().
ejbActivate():
When a client calls for, a business method on a EJB object but no entity bean instance is
bound to EJB object, the container needs to take a bean from the pool and transition into a
ready state. This is called Activation. Upon activation the ejbActivate() method is called
for by the ejb container.
ejbLoad():
This method is called for, to load the database in the bean instance.
ejbStore():
This method is used for, to update the database with new values from the memory. This
method is also called for during ejbPassivate().
ejbPassivate():
This method is called for, by the EJB container when an entity bean is moved from the
ready state to the pool state.
ejbRemove():
This method is used to destroy the database data. It does not remove the object. The object
is moved to the pool state for reuse.
unsetEntityContext():
This method removes the bean from its environment. This is called for, just before
destroying the entity bean.
• The bean represents a business entity and not a procedure. For example,
BookInfoBean would be an entity bean, but BookInfoVerifierBean would be a session
bean.
• The bean’s state must be persistent. If the bean instance terminates or if the
Application Server is shut down, the bean's state still exists in persistent storage (a
database).
17
EJB and XML
1.7.3 Message Driven Bean
A message-driven bean acts as a consumer of asynchronous messages. It cannot be called
for, directly by clients, but is activated by the container when a message arrives. Clients
interact with these EJBs by sending messages to the queues or topics to which they are
listening. Although a message-driven EJB cannot be called for, directly by clients, it can
call other EJBs itself.
ejbRemove
Ready Pool
• Does not exist: In this state, the bean instance simply does not exist. Initially, the
bean exists in the; does not exist state.
• Pooled state: After invoking the ejbCreate() method, the MDB instance is in the
ready pool, waiting to consume incoming messages. Since, MDBs are stateless, all
instances of MDBs in the pool are identical; they're allocated to process a message
and then return to the pool.
This method is invoked for each message that is consumed by the bean. The container is
responsible for serialising messages to a single message driven bean.
ejbCreate():
When this method is invoked, the MDB is first created and then, added to the ‘to pool’.
ejbCreate():
When this method is invoked, the MDB is removed from the ‘to pool’.
setMessageDrivenContext(MessageDrivenContext):
This method is called for, as a part of the event transition that message driven bean goes
through, when it is being added to the pool. This is called for, just before the ejbCreate().
18