0% found this document useful (0 votes)
65 views55 pages

New EJB

EJB 3.x introduces annotations that allow developers to specify services like transactions, security, and persistence without complex configuration files. This simplifies development by treating enterprise java beans like regular java objects. Key features include dependency injection, elimination of home interfaces, and use of the Java Persistence API for data access. EJB 3.x aims to make enterprise application development easier and more productive.

Uploaded by

Long Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views55 pages

New EJB

EJB 3.x introduces annotations that allow developers to specify services like transactions, security, and persistence without complex configuration files. This simplifies development by treating enterprise java beans like regular java objects. Key features include dependency injection, elimination of home interfaces, and use of the Java Persistence API for data access. EJB 3.x aims to make enterprise application development easier and more productive.

Uploaded by

Long Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

The New Enterprise Java Beans

EJB 3.x
Review
Enterprise Java Beans
J2EE/JavaEE Architecture
Logical Architecture
EJB Container (Transaction, Security, Persistent, Management, )
Objects: Session Beans (Stateless, Stateful), Entity Beans (BMP,
CMP), Message Driven Beans
Components: Component interface (Remote interface, Local
interface), Home interface (Home interface, Local Home
interface), Bean class, EJB deployment descriptors, server
deployment descriptor, DB mapping descriptors,
Implementation
JNDI
API, SPI, Naming Manager
Context Factory, Initial Context Factory
Objectives
How to build the application using EJB 3?
Need of EJB 3
New Features
Objectives

.
Web

Topic 8 Login
Advanced EJB 2.x
Java - PBL
Topic 9 Login
EJB 3.x
Enterprise
Topic 10 Shopping Cart
Session Bean

Topic 11 CRUD
MVC 2 + EJB 3.x Complete
Build Simple Application with EJB3
Requirement
Build Simple Application with EJB3
Expectation
Introduction to EJB 3.0
Overview
EJB 3 uses Metadata annotations to specify the services
that the EJB components will use when it is deployed in
the containers
Annotations help the developers to provide the specification and
based on specification, the system automatically adds code
Annotations help the developers to transform a simple POJO to
an EJB
Annotations can be only used to specify the required services but
also used to specify the component type
Introduction to EJB 3.0
Overview
EJB implements the business logic and the persistence
layer
Session and Message-driven bean reside in and use the services
of business layer
Entities reside in and use the services of persistence layer
Entities can be used to model domain objects including modeling state and
behavior
Domain-Driven Design
Domain objects should contain business logic
Represents domain objects as entities in EJB 3.0
Add business logic in domain object
Implements business logic in the application layer also known as
service layer
Introduction to EJB 3.0
Need of EJB 3.0
Benefits are as follows
Simple
The developer can focus on developing the business logic instead of concentrating
on other services such as transaction, security, resource pooling,
EJB 3 provides a practical outlook and does not demand much understanding of
theoretical intricacies
Reusable
EJB 3 is a reusable component because it can be used by multiple applications
that can make calls to the deployed enterprise bean
Scalable
EJB is used when application needs to scale beyond initial low level usage level and
support multiple concurrent users
Transactional
Transaction is support by EJB container that helps to maintain the consistent state
of the DB when an error takes place
EJB cannot be used where there is
No need for the application to have scalability, transaction management, or
security features
No need for the application to be platform independent
Introduction to EJB 3.0
Types
Introduction to EJB 3.0
Types
Session Beans & Message-driven Beans
Are same as EJB 2.x specification in concepts
Introduction to EJB 3.0
Types
Entity Beans/ Entity Classes
Represents business data
Are Java objects that store database information
Are POJO classes that use JPA for persisting data into relational
database using Object-Relational Mapping (ORM)
In EJB3, persistence activity is performed by JPA using ORM
techniques.
The standards defined by JPA are as follows
ORM configuration metadata is created for mapping of entities to
relational table
EntityManager API is used for performing persistence operations for
entities
Java Persistence Query Language (JPQL) is used for searching and
retrieving data persistence in DB
Introduction to EJB 3.0
New Features
Eases development of enterprise applications as it removes the need
for interfaces and deployment descriptors
Uses the metadata annotations to generate the interfaces and the
deployment descriptors
Annotations
Is a metadata information that is attached to an element within the code to
characterize it
Are processed when the code containing it are compiled or interpreted by
compilers, deployment tools, and so on
Can result in the generation of code documents, code artifacts, and so on
EJB 3 has defined many built-in annotations
This resulted in changes in EJB programming as it contains a mix of metadata tags
and code constructs
This made the configuration task easier for the developer (The developer can use
the defaults wherever possible)
It is the responsibility of the compilers, code generators, deployment tools to
generate the appropriate semantics
Introduction to EJB 3.0
New Features
The advantages of using annotations
Ease of use
Annotations are checked and compiled by the Java language compiler and are simple
to use
Portability
Type Checking
Annotations are instances of annotation types and are compiled in their own class
files
Runtime Reflection
Annotations are stored in the class files and accessed for runtime access
The disadvantages of using annotations
It is invisible when the bean developer and deployer are two separate entities
Before the deployer generates the bean deployment descriptor, he/she needs
to read the code so that the deployment descriptor does not override the bean
providers deployment metadata-specified configuration
Another important issue with metadata is that each time a change is made to
the bean code, the bean needs to be recompiled and repackaged
Introduction to EJB 3.0
New Features
Callback Methods
Implementation of call back methods are optional in EJB 3.0
Container invokes the method when defined by the developer
Any method can be designated as callback method to listen to life
cycle events
The developer can use the callback listener class instead of
defining the callback methods in the bean class
Elimination of Home Interface and Home Objects
Home interface has been replaced by Plain Old Java Interface
(POJI)
Home object has been replaced by POJO
Business interface contains all the business methods
Remote Business Interface
Local Business Interface
Introduction to EJB 3.0
New Features
Elimination of Component Interface
Earlier, component interfaces were used as they provided a way for
the container to notify the bean instance of the various life cycle
events affecting it
Now, bean is represented as a simple POJO class implementing
the business interface if it is a session bean
Two ways in which a bean class can get notification from the
container are as follows:
Developer writes a separate class containing the implementation of
callback notification method
Developer implements the callback notification methods within the bean
class and designate each of these methods to handle appropriate events
Both these approaches require the use of annotations
Introduction to EJB 3.0
New Features
Dependency Injection/Simplified Access
Earlier, JNDI APIs were used to gain access to environment entries
In EJB 3, dependence injection and lookup() method on
EJBContext interface have been added
Dependency Injection is a means by which the container makes available
the requested environmental entry for the bean instance
Environment variables are injected into the beans variables or methods
These are made available to the bean instance before any business methods
are invoked by the instance
Developer uses deployment descriptor or annotations to specify
these injections
The bean methods should follow the Java naming conventions for
properties (getter/ setter or accessor methods)
If the dependency injection fails, then the container discards the
bean instance and creates another bean instance
@Inject or @Resource annotations can be used for dependency
injection
Introduction to EJB 3.0
New Features
Interceptors
Used to intercept business method calls or life cycle callback
method calls
Used by Stateless, Stateful, and MDB
Help the developers to enhance the business methods by adding
additional functionality
Can be defined in a separate class
Simple JNDI lookup for EJB
The client can easily invoke methods on EJB than creating an
instance by invoking the ejbCreate method as done previously
Java Persistence API
EJB 3 provides JPA for simplifying the programming model for
entity persistence
Using the POJO model, instead of the abstract persistence
schema model
Introduction to EJBs
Packaging and Deploying

Figure 3.5 Mastering EJB 3, 4th, Rima Patel Sriganesh


Introduction to EJBs
Accessing

Figure 3.2
Mastering EJB 3, 4th,
Rima Patel Sriganesh
EJB Implementation
EJB Development Process
Requirement: JBoss 6.1.0 Final Application Server &
Netbeans 7.4
Step 1: Creating a new EJB Module project/ Enterprise
Application Project
Step 2: Creating the new corresponding bean depending on
your purpose.
Step 3: Building the business on Beans
Step 4: Mapping the JNDI to beans
Step 5: Creating the web/client application to consume
Step 6: Building the project to jar/ear file
Step 7: Deploying the project on Application server
Step 8: Running the client to test the EJB
Summary
How to build the application using EJB 3
Need of EJB 3
New Features

Q&A
Next Lecture
How to build enterprise application using
Session Beans?
Stateless
Stateful
Definition, Implementation, Life cycles
Next Lecture

.
Web

Topic 8 Login
Advanced EJB 2.x
Java - PBL
Topic 9 Login
EJB 3.x
Enterprise
Topic 10 Shopping Cart
Session Bean

Topic 11 CRUD
MVC 2 + EJB 3.x Complete
Appendix EJB Implementation
Step 1: Creating a new EJB project

Choose Enterprise Beans on Categories


Then, choose Enterprise Application on Projects. Click Next
button
EJB Implementation
Step 1: Creating a new EJB project

Fill your project name

Click Next button


EJB Implementation
Step 1: Creating a new EJB project
Choose the Jboss
Server that is
added
Choose the
JavaEE5

Click Finish button


EJB Implementation
Step 1: Creating a new EJB project
EJB Implementation
Step 2: Creating the new corresponding bean

Choose Enterprise JavaBeans on Categories


Then, choose Session Bean on File Types. Click Next button
EJB Implementation
Step 2: Creating the new corresponding bean

Fill your bean name

Fill or choose the


package name

Choose stateless or
stateful
Choose local

Click Finish button


EJB Implementation
Step 2: Creating the new corresponding bean
EJB Implementation
Step 2: Creating the new corresponding bean
Create the remote business interface
Create a java interface with annotation @Remote
Then, implement it in bean class
EJB Implementation
Addition Create the DS to connect DB
Create the jboss-ds. xml in Server Resources in EJB module
Modify the connection to create the data source that is used to connect to DB
EJB Implementation
Addition Create the DS to connect DB
Deploy data source file: jboss-ds.xml to server
EJB Implementation
Step 3: Building the business methods
Adding a new business method
Right click on source code of the
Bean file
Then, choose Insert Code, click
Add Business Method

Fill or type the method


name with return type and
add all parameters
Then, click OK Button
EJB Implementation
Step 3: Building the business methods

Implement the body of


method corresponding
with your purpose
EJB Implementation
Addition Using Resource in Bean
Adding a resource into bean
Right click on source code of the
Bean file
Then, choose Insert Code, click
Use Database

Click Add button


The Add Datasource
Reference is shown
EJB Implementation
Addition Using Resource in Bean

Choose the Server Data Source then choosing the datasource that is
deployed in server
Type the reference Name
If click the copy selected DataSource to Project, the reference code
in jboss is not added
Click OK button to return the choose Database dialog
EJB Implementation
Addition Using Resource in Bean 2nd Way
Do not create the datasource file jboss-ds.xml
Use create the data link in Database of Services tab
Use the Use Database in Insert Code context menu
Choose the Project Data Source then click Add
EJB Implementation
Addition Using Resource in Bean 2nd Way

Fill the JNDI name


Choose the DB connection
Click Ok button (The datasource file can be created in
automatically in Server Resource folder)
EJB Implementation
Addition Using Resource in Bean 2nd way

Type the reference Name


Click OK button to return the choose Database dialog
EJB Implementation
Addition Using Resource in Bean

Click OK button, the code is generated


EJB Implementation
Addition Using Resource in Bean
EJB Implementation
Using Resource in Bean determine DS name
EJB Implementation
Step 3: Building the business methods
EJB Implementation
Step 4: Mapping the JNDI to beans
EJB Implementation
Step 5: Create the UI in the web project
EJB Implementation
Process in client coding in Servlet
EJB Implementation
Process in client coding in Servlet
EJB Implementation
Step 6: Building & Deploying
EJB Implementation
Step 6: Building & Deploying
EJB Implementation
Un-Deploying
EJB Implementation
Process in client coding in Servlet other ways
In the Servlet code
Right click on servlet code, click Insert
Code
Click Call Enterprise Bean

In Call Enterprise Bean dialog


Choose the bean
Input the Reference Name
Click OK
EJB Implementation
Process in client coding in Servlet other ways
EJB Implementation
Process in client coding in Servlet other ways

You might also like