SlideShare a Scribd company logo
1 of 22
Download to read offline
© Peter R. Egli 2015
1/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
Peter R. Egli
INDIGOO.COM
INTRODUCTION TO ENTERPRISE JAVA BEANS,
JAVA'S SERVER SIDE COMPONENT TECHNOLOGY
EJBENTERPRISE JAVA BEANS
© Peter R. Egli 2015
2/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
Contents
1. What is a bean?
2. Why EJB?
3. Evolution of EJB
4. Bean interfaces
5. EJB bean types
6. Lifecycle of EJBs
7. Session facade pattern for uniform bean access
8. Bean deployment
9. EJB container
10. Comparison of EJB with other DOT technology like CORBA
11. When to use EJB
© Peter R. Egli 2015
3/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
1. What is a bean?
 Beans are business logic components that implement a standard interface through which
the bean is hooked into the bean container (= runtime object for bean).
 A Java class implementing one of the standard bean interfaces is a bean.
 Beans can be accessed remotely, usually from a client tier.
Bean
Bean
container
Snapshots from OpenEJB libs in Eclipse package explorer
© Peter R. Egli 2015
4/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
2. Why EJB?
Common concerns in different applications lead to re-implementing the same functionality for
business logic components.
Examples of common functionality:
- Persistence
- Transactions
- Security
- Runtime and lifecycle management (create, start, stop and delete component)
EJB is a framework that provides the following services to applications:
- Persistence
- Transaction processing
- Concurrency control (each client accesses its own bean instance)
- Events using JMS (Java Messaging Service)
- Naming and directory services via JNDI (Java Naming and Directory Interface)
- Security using JAAS (Java Authentication and Authorization Service)
- Deployment of software components to a server host
- Remote procedure calls via RMI (RMI over IIOP)
- Exposing business functionality via web services
Business
logic
Front end
(protocol, GUI)
Backend
(DB)
© Peter R. Egli 2015
5/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
3. Evolution of EJB
EJB 1.0 (1998):
Framework for distributed applications, backed by industry giants IBM and Sun.
Complexity (difficult to write beans due to high number of interfaces, exception etc. to be used).
Low performance due to use of CORBA as the only available remoting technology.
EJB 2.0 (2001):
Performance improvements due to the introduction of local and remote interfaces (local applications use faster local
interface).
Introduction of Message Driven Beans (connect EJB with JMS).
Complexity (specification: 646 pages).
Severely limited SQL dialect for entity beans (EJBQL).
EJB 3.0 (2006):
Home interface eliminated.
Deployment descriptor optional, replaced by Java annotations.
Implementation of business interfaces as POJI (Plain Old Java Interface) with Java annotations.
Implementation of beans as POJOs (Plain Old Java Object) with Java annotations.
Entity beans replaced by POJOs that use JPA-annotations (Java Persistence API) @Resource.
JNDI-lookup of home interface replaced by dependency injection via annotations (@Inject).
Beans no longer need to implement callback methods ejbCreate() and ejbActivate(); instead they may use annotations.
Client
CORBA
DBBean
Container
© Peter R. Egli 2015
6/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
4. Bean interfaces (1/5)
Access to beans is provided through 2 interfaces (in EJB versions EJB 1.x and EJB 2.x):
Component interface:
The component interface (also called remote interface) provides the business logic methods
(methods that are specific to a bean).
The component interface methods are instance (object) methods.
Home interface:
The home interface specifies bean lifecycle methods (creation and deletion of beans). These
methods are static (class) methods.
Business logic methods
Bean lifecycle methods and
finder methods (for entity beans only)
BankAccountEJB
Component Interface
deposit()
credit()
Home Interface
create()
findByPrimaryKey()
Remote Client
© Peter R. Egli 2015
7/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
4. Bean interfaces (2/5)
Both component and home interface are Java RMI interfaces.
This allows remote access to a Java class that implements these interfaces (bean).
But: A bean can be accessed remotely (client is in a different VM) and locally (client is in the
same VM as the bean container), see page 8.
UML view:
BankAccCI
Comp. interface
BankAccountEJB
BankAccHI
Home interface
EJBHome
Remote
EJBObject
Java RMI remote interface.
EJB component and home interfaces.
User defined component and
home interfaces.
© Peter R. Egli 2015
8/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
Bean Container
4. Bean interfaces (3/5)
Typical access of a client to a bean (EJB 1.x and EJB 2.x):
1. Lookup of the home interface through JNDI (Java Naming and Directory Interface).
2. The Client calls the create() method on the home object.
3. The home object creates an instance of the bean and calls ejbCreate() with the same
signature as the client. The container returns a reference to the created bean instance.
4. The client calls a business method on the created bean.
5. When finished the client destroys the bean by calling remove() on the home interface.
JNDI
Client
Comp.
Home
Bean
instance
1
4 4
2 35
© Peter R. Egli 2015
9/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
JVM
Bean container
4. Bean interfaces (4/5)
Local versus remote bean access (1/2):
EJB 2.0 introduced local and remote access interfaces.
Clients can run in a different JVM (= remote client) or in the same JVM as the bean (= local
client).
Arguments are passed-by-value in calls made by the remote client and passed-by-reference
in calls made by the local client.
Bean
Remote
Client
Remote
Component
Interface
Remote
Home
Interface
Local
Component
Interface
Local
Home
Interface
Local
Client
Local client viewRemote client view
© Peter R. Egli 2015
10/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
4. Bean interfaces (5/5)
Local versus remote bean access (2/2):
Remote access:
 Loose coupling between client and server (=bean).
 Call-by-value arguments in calls (copy-semantics).
 Potentially slow (network latency, network stack processing, parameter marshalling etc.).
 Remote interface = RMI interface.
 Location transparency (client does not know where server bean resides).
 Recommended usage: Coarse-grain access client to server (only occasional accesses).
Local access:
 Tight coupling between client and server (=bean).
 Call-by-reference arguments (no copying).
 Plain Java object interface (direct method calls).
 No location transparency.
 Recommended usage: May be used when client and bean have a tight interaction.
© Peter R. Egli 2015
11/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
5. EJB bean types (1/3)
5.1. Session bean (EJB 1.x, 2.x and 3.x):
A session bean contains and represents some business logic.
a. Stateless session bean:
 The instance variables of the bean are maintained only for the duration of the client method
invocation.
 Stateless session beans provide better scalability (beans may support multiple clients).
b. Stateful session bean:
 The bean maintains a conversational state throughout the session with the client, until the
session terminates.
Bean container
Client
S-BeanClient
Client
Bean container
Client
S-BeanClient
Client
S-Bean
S-Bean
© Peter R. Egli 2015
12/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
5. EJB bean types (2/3)
5.2. Entity bean (EJB1.x, EJB 2.x):
An entity bean represents persistent data maintained in a database (DB).
Typically an entity bean represents a row (=entry) in a DB table.
An entity bean is identified by its primary key.
Types of persistence:
a. BMP – Bean Managed Persistence:
 The bean manages persistence on its own (bean developer must write the DB access calls).
b. CMP – Container Managed Persistence:
 The persistence is managed by the bean container, i.e. the container generates the DB
access calls.
Bean container
E-BeanClient
DB
© Peter R. Egli 2015
13/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
5. EJB bean types (3/3)
5.3. MDB - Message Driven Bean (EJB 2.x, EJB 3.x):
A message driven bean acts as a listener on JMS message queues (Java Message Service).
Clients do not have direct access to MDBs through home or component interfaces.
MDBs are similar to stateless session beans in that they may receive messages from
multiple clients.
MDBs allow asynchronous interaction between client and server which reduces the coupling
between them.
MDBs may be used as an asynchronous interface to a server application. The MDB receives
messages and converts these to method calls on session and entity beans in the bean
container.
JMS queue
Bean container
MDB
Client
S-Bean
E-Bean
© Peter R. Egli 2015
14/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
6. Lifecycle of EJBs (1/4)
6.1. Stateless session bean (EJB 1.x, 2.x, 3.x):
Stateless session beans have a very simple lifecycle.
They either do not exist or are ready for receiving method invocations.
Lifecycle steps:
1. The client obtains a reference to a stateless session bean (JNDI lookup).
2. The EJB container performs dependency injection (evaluation of @EJB, @Resource, @Inject
annotations if such are provided). The bean goes into the state Ready.
3. The EJB container invokes methods annotated with @PostConstruct.
4. The client invokes business methods on the bean.
5. When the client reference goes out of scope, the lifecycle of the bean ends. The EJB
container calls methods annotated with @PreDestroy and then disposes of the bean.
1. Create bean
2. Dependency injection (if any)
3. PostConstruct callbacks (if any)
Does not
exist
Ready
5. PreDestroy callbacks (if any)
© Peter R. Egli 2015
15/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
6. Lifecycle of EJBs (2/4)
6.2. Statefull session bean (EJB 1.x, 2.x, 3.x):
In addition to the lifecycle states of stateless beans, stateful beans have the state Passive.
The EJB container may move (evict) unused beans to secondary storage, e.g. disk for saving
resources (RAM). When a client invokes a method on a passivated object, the container
resurrects a bean of the requested type, loads it with state data that it had before passivation
(state data is stored separately on disk) and then performs the method invocation.
Lifecycle steps:
The bean creation process (steps 1. through 3.) is the same as for stateless session beans.
4. The client invokes business methods on the bean.
5. The server may, when it detects that the bean is not invoked for some time, passivate the
bean. Before passivating the bean, the container calls the @PrePassivate callback.
6. When a new client invocation arrives, the EJB container retrieves a bean of the requested
type, fills it with state data the bean had before passivation, calls the @PostActive annotation
methods and then the called business method.
7. The actions for bean destruction are the same as for stateless beans.
1. Create bean
2. Dependency injection (if any)
3. PostConstruct callbacks (if any)
Does not
exist
Ready
7. PreDestroy callbacks (if any)
Passive
5. PrePassivate callbacks
6. PostActivate callbacks
© Peter R. Egli 2015
16/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
6. Lifecycle of EJBs (3/4)
6.3. Entity bean (EJB 1.x and 2.x):
Entity beans are moved between Pooled and Ready states, either by client or container request.
Lifecycle steps:
1. The EJB container creates the instance, calls setEntityContext() (set the entity context that
may be used in transactions) and moves the bean to the bean pool. At this stage the bean is not
associated with any particular object identity.
There exist 2 paths for moving from the Pooled state to the Ready state:
2.a. The client calls the create() method which causes the container to move the bean to the
Ready state. Before doing so, the container calls the ejbCreate() and ejbPostCreate() methods.
2.b. The container itself calls the ejbActivate() method and moves the bean to the Ready state.
Likewise there are 2 paths to move a bean from the Ready to the Pooled state:
3.a. The client calls the remove() method, the container then calls the ejbRemove() method.
3.b. The container calls the ejbPassivate() method.
4. Before destroying the bean, the container
calls unsetEntityContext().
1. setEntityContext()
Does not
exist
Pooled
4. unsetEntityContext()
Ready
2.b. ejbActivate
3.a. remove
ejbRemove
2.a. create
ejbCreate
ejbPostCreate
3.b. ejbPassivate
© Peter R. Egli 2015
17/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
6. Lifecycle of EJBs (4/4)
6.4. Message driven beans (EJB 2.x and EJB 3.x):
The lifecycle of MDBs is similar to the lifecycle of stateless session beans.
Lifecycle steps:
1. The container creates a pool of MDBs.
2. The container injects dependencies into the bean.
3. The container calls methods annotated with @PostConstruct and activates the bean.
4. Upon message reception, the onMessage() method is called in which the bean processes the
message.
5. Before the bean is destroyed, the container calls any @PreDestroy callback methods.
Does not
exist
Ready
4. onMessage
1. Create bean
2. Dependency injection (if any)
3. PostConstruct callbacks (if any)
5. PreDestroy callbacks (if any)
© Peter R. Egli 2015
18/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
7. Session facade pattern for uniform bean access
Common problems with beans:
 Tight coupling between client objects and beans (direct dependency between clients and
business objects).
 Too many method invocations between client and server (network performance problems).
Proposed solution by Sun:
 Use of a session bean as a facade that hides complexity from the client and manages
business and data objects.
Remote
Client
Session Bean
Session
Facade
EntityBean
SessionBean
Busines
object (POJO)
The facade provides a uniform
interface to the client and hides the
access to the business
objects (entity and session beans).
The facade session bean provides
high-level methods that dispatch
individual method invocations
to the attached beans.
© Peter R. Egli 2015
19/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
8. Bean deployment
In EJB 1.x and EJB 2.x, a deployment descriptor (XML-file) was required to inform the bean
container (JEE application server) about the classes implementing the home and component
(remote) interfaces, the type of bean etc.
In EJB 3.0, most of the XML elements were replaced by annotations.
<?xml version="1.0" encoding="UTF-8"?>
<application-client version="5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application-
client_5.xsd">
<description>My super application</description>
<display-name>MrBean</display-name>
<enterprise-beans>
<session>
<ejb-name>MySuperBean</ejb-name>
<home>com.indigoo.wsmw.ejb.MySuperBeanHomeInterface</home>
<remote>com.indigoo.wsmw.ejb.MySuperBeanInterface</remote>
<ejb-class>com.indigoo.wsmw.ejb.MySuperBean</ejb-class>
<session-type>Stateless</session-type>
</session>
</enterprise-beans>
</application-client>
© Peter R. Egli 2015
20/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
JEE Server
Client Machine
Browser
Application
Client
Container
Application
Client
Web Container
Servlet
JSP
Page
EJB Container
EJB
Bean
EJB
Bean
DB
9. EJB container
The EJB container is the place where Java beans „live“ (are hosted).
EJB containers are part of the J2EE (application) server which in turn runs in a JVM.
EJB daemon which
hosts and services EJB
beans.
Java application server
(Examples: Glassfish, JBoss,
IBM Websphere, Oracle Weblogic,
Apache Geronimo etc.).
JVM
© Peter R. Egli 2015
21/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
10. Comparison of EJB with other DOT technology like CORBA
EJB and CORBA are similar in many ways. There are some notable differences:
Feature CORBA EJB
Middleware type CORBA = explicit middleware:
Client accesses directly the CORBA API
EJB = implicit middleware:
Client is shielded from the specifics of the
EJB-API
Language support CORBA = language-neutral (there are
various IDL-language-mappings)
EJB = Java only
Server-side integration CORBA-POA-model is more flexible
CORBA leaves it open how to integrate
the server-side objects
EJBs are tightly integrated into the EJB-
container
Interface specification IDL (language-neutral syntax and
semantics)
Interface specification = Java interface
Configuration CORBA does not provide interfaces or
concepts for configuration
EJB 1 and 2:
EJB configuration is placed into a
deployment descriptor (XML file)
EJB 3:
Configuration through annotations
CORBA: Common Object Request Broker Architecture
DOT: Distributed Object Technology
IDL: Interface Description Language
POA: Portable Object Adaptor
© Peter R. Egli 2015
22/22
Rev. 1.80
EJB – Enterprise Java Beans indigoo.com
11. When to use EJB
EJB may be overkill in many applications and alternatives (namely Spring) may often be better
suited to fulfill a certain task. The following table compares EJB and Spring:
Feature EJB Spring Framework
Multi-tiered application
(distributed application,
remoting)
Yes (client and server tier, separation of
client and business logic is one of the
main goals of EJB).
Container-managed remote method
calls.
Remoting through RMI, HTTPInvoker, JAX-RPC
web services, JMS.
Standard platform,
vendor independence
Yes (defined by JCP, supported by all
major JEE vendors)
No (vendor = SpringSource)
Application server JEE application server needed. Spring comes with its own (lightweight) object
container.
Dependency injection Yes (EJB 3.0), but less powerful than
Spring (only JNDI-objects, not POJOs).
Yes (even between POJOs).
Distributed transactions Yes (must use JTA). Yes (JTA and other transaction managers
possible).
Persistence / ORM Programmatic bean-managed
persistence.
Vendor specific ORM.
Integration with different persistence frameworks
(Hibernate etc.)
JEE: Java Enterprise Edition
JCP: Java Community Process
JMS: Java Messaging Service
JTA: Java Transaction API
ORM: Object Relational Mapper
POJO: Plain Old Java Object

More Related Content

What's hot

Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1sandeep54552
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Elizabeth alexander
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in JavaTushar B Kute
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side ProgrammingMilan Thapa
 
Race conditions
Race conditionsRace conditions
Race conditionsMohd Arif
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate pptAneega
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Appletsamitksaha
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
Java web application development
Java web application developmentJava web application development
Java web application developmentRitikRathaur
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mountingrajshreemuthiah
 
File concept and access method
File concept and access methodFile concept and access method
File concept and access methodrajshreemuthiah
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Peter R. Egli
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net frameworkThen Murugeshwari
 
servlet in java
servlet in javaservlet in java
servlet in javasowfi
 

What's hot (20)

Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side Programming
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Race conditions
Race conditionsRace conditions
Race conditions
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Java web application development
Java web application developmentJava web application development
Java web application development
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mounting
 
File concept and access method
File concept and access methodFile concept and access method
File concept and access method
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
 
EJB .
EJB .EJB .
EJB .
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net framework
 
servlet in java
servlet in javaservlet in java
servlet in java
 

Similar to Enterprise Java Beans - EJB

EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questionsguest346cb1
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3phanleson
 
Aravind vinnakota ejb_architecture
Aravind vinnakota ejb_architectureAravind vinnakota ejb_architecture
Aravind vinnakota ejb_architecturetayab4687
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006achraf_ing
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionKelum Senanayake
 
Session 4 Tp4
Session 4 Tp4Session 4 Tp4
Session 4 Tp4phanleson
 
ADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptrani marri
 
Connecting ejb with mule
Connecting ejb with muleConnecting ejb with mule
Connecting ejb with muleRuman Khan
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)vikram singh
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2vikram singh
 
1-introduction to ejb
1-introduction to ejb1-introduction to ejb
1-introduction to ejbashishkirpan
 
J2EE - Practical Overview
J2EE - Practical OverviewJ2EE - Practical Overview
J2EE - Practical OverviewSvetlin Nakov
 

Similar to Enterprise Java Beans - EJB (20)

EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questions
 
EJB 2
EJB 2EJB 2
EJB 2
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3
 
Ch4 ejb
Ch4 ejbCh4 ejb
Ch4 ejb
 
Ejb notes
Ejb notesEjb notes
Ejb notes
 
Introcution to EJB
Introcution to EJBIntrocution to EJB
Introcution to EJB
 
Aravind vinnakota ejb_architecture
Aravind vinnakota ejb_architectureAravind vinnakota ejb_architecture
Aravind vinnakota ejb_architecture
 
Ejb - september 2006
Ejb  - september 2006Ejb  - september 2006
Ejb - september 2006
 
EJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another IntroductionEJB 3.0 - Yet Another Introduction
EJB 3.0 - Yet Another Introduction
 
Session 4 Tp4
Session 4 Tp4Session 4 Tp4
Session 4 Tp4
 
ADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.pptADVANCED JAVA MODULE I & II.ppt
ADVANCED JAVA MODULE I & II.ppt
 
Unite5-EJB-2019.ppt
Unite5-EJB-2019.pptUnite5-EJB-2019.ppt
Unite5-EJB-2019.ppt
 
Ejb intro
Ejb introEjb intro
Ejb intro
 
Connecting ejb with mule
Connecting ejb with muleConnecting ejb with mule
Connecting ejb with mule
 
Enterprise java beans(ejb)
Enterprise java beans(ejb)Enterprise java beans(ejb)
Enterprise java beans(ejb)
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2
 
1-introduction to ejb
1-introduction to ejb1-introduction to ejb
1-introduction to ejb
 
J2 Ee Overview
J2 Ee OverviewJ2 Ee Overview
J2 Ee Overview
 
Virtual classroom
Virtual classroomVirtual classroom
Virtual classroom
 
J2EE - Practical Overview
J2EE - Practical OverviewJ2EE - Practical Overview
J2EE - Practical Overview
 

More from Peter R. Egli

LPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M ScenariosLPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M ScenariosPeter R. Egli
 
Data Networking Concepts
Data Networking ConceptsData Networking Concepts
Data Networking ConceptsPeter R. Egli
 
Communication middleware
Communication middlewareCommunication middleware
Communication middlewarePeter R. Egli
 
Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)Peter R. Egli
 
Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)Peter R. Egli
 
Microsoft .NET Platform
Microsoft .NET PlatformMicrosoft .NET Platform
Microsoft .NET PlatformPeter R. Egli
 
Overview of Cloud Computing
Overview of Cloud ComputingOverview of Cloud Computing
Overview of Cloud ComputingPeter R. Egli
 
MQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message QueueingMQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message QueueingPeter R. Egli
 
Enterprise Application Integration Technologies
Enterprise Application Integration TechnologiesEnterprise Application Integration Technologies
Enterprise Application Integration TechnologiesPeter R. Egli
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyPeter R. Egli
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development KitPeter R. Egli
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Peter R. Egli
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Peter R. Egli
 
Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)Peter R. Egli
 
MSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message QueueingMSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message QueueingPeter R. Egli
 
Common Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBACommon Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBAPeter R. Egli
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Peter R. Egli
 
JMS - Java Messaging Service
JMS - Java Messaging ServiceJMS - Java Messaging Service
JMS - Java Messaging ServicePeter R. Egli
 
Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Peter R. Egli
 

More from Peter R. Egli (20)

LPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M ScenariosLPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
 
Data Networking Concepts
Data Networking ConceptsData Networking Concepts
Data Networking Concepts
 
Communication middleware
Communication middlewareCommunication middleware
Communication middleware
 
Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)
 
Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)
 
Microsoft .NET Platform
Microsoft .NET PlatformMicrosoft .NET Platform
Microsoft .NET Platform
 
Overview of Cloud Computing
Overview of Cloud ComputingOverview of Cloud Computing
Overview of Cloud Computing
 
MQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message QueueingMQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message Queueing
 
Enterprise Application Integration Technologies
Enterprise Application Integration TechnologiesEnterprise Application Integration Technologies
Enterprise Application Integration Technologies
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technology
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development Kit
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)
 
Web services
Web servicesWeb services
Web services
 
Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)
 
MSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message QueueingMSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message Queueing
 
Common Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBACommon Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBA
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)
 
JMS - Java Messaging Service
JMS - Java Messaging ServiceJMS - Java Messaging Service
JMS - Java Messaging Service
 
Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

Enterprise Java Beans - EJB

  • 1. © Peter R. Egli 2015 1/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com Peter R. Egli INDIGOO.COM INTRODUCTION TO ENTERPRISE JAVA BEANS, JAVA'S SERVER SIDE COMPONENT TECHNOLOGY EJBENTERPRISE JAVA BEANS
  • 2. © Peter R. Egli 2015 2/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com Contents 1. What is a bean? 2. Why EJB? 3. Evolution of EJB 4. Bean interfaces 5. EJB bean types 6. Lifecycle of EJBs 7. Session facade pattern for uniform bean access 8. Bean deployment 9. EJB container 10. Comparison of EJB with other DOT technology like CORBA 11. When to use EJB
  • 3. © Peter R. Egli 2015 3/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 1. What is a bean?  Beans are business logic components that implement a standard interface through which the bean is hooked into the bean container (= runtime object for bean).  A Java class implementing one of the standard bean interfaces is a bean.  Beans can be accessed remotely, usually from a client tier. Bean Bean container Snapshots from OpenEJB libs in Eclipse package explorer
  • 4. © Peter R. Egli 2015 4/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 2. Why EJB? Common concerns in different applications lead to re-implementing the same functionality for business logic components. Examples of common functionality: - Persistence - Transactions - Security - Runtime and lifecycle management (create, start, stop and delete component) EJB is a framework that provides the following services to applications: - Persistence - Transaction processing - Concurrency control (each client accesses its own bean instance) - Events using JMS (Java Messaging Service) - Naming and directory services via JNDI (Java Naming and Directory Interface) - Security using JAAS (Java Authentication and Authorization Service) - Deployment of software components to a server host - Remote procedure calls via RMI (RMI over IIOP) - Exposing business functionality via web services Business logic Front end (protocol, GUI) Backend (DB)
  • 5. © Peter R. Egli 2015 5/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 3. Evolution of EJB EJB 1.0 (1998): Framework for distributed applications, backed by industry giants IBM and Sun. Complexity (difficult to write beans due to high number of interfaces, exception etc. to be used). Low performance due to use of CORBA as the only available remoting technology. EJB 2.0 (2001): Performance improvements due to the introduction of local and remote interfaces (local applications use faster local interface). Introduction of Message Driven Beans (connect EJB with JMS). Complexity (specification: 646 pages). Severely limited SQL dialect for entity beans (EJBQL). EJB 3.0 (2006): Home interface eliminated. Deployment descriptor optional, replaced by Java annotations. Implementation of business interfaces as POJI (Plain Old Java Interface) with Java annotations. Implementation of beans as POJOs (Plain Old Java Object) with Java annotations. Entity beans replaced by POJOs that use JPA-annotations (Java Persistence API) @Resource. JNDI-lookup of home interface replaced by dependency injection via annotations (@Inject). Beans no longer need to implement callback methods ejbCreate() and ejbActivate(); instead they may use annotations. Client CORBA DBBean Container
  • 6. © Peter R. Egli 2015 6/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 4. Bean interfaces (1/5) Access to beans is provided through 2 interfaces (in EJB versions EJB 1.x and EJB 2.x): Component interface: The component interface (also called remote interface) provides the business logic methods (methods that are specific to a bean). The component interface methods are instance (object) methods. Home interface: The home interface specifies bean lifecycle methods (creation and deletion of beans). These methods are static (class) methods. Business logic methods Bean lifecycle methods and finder methods (for entity beans only) BankAccountEJB Component Interface deposit() credit() Home Interface create() findByPrimaryKey() Remote Client
  • 7. © Peter R. Egli 2015 7/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 4. Bean interfaces (2/5) Both component and home interface are Java RMI interfaces. This allows remote access to a Java class that implements these interfaces (bean). But: A bean can be accessed remotely (client is in a different VM) and locally (client is in the same VM as the bean container), see page 8. UML view: BankAccCI Comp. interface BankAccountEJB BankAccHI Home interface EJBHome Remote EJBObject Java RMI remote interface. EJB component and home interfaces. User defined component and home interfaces.
  • 8. © Peter R. Egli 2015 8/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com Bean Container 4. Bean interfaces (3/5) Typical access of a client to a bean (EJB 1.x and EJB 2.x): 1. Lookup of the home interface through JNDI (Java Naming and Directory Interface). 2. The Client calls the create() method on the home object. 3. The home object creates an instance of the bean and calls ejbCreate() with the same signature as the client. The container returns a reference to the created bean instance. 4. The client calls a business method on the created bean. 5. When finished the client destroys the bean by calling remove() on the home interface. JNDI Client Comp. Home Bean instance 1 4 4 2 35
  • 9. © Peter R. Egli 2015 9/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com JVM Bean container 4. Bean interfaces (4/5) Local versus remote bean access (1/2): EJB 2.0 introduced local and remote access interfaces. Clients can run in a different JVM (= remote client) or in the same JVM as the bean (= local client). Arguments are passed-by-value in calls made by the remote client and passed-by-reference in calls made by the local client. Bean Remote Client Remote Component Interface Remote Home Interface Local Component Interface Local Home Interface Local Client Local client viewRemote client view
  • 10. © Peter R. Egli 2015 10/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 4. Bean interfaces (5/5) Local versus remote bean access (2/2): Remote access:  Loose coupling between client and server (=bean).  Call-by-value arguments in calls (copy-semantics).  Potentially slow (network latency, network stack processing, parameter marshalling etc.).  Remote interface = RMI interface.  Location transparency (client does not know where server bean resides).  Recommended usage: Coarse-grain access client to server (only occasional accesses). Local access:  Tight coupling between client and server (=bean).  Call-by-reference arguments (no copying).  Plain Java object interface (direct method calls).  No location transparency.  Recommended usage: May be used when client and bean have a tight interaction.
  • 11. © Peter R. Egli 2015 11/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 5. EJB bean types (1/3) 5.1. Session bean (EJB 1.x, 2.x and 3.x): A session bean contains and represents some business logic. a. Stateless session bean:  The instance variables of the bean are maintained only for the duration of the client method invocation.  Stateless session beans provide better scalability (beans may support multiple clients). b. Stateful session bean:  The bean maintains a conversational state throughout the session with the client, until the session terminates. Bean container Client S-BeanClient Client Bean container Client S-BeanClient Client S-Bean S-Bean
  • 12. © Peter R. Egli 2015 12/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 5. EJB bean types (2/3) 5.2. Entity bean (EJB1.x, EJB 2.x): An entity bean represents persistent data maintained in a database (DB). Typically an entity bean represents a row (=entry) in a DB table. An entity bean is identified by its primary key. Types of persistence: a. BMP – Bean Managed Persistence:  The bean manages persistence on its own (bean developer must write the DB access calls). b. CMP – Container Managed Persistence:  The persistence is managed by the bean container, i.e. the container generates the DB access calls. Bean container E-BeanClient DB
  • 13. © Peter R. Egli 2015 13/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 5. EJB bean types (3/3) 5.3. MDB - Message Driven Bean (EJB 2.x, EJB 3.x): A message driven bean acts as a listener on JMS message queues (Java Message Service). Clients do not have direct access to MDBs through home or component interfaces. MDBs are similar to stateless session beans in that they may receive messages from multiple clients. MDBs allow asynchronous interaction between client and server which reduces the coupling between them. MDBs may be used as an asynchronous interface to a server application. The MDB receives messages and converts these to method calls on session and entity beans in the bean container. JMS queue Bean container MDB Client S-Bean E-Bean
  • 14. © Peter R. Egli 2015 14/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 6. Lifecycle of EJBs (1/4) 6.1. Stateless session bean (EJB 1.x, 2.x, 3.x): Stateless session beans have a very simple lifecycle. They either do not exist or are ready for receiving method invocations. Lifecycle steps: 1. The client obtains a reference to a stateless session bean (JNDI lookup). 2. The EJB container performs dependency injection (evaluation of @EJB, @Resource, @Inject annotations if such are provided). The bean goes into the state Ready. 3. The EJB container invokes methods annotated with @PostConstruct. 4. The client invokes business methods on the bean. 5. When the client reference goes out of scope, the lifecycle of the bean ends. The EJB container calls methods annotated with @PreDestroy and then disposes of the bean. 1. Create bean 2. Dependency injection (if any) 3. PostConstruct callbacks (if any) Does not exist Ready 5. PreDestroy callbacks (if any)
  • 15. © Peter R. Egli 2015 15/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 6. Lifecycle of EJBs (2/4) 6.2. Statefull session bean (EJB 1.x, 2.x, 3.x): In addition to the lifecycle states of stateless beans, stateful beans have the state Passive. The EJB container may move (evict) unused beans to secondary storage, e.g. disk for saving resources (RAM). When a client invokes a method on a passivated object, the container resurrects a bean of the requested type, loads it with state data that it had before passivation (state data is stored separately on disk) and then performs the method invocation. Lifecycle steps: The bean creation process (steps 1. through 3.) is the same as for stateless session beans. 4. The client invokes business methods on the bean. 5. The server may, when it detects that the bean is not invoked for some time, passivate the bean. Before passivating the bean, the container calls the @PrePassivate callback. 6. When a new client invocation arrives, the EJB container retrieves a bean of the requested type, fills it with state data the bean had before passivation, calls the @PostActive annotation methods and then the called business method. 7. The actions for bean destruction are the same as for stateless beans. 1. Create bean 2. Dependency injection (if any) 3. PostConstruct callbacks (if any) Does not exist Ready 7. PreDestroy callbacks (if any) Passive 5. PrePassivate callbacks 6. PostActivate callbacks
  • 16. © Peter R. Egli 2015 16/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 6. Lifecycle of EJBs (3/4) 6.3. Entity bean (EJB 1.x and 2.x): Entity beans are moved between Pooled and Ready states, either by client or container request. Lifecycle steps: 1. The EJB container creates the instance, calls setEntityContext() (set the entity context that may be used in transactions) and moves the bean to the bean pool. At this stage the bean is not associated with any particular object identity. There exist 2 paths for moving from the Pooled state to the Ready state: 2.a. The client calls the create() method which causes the container to move the bean to the Ready state. Before doing so, the container calls the ejbCreate() and ejbPostCreate() methods. 2.b. The container itself calls the ejbActivate() method and moves the bean to the Ready state. Likewise there are 2 paths to move a bean from the Ready to the Pooled state: 3.a. The client calls the remove() method, the container then calls the ejbRemove() method. 3.b. The container calls the ejbPassivate() method. 4. Before destroying the bean, the container calls unsetEntityContext(). 1. setEntityContext() Does not exist Pooled 4. unsetEntityContext() Ready 2.b. ejbActivate 3.a. remove ejbRemove 2.a. create ejbCreate ejbPostCreate 3.b. ejbPassivate
  • 17. © Peter R. Egli 2015 17/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 6. Lifecycle of EJBs (4/4) 6.4. Message driven beans (EJB 2.x and EJB 3.x): The lifecycle of MDBs is similar to the lifecycle of stateless session beans. Lifecycle steps: 1. The container creates a pool of MDBs. 2. The container injects dependencies into the bean. 3. The container calls methods annotated with @PostConstruct and activates the bean. 4. Upon message reception, the onMessage() method is called in which the bean processes the message. 5. Before the bean is destroyed, the container calls any @PreDestroy callback methods. Does not exist Ready 4. onMessage 1. Create bean 2. Dependency injection (if any) 3. PostConstruct callbacks (if any) 5. PreDestroy callbacks (if any)
  • 18. © Peter R. Egli 2015 18/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 7. Session facade pattern for uniform bean access Common problems with beans:  Tight coupling between client objects and beans (direct dependency between clients and business objects).  Too many method invocations between client and server (network performance problems). Proposed solution by Sun:  Use of a session bean as a facade that hides complexity from the client and manages business and data objects. Remote Client Session Bean Session Facade EntityBean SessionBean Busines object (POJO) The facade provides a uniform interface to the client and hides the access to the business objects (entity and session beans). The facade session bean provides high-level methods that dispatch individual method invocations to the attached beans.
  • 19. © Peter R. Egli 2015 19/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 8. Bean deployment In EJB 1.x and EJB 2.x, a deployment descriptor (XML-file) was required to inform the bean container (JEE application server) about the classes implementing the home and component (remote) interfaces, the type of bean etc. In EJB 3.0, most of the XML elements were replaced by annotations. <?xml version="1.0" encoding="UTF-8"?> <application-client version="5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application- client_5.xsd"> <description>My super application</description> <display-name>MrBean</display-name> <enterprise-beans> <session> <ejb-name>MySuperBean</ejb-name> <home>com.indigoo.wsmw.ejb.MySuperBeanHomeInterface</home> <remote>com.indigoo.wsmw.ejb.MySuperBeanInterface</remote> <ejb-class>com.indigoo.wsmw.ejb.MySuperBean</ejb-class> <session-type>Stateless</session-type> </session> </enterprise-beans> </application-client>
  • 20. © Peter R. Egli 2015 20/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com JEE Server Client Machine Browser Application Client Container Application Client Web Container Servlet JSP Page EJB Container EJB Bean EJB Bean DB 9. EJB container The EJB container is the place where Java beans „live“ (are hosted). EJB containers are part of the J2EE (application) server which in turn runs in a JVM. EJB daemon which hosts and services EJB beans. Java application server (Examples: Glassfish, JBoss, IBM Websphere, Oracle Weblogic, Apache Geronimo etc.). JVM
  • 21. © Peter R. Egli 2015 21/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 10. Comparison of EJB with other DOT technology like CORBA EJB and CORBA are similar in many ways. There are some notable differences: Feature CORBA EJB Middleware type CORBA = explicit middleware: Client accesses directly the CORBA API EJB = implicit middleware: Client is shielded from the specifics of the EJB-API Language support CORBA = language-neutral (there are various IDL-language-mappings) EJB = Java only Server-side integration CORBA-POA-model is more flexible CORBA leaves it open how to integrate the server-side objects EJBs are tightly integrated into the EJB- container Interface specification IDL (language-neutral syntax and semantics) Interface specification = Java interface Configuration CORBA does not provide interfaces or concepts for configuration EJB 1 and 2: EJB configuration is placed into a deployment descriptor (XML file) EJB 3: Configuration through annotations CORBA: Common Object Request Broker Architecture DOT: Distributed Object Technology IDL: Interface Description Language POA: Portable Object Adaptor
  • 22. © Peter R. Egli 2015 22/22 Rev. 1.80 EJB – Enterprise Java Beans indigoo.com 11. When to use EJB EJB may be overkill in many applications and alternatives (namely Spring) may often be better suited to fulfill a certain task. The following table compares EJB and Spring: Feature EJB Spring Framework Multi-tiered application (distributed application, remoting) Yes (client and server tier, separation of client and business logic is one of the main goals of EJB). Container-managed remote method calls. Remoting through RMI, HTTPInvoker, JAX-RPC web services, JMS. Standard platform, vendor independence Yes (defined by JCP, supported by all major JEE vendors) No (vendor = SpringSource) Application server JEE application server needed. Spring comes with its own (lightweight) object container. Dependency injection Yes (EJB 3.0), but less powerful than Spring (only JNDI-objects, not POJOs). Yes (even between POJOs). Distributed transactions Yes (must use JTA). Yes (JTA and other transaction managers possible). Persistence / ORM Programmatic bean-managed persistence. Vendor specific ORM. Integration with different persistence frameworks (Hibernate etc.) JEE: Java Enterprise Edition JCP: Java Community Process JMS: Java Messaging Service JTA: Java Transaction API ORM: Object Relational Mapper POJO: Plain Old Java Object