Unit Ii Ejb Architecture: Preface
Unit Ii Ejb Architecture: Preface
EJB ARCHITECTURE
CONTENTS
EJB Introduction
EJB Architecture
Overview of EJB software architecture
View of EJB
Conversation
Building and Deploying EJBs
Roles in EJB.
Preface
This unit introduces the core aspects of EJP concepts. It deals with EJB Architecture
and Views of EJB . This unit portrays the concepts of Building and Deploying EJBs. And
also this unit covers the roles in EJB.
Introduction
Enterprise JavaBeans (EJB) are reusable Java components that implement
business logic and enable you to develop component-based distributed business
applications. EJBs reside in an EJB container, which provides a standard set of services
such as persistence, security, transactions, and concurrency. Enterprise JavaBeans are the
standard for defining server-side components. WebLogic Server's implementation of the
Enterprise JavaBeans component architecture is based on Sun Microsystems EJB
specification.
Enterprise Java Bean:
EJB Vs JavaBeans
Java Beans
EJB component model logically extends the JavaBeans component model to support
server components.
EJB components cannot be manipulated by a visual Java IDE in the same way that
JavaBeans can, They are prepackaged pieces of application functionality that are
designed to run in an application server.
Characteristics of EJB:
1. Hides server-side system level issues from developers.
2. Defines a standard component architecture that enables you to build distributed
object-oriented business applications.
3. Facilitates creating enterprise-level applications by allowing easy integrating
with other EJB components as well as with other components, such as servlets,
Java Server Pages (JSP), and Java Beans.
4. Enables you to create components and applications that are reusable across
various J2EE-compliant servers.
The server-side components deployed on the application servers can be
implemented using various technologies, such as EJB and Microsoft’s COM/DCOM.
Enterprise JavaBeans are different from JavaBeans, another similarly named Sun
Microsystems component based technology.
Examples of real-life applications where you can use the EJB component architecture are:
• Online share trading application
• Online reservation application
• Online catalog application
• Online library application
The EJB specification defines the following four types of Enterprise JavaBeans:
Configuring EJBs
4. In the left pane of the Console, click EJB. A table is displayed in the right pane
of the Console showing all the deployed EJBs.
6. Locate the EAR, WAR or JAR file you would like to configure. You can also
configure an exploded application or component directory. Note that WebLogic
Server will deploy all components it finds in and below the specified directory.
7. Click [select] to the left of a directory or file to choose it and proceed to the
next step.
10. Click Configure and Deploy. The Console will display the Deploy panel, which
lists deployment status and deployment activities for the EJB.
EJB Architecture
• EJB Server
• EJB Container
• EJB Client
• Enterprise Java Beans
• Auxiliary Systems - JNDI, JTA, JavaMail
DEPT OF MCA / MC503/ MIDDLEWARE TECHNOLOGIES/ UNIT – 2
• The EJB specification that governs the use of enterprise beans is compatible with
other Java™ APIs and CORBA. It also provides for interoperability between
enterprise beans and non-Java applications.
Enterprise beans
The remote client view specification became available beginning with EJB 1.1.
The remote client view of an enterprise bean is location independent. A client
running in the same JVM as a bean instance uses the same API to access the bean
as a client running in a different JVM on the same or different machine.
• Remote interface: The remote interface specifies the remote business
methods that a client can call on an enterprise bean.
• Remote home interface: The remote home interface specifies the methods
used by remote clients for locating, creating, and removing instances of
enterprise bean classes.
The local client view specification is available in EJB 2.0 or later. Unlike the
remote client view, the local client view of a bean is location dependent. Local
client view access to an enterprise bean requires both the local client and the
enterprise bean that provides the local client view to be in the same JVM. The
local client view therefore does not provide the location transparency provided by
the remote client view. Local interfaces and local home interfaces provide support
for lightweight access from enterprise bean that are local clients. Session and
entity beans can be tightly couple with their clients, allowing access without the
overhead typically associated with remote method calls.
• Local interface: The local interface is a lightweight version of the remote
interface, but for local clients. It includes business logic methods that can
be called by a local client.
• Local home interface: The local home interface specifies the methods used
by local clients for locating, creating, and removing instances of enterprise
bean classes.
In the EJB 2.1 specification, the EJB architecture introduced the support for Web
services. A client for a session bean can be a Web service client. A Web service
client can make use of the Web service client view of a stateless session bean,
which has a corresponding service endpoint interface..
The service endpoint interface for a stateless session bean exposes the
functionality of the session bean as a Web service endpoint. The Web Service
Description Language (WSDL) document for a Web service describes the Web
service as a set of endpoints operating on messages. A WSDL document can
include the service endpoint interface of a stateless session bean as one of its
endpoints. An existing stateless session bean can be modified to include a Web
service client view, or a service endpoint interface can be mapped from an
existing WSDL to provide the correct interface.
A Web service client view is independent of location and can be accessed through
remote calls.
An EJB client JAR file is an optional JAR file that can contain the client
interfaces that a client program needs to use the client views of the enterprise
beans that are contained in the EJB JAR file. If you decide not to create an EJB
client JAR file for an EJB module, all of the client interface classes will be in the
EJB JAR file. By default, the workbench creates EJB client JAR projects for each
corresponding EJB project.
EJB container
each EJB container is provided by an EJB server and contains a set of enterprise
beans that run on the server.
Deployment descriptor
EJB server
Types of EJB
Version 1.1 of the EJB Specification [EJB1.1 4.3] defined two types of EJB: the session
EJB and the entity EJB. Version 2.0 introduces a third type, the message-driven EJB
[EJB2.0 4.3]. It is possible to envisage EJB applications where the EJBs don’t fit neatly
into any of these categories, but in practice one of these types can be adapted to suit most
jobs.
(i)Session EJBs
a stateful session EJB is created for a specific client and not used by any others. The use
of stateless EJBs offers efficiency advantages but, of course, it is not always possible to
use them.
Entity EJBs represent persistent objects: Their lifetimes are not related to the
duration of interaction with clients. In nearly all cases, entity EJBs are synchronized 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.
Whereas a client normally creates a session EJB and removes it after use, clients
normally look up an existing entity EJB. Creation of an entity EJB corresponds to adding
new data items to the application (e.g., adding rows to database tables).
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.
(iii)Message-driven EJBs
Message-driven EJBs are the only type that do not follow a strict request-response
interaction with clients.
• The EJB developer works within the EJB framework to take advantage of its
built-in support for features like transaction management and security; once
understood, little developer effort is required to make effective use of these
features. With suitable servers, EJB applications can even make use of features as
sophisticated as distributed transaction management with no coding whatsoever.
• EJB 2.0 introduces a very powerful persistence management scheme, which
supports not only persistent data, but persistent relationships between objects. It
takes a while to get used to, but once mastered, this revolutionizes the
development of persistent application objects. Again, very little coding is required
to implement such objects.
• The EJB framework presents the EJB with a managed operating environment. For
example, the framework ensures that an EJB instance is never executing in more
than one concurrent thread,3so the developer does not have to be concerned about
the effects of concurrency on data integrity. The container is also responsible for
memory and resource management, so EJB developers don't have to be concerned
with these issues.
• EJBs can make use of the full range of Java APIs and class libraries. It is
relatively straightforward to code an EJB that parses XML, sends email, and
communicates with directory and database servers via standard APIs like JDBC
and JNDI.
• EJBs isolate database implementation from the application's business logic. If we
use entity EJBs, for example, as models of the underlying data, then theuse of that
data is decoupled from its internal representation. This leads to increased ease of
maintenance and portability of code. We can also use EJBs as interfaces to legacy
systems, with similar advantages.
• EJBs are very accessible to Web components such as servlets and JSPs. There are
a number of products available that support these components in the same
application framework (typically called application servers). This makes it
straightforward to provide EJB applications with a Web-based user interface and,
perhaps, provide Internet access to the application.
o EJB specification mentions two ways in which a client can find a bean
Using the Java Naming and Directory Interface
Using the CORBA’s Common Object Service (COS)
o JNDI API provides a uniform interface to naming and directory services.
o A naming service is a service that associates a Symbolic Name with an
object
o Domain Name Service is a commonly used naming service that is used to
transfer symbolic machine names into numeric IP addresses.
o Previous model of DNS, needed to maintain a file containing a list of
symbolic host names along with the numeric IP addresses that they should
resolve to.
o Problems in this approach:
If the /etc/hosts/file developed errors, couldn’t resolve symbolic
names.
o When the client has finished with an EJB object, it can call the remove()
method on the home interface or on the object’s remote interface.
o Usually remove() is called on a stateful session bean when it is no longer
needed, so that the container can discard it and use the space for other
beans.
o remove() method on the stateless bean can be invoked, but generally has
no effect; the server simply returns the instance to a pool.
o remove() on entity beans, would remove the underlying object from
persistent storage.
(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.
After the Bean Provider has implemented an EJB's business logic and has produced an
initial deployment descriptor, the process for building and deploying that EJB in the
WLE environment includes the following steps:
• Step 1: Obtain the EJB JAR file from the bean provider.
• Step 2: Modify the deployment descriptor.
• Step 3: Create the WebLogic EJB extensions to the deployment descriptor DTD.
• Step 4: Produce the deployable EJB JAR file.
• Step 5: Configure the EJB application.
• Step 6: Specify the module initializer object in the WebLogic EJB extensions to
the deployment descriptor DTD.
When you build the EJB that has been produced by the Bean Provider, the end result is an
EJB JAR file. The WLE system allows you to build two kinds of EJB JAR files:
Standard EJB An EJB that has been built, but lacks the specific deployment
JAR file information on any specific system. You typically build a standard EJB
with the goal of being able to distribute that EJB to a variety of
deployment environments. If you are only creating a standard EJB JAR
file, you only need to perform steps 1 and 2 in this topic.
Deployable An EJB that has been built with deployment descriptor information
EJB JAR file specific to a particular deployment environment. The steps described in
this chapter for building a deployable EJB JAR file specifically create an
EJB that can be deployed on a WLE system, and require you to perform
Step 1: Obtain the EJB JAR file from the bean provider.
The first step in deploying an EJB is to obtain the EJB JAR file from the Bean Provider.
In addition to the class files contained in the EJB JAR file, the EJB JAR file also has a
deployment descriptor for each bean in that file. The steps for producing the Bean
Provider's EJB JAR file are described in the section Development Steps.
Because multiple EJBs can be joined together in a single, deployable unit, part of the
assembly process is joining the EJB JAR files for each of the beans.
The Bean Provider specifies some initial deployment information in the deployment
descriptor. The deployer typically needs to add to or modify that information, such as
shown in the following table:
The EJB's name You may change the enterprise bean's name defined in the ejb-name
element.
Values of You may change existing values or define new values for the
environment environment properties.
entries
Description fields You may change existing or create new description elements.
Security roles You may define one or more security roles. The security roles
define the recommended security roles for the clients of the
enterprise beans. You define the security roles using the security-
role elements. For more information about EJB security, see Using
Security in the WebLogic Enterprise online documentation.
Method You may define method permissions, which are binary relationships
permissions between the security roles and the methods of the remote and home
interfaces of the EJBs. You define method permissions using the
method-permission elements.
Linking of If you define security roles in the deployment descriptor, you must
security role link the security role references declared by the Bean Provider to
references the security roles. You define these links using the role-link
element. For more information about EJB security, see Using
Security in the WebLogic Enterprise online documentation.
Changing The deployer can change the type of persistent storage used by a
persistent storage bean. If the persistentStoreType is file , the serialized files are
information, if created in this directory. The default file is / pstore/ bean_name .dat
necessary , where the directory pstore represents the directory from which the
WLE application was started, and bean_name is the fully qualified
name of the EJB with underscores (_ ) replacing the periods (. ) in
the name.
To modify a deployment descriptor, you can use either of the following two methods:
The type of deployment descriptor you produce depends on whether you are creating a
standard EJB JAR file or a deployable EJB JAR file.
If you are creating a standard EJB JAR file, you only need to modify the single
deployment descriptor contained in the EJB JAR file produced by the Bean Provider.
When modifying this deployment descriptor, use the syntax described in the EJB 1.1
Specification produced by Sun Microsystems, Inc.
For more information about the ejbc command, see the Command Reference in the
WebLogic Enterprise online documentation.
If you are creating a deployable EJB JAR file, you need to do the following:
Step 3: Create the WebLogic EJB extensions to the deployment descriptor DTD.
For an EJB application to be deployable in the WLE environment, you need to create a
file containing the WebLogic EJB extensions to the deployment descriptor DTD. This
file specifies the following run time and configuration information for the EJB
application:
The file that includes the WebLogic EJB extensions to the deployment descriptor DTD
must specify the following DTD reference at the beginning:
Server application startup and shutdown can be handled automatically by the EJB
container. However, if you have special server initialization or shutdown requirements,
use the module-initializer-class-name element in the WebLogic EJB extensions to the
deployment descriptor DTD to specify the name of the module initializer object.
The syntax for specifying a module initializer object for handling the initialize and
release methods is similar to that for CORBA and RMI. However, with the CORBA and
RMI models, the startup and shutdown information is specified in an XML file and is
serialized by the buildjavaserver command. For EJB deployment, XML elements for
startup and shutdown procedures are specified together with the other elements in the
WebLogic EJB extensions to the deployment descriptor DTD, and you process them
using the ejbc command. The WLE EJB container parses the XML at run time and
performs the startup and shutdown processing. See the section Step 5: Configure the EJB
application. for a complete description of startup and shutdown handling in WLE.
Example
<weblogic-ejb-extensions>
. . .
<module-initializer-class>
<moduleinitializer-class-name>ServerImpl
</moduleinitializer-class-name>
</module-initializer-class>
</weblogic-ejb-extensions>
A name for the EJB home class must be registered in the global WLE JNDI namespace.
This allows Java clients to perform a lookup on the JNDI name for the EJB home and
gain access to the object. The name for the EJB home class can be different than the <ejb-
name> element specified in the standard EJB XML. The <ejb-name> in the standard
deployment descriptor must be unique only among the names of the EJBs in the same
EJB JAR file. However, the JNDI name must be unique among all global home or factory
names in a WLE domain; this includes EJB homes, CORBA factories, and RMI named
objects.
Example
<weblogic-ejb-extensions>
<weblogic-version>
WebLogic Enterprise Server 5.0
</weblogic-version>
<weblogic-enterprise-bean>
<ejb-name>
oracle
</ejb-name>
<weblogic-deployment-params>
<max-beans-in-free-pool>
20
</max-beans-in-free-pool>
<max-beans-in-cache>
1000
</max-beans-in-cache>
<idle-timeout-seconds>
5
</idle-timeout-seconds>
<!-- JNDI name that is associated with this EJB;
used for lookup -->
<jndi-name>
oracle.OracleHome
</jndi-name>
.
.
.
</weblogic-ejb-extensions>
The WLE EJB container provides container-managed persistence. The code for
implementing the persistence is generated by the ejbc command based on the deployment
descriptors. The persistence store can be a flat file or it can be a database managed with a
JDBC connection pool. For the EJB state to fully cooperate in a WLE global transaction,
configure the EJB to use the JDBC-managed database store provided in WLE. Use file-
based persistence only during development and prototyping.
The standard deployment descriptor created by the Bean Provider normally specifies:
• The fields in the EJB that are to be persistent, via the cmp-field element
• Information about the primary key
However, you, as the deployer, need to specify additional information for mapping an
EJB to its persistent store via the WebLogic EJB extensions to the deployment descriptor
DTD.
File-based Persistence
The following code shows the WebLogic EJB extensions to the deployment descriptor
DTD for specifying file-based persistence:
<!--
Persistence store descriptor. Specifies what type of persistence store
EJB container should use to store state of bean.
-->
<!ELEMENT persistence-store-descriptor (description?,
(persistence-store-file |
persistence-store-jdbc)?)>
<!--
Persistence store using file. Bean is serialized to a file.
Mainly used to store state of Stateful Session Beans.
-->
<!ELEMENT persistence-store-file (description?,
persistence-store-directory-root
?)>
<!--
Root directory on File system for storing files per bean.
-->
<!ELEMENT persistence-store-directory-root (#PCDATA)>
The information supplied for the persistence-store-directory-root element is used by the
EJB container to store all instances of the EJB, with the ejb-name element converted to a
directory name.
Database-stored Persistence
The following code shows the WebLogic EJB extensions to the deployment descriptor
DTD for specifying a JDBC connection for database-stored persistence:
<!--
Persistence store is any RDBMS. JDBC driver is used to talk to database.
Finder descriptors are the WLE implementation of the EJB find methods. The finder-
descriptor elements are pairs of method signatures and expressions. You specify a
method signature in the EJBHome interface, and you specify the method's expression in
the deployment descriptor via the query-grammar element. The finder methods return an
enumeration of EJBs. For more information about specifying finder descriptors, see the
EJB XML Reference in the WebLogic Enterprise online documentation.
Example
The following WebLogic EJB extensions to the deployment descriptor DTD fragment
specify automatic saving using JDBC persistence of two fields of an entity bean
(accountId and salary ) to a database table (emp ) using a connection pool (pool1 ),
shown in boldface type:
<weblogic-ejb-extensions>
<weblogic-version>
WebLogic Enterprise Server 5.0
</weblogic-version>
<weblogic-enterprise-bean>
<ejb-name>
oracle
</ejb-name>
<weblogic-deployment-params>
<max-beans-in-free-pool>
20
</max-beans-in-free-pool>
<max-beans-in-cache>
1000
</max-beans-in-cache>
<idle-timeout-seconds>
5
</idle-timeout-seconds>
<!-- JNDI name that is associated with this EJB;used for lookup -->
<jndi-name>
oracle.OracleHome
</jndi-name>
<persistence-store-jdbc>
<!-- Pool name is looked up by the EJB source -->
<pool-name>
jdbc/pool1
</pool-name>
<!-- *** DATABASE INFORMATION SPECIFIC TO
INSTALLATION SITE *** -->
<!-- Default user URL is for Oracle 8i-->
<user>
scott
</user>
</bean-field-name>
<table-column-name>
empno
</table-column-name>
</attribute-map-entry>
<attribute-map-entry>
<bean-field-name>
salary
</bean-field-name>
<table-column-name>
sal
</table-column-name>
</attribute-map-entry>
</attribute-map>
</weblogic-deployment-params>
</weblogic-enterprise-bean>
</weblogic-ejb-extensions>
For more information about JDBC connections, see Using the JDBC Drivers in the
WebLogic Enterprise online documentation.
If you have multiple bean packages meant to be assembled as a deployable unit, the bean
packages must be specified in a single deployment descriptor. For more information
about the ejbc command, see the Command Reference in the WebLogic Enterprise online
documentation.
Step 6: Specify the module initializer object in the WebLogic EJB extensions to the
deployment descriptor DTD.
Server application startup and shutdown can be handled automatically by the EJB
container. However, if you have special server initialization or shutdown requirements,
you need to implement an application entity called the module initializer object.
The module initializer object implements operations that execute the following tasks:
• Performing basic module initialization (or EJB JAR file deployment) operations,
which may include allocating resources needed by the EJB JAR file.
• Performing basic server application initialization operations, which may include
registering homes or factories managed by the server application and allocating
resources needed by the server application.
• Performing server process shutdown and cleanup procedures when the server
application has finished servicing requests.
Note: For EJBs, the scope of the module initializer object is at the EJB JAR file
level and not of the entire server application, as with the Server object and WLE
CORBA applications.
EJB Roles
An interesting feature of the EJB Specification is that its requirements are largely
divided into different 'roles.' When reading the Specification, it is therefore quite clear to
whom the various requirements correspond.
CHETTINAD COLLEGE OF ENGG AND TECHNOLOGY 33
DEPT OF MCA / MC503/ MIDDLEWARE TECHNOLOGIES/ UNIT – 2
Component provider
The Specification indicates how the component provider uses the XML deployment
descriptor to indicate that these EJBs and resources are required, and the names by which
it has referenced them.
Application assembler
references to external resources, and packages the components into a single file ready for
deployment.
It is slightly confusing for the newcomer to EJB development that most of the
graphical tools used for building EJB applications don't distinguish between what they
are doing in the provider role versus in the assembler role. It often appears that the
developer is supplying redundant information or information that is duplicated; this is
because the deployment descriptor being built does support the notion of a separate
provider and assembler, even if they are in practice the same person.
Deployer
The deployer [EJB2.0 25.3] takes a file from the application assembler that
contains the EJBs and other components required for the application and installs it on the
server, making appropriate customizations for the site.
Server vendor
The server vendor supplies EJB server products. An EJB developer should not
assume that the sections in the EJB Specification that deal with the responsibilities of the
server vendor are not of interest. On the contrary: The server sets the operating
environment of the EJB, and this section of the EJB Specification gives the developer an
insight into what environment the EJB can expect.
Administrator
Tool provider
TX_MANDATORY
9. What are the various tasks performed by server?
a) Instance passivation
b) Instance pooling
c) Database connection pooling
d) Pre-cached instances
e) Optimized method invocations
22. What are the various APIs provided by the EJB container?
Various APIs provided by the EJB container are:
Java Platform, Standard Edition, (J2SE) APIs
EJB Standard Extension
JDBC Standard Extension
JNDI Standard Extension
JMS Standard Extension
JavaMail Standard Extension (for sending mail only)
JAXP
JTA Standard Extension (Only User Transaction interface)
27. Draw diagram for various EJB roles and their interaction.