01 Models EJB
01 Models EJB
Contents:
z Overview Enterprise Java Beans
z Using EJBs as Web Dynpro Model
Web Browser
Applet
Java Client
SOAP
Client
Web Container
COM Client
EJB Container
CORBA Client
J2EE Server
Components
J2EE applications provide their functions in various components. These are always dependent on a
specific runtime environment (container) when they are executed.
J2EE specifies the following main types of components:
y Client components
This is usually an application that is outside of the J2EE server, which accesses components that are
also external to the J2EE server. Client applications are usually combined in jar-archives.
y Web components
This includes HTML pages, servlets, JSPs and pictures (.gif or.jpeg files), which are combined in a war-
archive.
y EJB components
These are one or more Enterprise Beans, which are combined in a jar-archive.
What Makes Up An EJB Component ?
Application Deployer
Assembler
Archive (.ear)
Deployment
Bean developer
tools
Archive (.jar)
Home-
/Component
Interfaces
You package the files in the preceding list into an EJB JAR file, the module that stores the enterprise bean.
An EJB JAR file is portable and may be used for different applications. To assemble a J2EE application,
you package one or more modules—such as EJB JAR files—into an EAR file, the archive file that holds
the application. When you deploy the EAR file that contains the bean’s EJB JAR file, you also deploy the
enterprise bean onto the J2EE server.
Enterprise Java Beans Types
EnterpriseJava
Enterprise JavaBean
Bean
SessionBean
Session Bean EntityBean
Entity Bean MessageDriven
Message DrivenBean
Bean
Synchronous Asynchronous
communication communication
Stateless
Stateless Stateful
Stateful
Session Bean
Session Bean Session Bean
Session Bean
ContainerManaged
Container Managed Bean
BeanManaged
Managed
Persistence
Persistence Persistence
Persistence
EJB types
3 different versions of the Enterprise Bean are available:
Session Beans Usually implement transactions or process flows, which are executed
as services that are performed for clients.
Entity Beans Represent business objects. These are associated persistent objects that
contain data from a database.
Message Driven Beans Also implement transactions or process flows, but are triggered by
receiving a message.
Session Beans
Persistence: Transient.
Lifetime defined by the Client Process
(Session duration).
SessionBean
Session Bean
Stateless
Stateless Stateful
Stateful
Session Bean
Session Bean Session Bean
Session Bean
Session Beans
Session Beans implement specific functions, which you make available to clients, such as purchase order
entry, bank transfers etc. A session bean can in turn access other Enterprise Beans or services for the EJB
container.
Session objects are always short-lived. A Session Bean usually only exists for as long as the session lasts.
This means that there is a connection between the client and the EJB container.
Stateful Session Bean
The respective session object must be able to trace the client state across the various steps involved for
business processes that are divided between several dialog steps (as a conversation). These types of
business process, which can cover several method calls or transactions, have been implemented using
Stateful Session Beans.
Stateless Session Beans
This type of bean is used for business processes, which are initiated from the client via a method call. The
state of the client is only valid during the method call.
Entity Beans
Persistence: Persistent
State remains in the persistent memory
(such as the DB) even after the container
is terminated.
EntityBean
Entity Bean
BeanManaged
Bean Managed ContainerManaged
Container Managed
Persistence
Persistence Persistence
Persistence
Entity Beans
Entity Beans in their most simple form, correspond conceptionally to a table line in a relational database.
They represent persistent objects, meaning that they encapsulate data from a persistent memory, usually a
database. In doing so, they represent an interface between the business logic and the database and give
clients transaction-secure access to data.
Entity objects are persistent (long-lived), since their state is saved to the database.
An additional feature of entity objects is that several clients can access them in parallel. The EJB container
is responsible for capturing problems that can occur during competitive access, for instance by using
synchronization.
EJB, Big Picture
Session Bean
Is thought to represent business
process EJB Client
Provides state management
Entity Bean
Is thought to represent business object EJB Container
Memory representation of persistent CreateOrder
object Session Bean
Order
Bean Managed Persistence Entity Bean
Persistence: Transient.
Lost during
container
termination.
ejb-jar.xml
1 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ejb-jar PUBLIC "-//Sun
Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">
2 <ejb-jar>
3 <description>My first EJB</description>
4 <display-name>HelloWorld</display-name>
5 <enterprise-beans>
6 <session>
7 <ejb-name>HelloWorldBean</ejb-name>
8 <home>com.sap.training.HelloWorldHome</home>
9 <remote>com.sap.training.HelloWorld</remote>
10 <local-home>com.sap.training.HelloWorldLocalHome</local-home>
11 <local>com.sap.training.HelloWorldLocal</local>
12 <ejb-class>com.sap.training.HelloWorldBean</ejb-class>
13 <session-type>Stateless</session-type>
14 <transaction-type>Container</transaction-type>
15 </session>
16 </enterprise-beans>
17 </ejb-jar>
These are automatically generated when the projects are created and
when they are modified.
ejb-j2se-engine.xml
persistent.xml
ejb-jar.xml
web-j2se-engine.xml
web.xml
application.xml
Describes the standard J2EE properties of the complete application and the referenced modules. In
particular, you must specify the URL where the J2EE application is stored on the J2EE Engine.
application-j2ee-engine.xml (SAP specific)
Contains additional entries specific to the SAP J2EE Engine. You do not need to make any entries for the
car rental application in this deployment descriptor.
ejb-j2ee-engine.xml (SAP specific)
Contains entries specific to the J2EE Engine.
persistent.xml (SAP specific)
Describes the mapping of entity beans and their CMP fields to the corresponding database tables and table
fields.
ejb-jar.xml
Describes the standard J2EE properties of the Enterprise JavaBeans.
web-j2ee-engine.xml (SAP specific)
Contains entries concerning Web resources specific to the SAP J2EE Engine.
web.xml
Describes the standard J2EE properties of the Web resources (JSPs, servlets, and so on). These properties
include mapping information, security entries (access restrictions and security roles), and entries
concerning EJB reference names.
The Deployment Process
Application Deployer
Assembler
Archive (.ear)
Archive (.ear) Deployment
Bean developer
tools
EJB Verifier
Archive (.jar) Container Compiler
Remote Compiler
Lifecycle Methods
Generated
Delegates Callback Methods
HOME-/
Lifecycle Interface
COMPONENT-/
Business Interface
Client Bean Instance
Enterprise JavaBean
HelloWorldBean
1 import javax.ejb.SessionBean;
2 import javax.ejb.SessionContext;
3 import javax.ejb.CreateException;
At Runtime, Instances of the Enterprise JavaBean class is fully managed by the container and it can only
indirectly used by its clients
Naming Conventions
Because enterprise beans are composed of multiple parts, it’s useful to follow a naming convention for
your applications.
Local Home vs. Remote Home Interface
There are some issues to keep in mind when using local interfaces:
The beans must run in the same VM -- they are, after all, local.
Parameters running under a local interface are sent by reference rather than being
copied, as is the case for remote objects.
Unexpected side effects can result if you ignore this distinction and do not code
accordingly.
Typically, you'll decide whether to use local or remote access based on:
Unless the client is always expected to run in another VM choose remote access.
Whether the beans are tightly or loosely coupled. If beans depend on each other and
interact frequently, you should consider local access.
Scalability. Remote access is inherently scalable and should be used if scalability is an
important factor.
With the advent of local interfaces in the EJB 2.0 specification, it is recommended that entity beans should
almost always be based on local access. When using local interfaces, most performance issues regarding
very fine-grained data access go away. If the client is remote, the standard design pattern has the client use
a remote interface to access a session bean, which then acts as a liaison to the entity bean. The session
bean communicates with the entity bean through a local interface (Session Façade Pattern).
The (Remote) Client’s View, Create JNDI Lookup
JNDI
API
1
2 Naming
Naming
Service
Bean Instance
(Stateless Session Bean)
Client “HelloWorldBean”
EJB Container
J2EE Server
HelloWorldClient
1 Properties jndiCtxProp = new java.util.Properties();
2 jndiCtxProp.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitCtxFactory);
4 . . .
5 try {
6 Context jndiCtx = new InitialContext(jndiCtxProp);
7 Object obj =
8 (Object)jndiCtx.lookup(“com.sap.training.HelloWorldBean”);
2
JNDI
API
Naming
Naming
Service
4
Client
Remote Component Interface
“HelloWorld”
EJB Container
J2EE Server
HelloWorldClient
. . .
9 HelloWorldHome beanHome =
(HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(
obj,HelloWorldHome.class);
10 HelloWorld myHelloWorld = beanHome.create(); 3
. . .
JNDI
API
Naming
Naming
Service
Bean Instance
6 (Stateless Session Bean)
Client “HelloWorldBean”
5
EJB Container
J2EE Server
HelloWorldClient
. . .
11 System.out.println(">> "+myHelloWorld.sayHello("Roland") );
. . . 5
JNDI
API
Naming
Naming
Service
8
Client
EJB Container
J2EE Server
HelloWorldClient
. . .
12 beanHome.remove(); 7
. . .
SAP
Web Dynpro
J2EE Web Dynpro XMI Java Objects Open
Runtime DB
Server Application Model (EJB etc.) SQL
for Java
Note: This feature not shown in the course. If you want to learn more about it, please refer to
the documentation.
In the course material an alternative is shown: Binding an existing EJB application to Web Dynpro using
JNDI.
Step 1: Deploy Dictionary Tables and EJBs
MyDicProject
Deployment
.sda
EJB Container
Step 1: Prerequisits
If you want to use Enterprise Java Beans as model for Web Dynpro, you can use the tools of SAP
NetWeaver Developer Studio to define Enterprise Java Beans and platform independent database objects.
After development you have to put the objects in archives and deploy them to the Web Application Server.
Step 2: Customize Web Dynpro Project