EJB Basics - by Example: Dave Landers BEA Systems, Inc
EJB Basics - by Example: Dave Landers BEA Systems, Inc
EJB Basics - by Example: Dave Landers BEA Systems, Inc
Dave Landers
BEA Systems, Inc.
[email protected]
[email protected]
Agenda
EJB Overview
Parts of an EJB
Component Interface
Home Interface
Implementation
Deployment Descriptor
Writing simple EJBs
Limitations
Etc.
What is an EJB?
Enterprise Java Beans
Not JavaBeans
Architecture for server-side components
Lots of services provided for you by Container
Transactions, security, etc.
Most are declarative – no coding
You still have to think about and understand these services
You get a lot for a little work
Where is an EJB
JSP
Web Server
Server
EJB EJB
Database
EJB Features
Transactions
Security
Database
Distributed Components
RMI, CORBA
Container Managed Persistence
Performance and Scalability
Pooling, Load Balancing, etc.
Descriptor-Based Features
Deployment of new code to running server
Kinds of EJB
Session Beans
Entity Beans
Message Driven Beans
Return Types
Session ejbCreate() returns void
Entity ejbCreate() returns Primary Key type
BMP returns PK instance, CMP returns null
Entity ejbFindXXX() returns PK or Collection of PK
Entity Beans
– Container Managed Persistence
Abstract implementation class
Each “attribute” has abstract get/set methods
Implemented by the container
Mapping described in deployment descriptor
Bind finders to database using EJB-QL
Kind of like SQL
In deployment descriptor
Select Methods allow EJB-QL to be used from
home or business methods
Can also set up relationships with other Entity
Beans
Code Break
Simple Entity Bean
Property
Persists Key and Value pairs in Database
Container Managed Persistence
Entity Beans
– Bean Managed Persistence
You write the database code
JDBC and SQL in
ejbCreate, ejbRemove
ejbLoad, ejbStore
Finders, select methods
Maintain state of EJBObject vs. Database
Good if:
Binding is too complex for CMP / EJB-QL
Persistence is not to database
You are a control freak or have extra time....
Code Break
Property
Bean Managed Persistence
Extends CMP class and provides just persistence
methods
EJB Lifecycle
For All EJB Types: Stateless Session
Lifecycle of Instances
is manged by the Does Not Exist
Container
EJB gets callbacks at newInstance
setSessionContext() ejbRemove()
appropriate times
ejbCreate()
More State ==
More Complicated Method Ready Pool
method
Stateful Session Lifecycle
Does Not Exist System Exception
from any method
newInstance
setSessionContext() ejbRemove() timeout
ejbCreate()
ejbPassivate()
(LRU victim) Passive
Method Ready
ejbActivate()
(method)
method
Entity Lifecycle
ejbFindX() ejbSelectX()
newInstance
setEntityContext() ejbHomeX()
Pooled
unsetEntityContext()
Does Not Exist ejbRemove()
ejbActivate()
ejbCreate()
ejbPostCreate() ejbPassivate()
System Exception
from any method
method ejbSelectX
About Exceptions
Throw any exception that makes sense
Application Exceptions
These are non-RuntimeExceptions
In your throws clause
You must handle things like rollback
System Exceptions
RuntimeException, Error (and subclasses)
Container must
Log it
Rollback transaction
Destroy bean instance
Deployment Descriptor
In EJB jar file: META-INF/ejb-jar.xml
Declares each EJB and
References to other EJBs (ejb-ref)
Environment Properties (env-entry)
Database and other resources (resource-ref)
Security restrictions
Transaction settings
CMP Definitions
Fields and Queries
Vendor specific Descriptor
Server specific configurations, tunings, etc.
Code Break
Deployment Descriptor for other examples
Client view of EJB
Lookup Home with JNDI
Create or find bean
Make method calls
Client EJB calls
Context jndiCtx = new InitialContext();
Might need JNDI properties for server connection
ejbObject.getPrimaryKey()
Returns Entity PK
Code Break
Client code for examples
JSP
Message Driven Beans
No client interfaces (component or home)
Implement
MessageDrivenBean
javax.jms.MessageListener
One method:
onMessage(javax.jms.Message m)
Tied to JMS Destination when deployed
EJB 2.1 will let MDB receive non-JMS messages
EJB Limitations
No Read/write static fields
Might not be accessible by all EJBs
Container might spread across multiple JVMs
No Thread synchronization or thread management
Might not work like you think
Container spread across multiple JVMs
Mess up the container pooling, load balancing, etc.
No File I/O
Database is better
No Server sockets or multicast
No ClassLoader games or Native Library Loading
EJB Limitations
Usually OK for EJB to use objects that do these
things
But:
Read the rationale in the EJB spec
Chapter 24.1.2 – Programming Restrictions
Make sure you are aware of the reasons for the restric-
tions
Food for Thought:
What is a Singleton?
Consider distributed system, multiple ClassLoaders, multiple JVMs,
multiple computers....
EJB Limitations
Never give away “this”
Object reference to implementation
It “belongs” to the container
Get interfaces from SessionContext or EntityContext
and pass these around instead
context.getEJBObject()
context.getEJBLocalObject()
Design hints
Use locals where possible
Intra-server calls
Hide some things from Remote clients
Only deployed code has access
Use “queries” rather than “bulk” finders
If finder would return a lot
Finder returns EJBObject, might swamp container pool
Query as Home method returning PKs
Can use as needed
Or set up finder to return “reasonable sized” sets
Like cursor
Design hints
Entity Value Object
Serializable object representing state of Entity
Client can work with this rather than lots of remote
calls (individual get/set methods)
Entity has getValue / setValue
Consider where state is kept
Client (memory or HttpSession) vs. Stateful Session
Session Bean methods to access groups of EJBs
Rather than all on the client
Allows control of logic, transactions, security, etc.
What’s New – EJB 2.1
Expanded EJB-QL
Adds useful things SQL users are used to like ORDER
BY, MIN, MAX, SUM, COUNT, etc.
Timer Service
EJBs can get timed callbacks from container
Web Services
Stateless Session Beans as Web Services endpoints
JAX-RPC
Summary
Component Interface
Contract for the Component
Local and/or Remote
Home Interface
Factory
Create, Find, etc.
Implementation
Container Required Stuff
Your Code for Component and Home
Deployment Descriptor
Book Recommendations
Enterprise JavaBeans
Richard Monson-Haefel
Mastering Enterprise JavaBeans
Ed Rowman, et. al.
www.theserverside.com
Practical Java Programming Language Guide
Peter Haggar
Effective Java Programming Language Guide
Joshua Bloch
Mr. Bunny's Big Cup o' Java
Carlton Egremont III
Web References
EJB Spec
http://java.sun.com/products/ejb/docs.html
Go ahead, it’s only 572 pages
J2EE API Docs
http://java.sun.com/j2ee/sdk_1.3/techdocs/api
The Server Side
http://www.theserverside.com
News, Patterns, Discussion, Downloads, etc.
The End – Thank You
Please fill out evaluations
Example Code
On the conference CDROM
http://www.avitek.com/landers
[email protected]
[email protected]
More Implementation...
Container Callbacks – called on state changes
setSessionContext, setEntityContext, unsetEntityContext
ejbActivate, ejbPassivate
ejbCreate
Matches create(...) from Home
BMP Entity – SQL INSERT, return Primary Key
ejbPostCreate – Entity Beans
Called after EJB created in Container – has identity
Do things that need EJB reference - establish relationships
ejbRemove
BMP Entity – SQL DELETE
ejbLoad, ejbStore
Entity Beans only – BMP SQL SELECT & UPDATE
Basic Knowledge about EJB Page 1 of 6
1. What is EJB?
EJB stands for Enterprise JavaBeans. EJB is a distributed object-oriented server component that makes
obsolete low-level APIs such as for component life-cycle management, transaction management, and
database processes, and enables the creation of server-side program components by simply writing
business logic processes. With Apdesigner, you can use Java to create server-side components (i.e.,
EJB) and client applications that use EJB. The EJB runtime environment absorbs transaction process
differences that might exist between the TP monitor and DBMS. It is possible to create non-platform-
dependent server program components.
Overview of EJB
To Top of Page
http://kr.fujitsu.com/products/solutions/ebiz_platform/was/technical/tutorial/ejb/kiso/kiso... 10/10/2010
Basic Knowledge about EJB Page 2 of 6
The following enterprise beans conforming to EJB1.1 or EJB2.0 contracts can be created with
Apdesigner.
Enterprise JavaBeans
Stateless Session Bean
Stateful Session Bean
Bean-managed persistence Entity Bean
Container-managed persistence Entity Bean(*1)
Point-to-Point model Message-driven Bean
Publish/Subscribe model Message-driven Bean
To Top of Page
1. To create a project in Apdesigner, select [Enterprise JavaBeans Project], and enter the required
information in the Enterprise Bean Wizard that creates enterprise bean model source and
deployment descriptor files.
2. When you create a model, the following files are created:
Type Generated File
enterprise bean source (java)
home interface source (java)
Session Bean
remote interface source (java)
deployment descriptor file (ejb-jar.xml)
enterprise bean source (java)
home interface source (java)
remote interface source (java)
Entity Bean
primary key class source (java) (*1)
record class source (java) (*1)
deployment descriptor file (ejb-jar.xml)
enterprise bean source (java)
Message-driven Bean
deployment descriptor file (ejb-jar.xml)
*1 These files are not always created depending on the options specified in the wizard.
3. Implement the Business method in the main source of the generated file.
4. Build the program using the Apdesigner and deploy it using the J2EE Deployment Tool.
To Top of Page
http://kr.fujitsu.com/products/solutions/ebiz_platform/was/technical/tutorial/ejb/kiso/kiso... 10/10/2010
Basic Knowledge about EJB Page 3 of 6
PATH: Interstage-installation-folder\EJBCL\jdbc\bin
CLASSPATH: Interstage-installation-folder\EJBCL\jdbc\lib\fjisjdbc2.jar
For the environment variable settings of the machine on which Interstage Application Server is
installed, define the following settings:
PATH: Interstage-application-server-installation-folder\EJB\jdbc\bin
CLASSPATH: Interstage-application-server-installation-folder\EJB\jdbc\lib\fjisjdbc2.jar
3. Using a user-defined class included in another jar file from a enterprise bean
In order for an enterprise bean to use a user-defined class included in another jar file, it must be
possible to refer to the class during deployment of the enterprise bean. With the J2EE Deployment
Tool, you can specify CLASSPATH for referring to it.
4. Environment settings for developing EJB
http://kr.fujitsu.com/products/solutions/ebiz_platform/was/technical/tutorial/ejb/kiso/kiso... 10/10/2010
Basic Knowledge about EJB Page 4 of 6
Interstage Application Server is required for developing, deploying, and debugging EJBs.
<<Installation>>
For development in a client/server environment, the following Interstage Application Server client
functions must be installed:
Click [Install Client] from the Interstage Installer menu, and select and install the above functions.
- Repository services
- Naming services
- CORBA services
If one of problems listed below occurs, an environment setting is incorrect. Refer to Developing
Enterprise JavaBeans Client Applications and check the environment.
http://kr.fujitsu.com/products/solutions/ebiz_platform/was/technical/tutorial/ejb/kiso/kiso... 10/10/2010
Basic Knowledge about EJB Page 5 of 6
jjavax.naming.NameNotFoundException: <lookup-identifier>
java.lang.ClassCastException: [narrow()]
7. Calling an enterprise bean on another server, with Plus Developer operating as a client
Normally, Interstage Apworks and Interstage Application Server clients can be installed in a
client/server environment for development and testing. However, for testing in a client/server
environment using Interstage Application Server Plus Developer, the environment for naming
services must be configured as described below.
Server functions are included in the installation of Interstage Application Server Plus Developer.
For this reason, to make Plus Developer operate as a client of another server instead of as a server,
the following batch program provided in Plus Developer must be run to switch the naming service
to be referenced:
Interstage-application-server-installation-folder\ISPLUS\bin\odmodifyns.bat
2) Switch the naming service used for references by running odmodifyns.bat as follows:
- Switching to another server of a remote host
odmodifyns-remote-hostname [port-number]
http://kr.fujitsu.com/products/solutions/ebiz_platform/was/technical/tutorial/ejb/kiso/kiso... 10/10/2010
Basic Knowledge about EJB Page 6 of 6
To Top of Page
http://kr.fujitsu.com/products/solutions/ebiz_platform/was/technical/tutorial/ejb/kiso/kiso... 10/10/2010