java ee ejb applications
DESCRIPTION
This slide covers building EJB applications. Presented by DevelopIntelligence the leader in customized developer training.TRANSCRIPT
![Page 1: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/1.jpg)
Java EE EJB Applications
Building EJB Applications
![Page 2: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/2.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 2
Presentation Topics
In this presentation, we will discuss:
Building the Enterprise
History of EJB Development
EJB Development Concepts
Session Beans
Message Driven Beans
Persistent Entities
Interceptors
Timer Service
Transactions
Security
![Page 3: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/3.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 3
Objectives
When we are done, you should be able to:
Explain benefits of EJB3 over EJB 2
Understand Java EE’s layered architecture and the functions provided in its layers
Describe three kinds of enterprise beans and their uses
Explain pros and cons of stateful vs. stateless session beans
Compare the development process under Java EE 5 versus J2EE
![Page 4: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/4.jpg)
Building the Enterprise
The Tools of Java Enterprise Edition
![Page 5: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/5.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 5
Presentation Topics
In this section, we will discuss:
Overview of Java EE
Java EE Platform
Java EE Development
![Page 6: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/6.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 6
Objectives
When we are done, you should be able to:
Describe how Java EE and Java SE fit together
List 3 technologies found in Java EE
Describe the basic development lifecycle for a Java EE application
![Page 7: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/7.jpg)
Overview of Java EE
A High-level Overview
![Page 8: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/8.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 8
What is Java EE?
A community-defined extension to the Java SE platform
An enterprise information systems platform
A standard architecture definingAn application programming modelAn application execution platform
![Page 9: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/9.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 9
History of Java EE
Java EE was not an “intent” of the original Java design
Java EE was more “evolutionary” than revolutionary
![Page 10: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/10.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 10
Motivations for Java EE
Provide an easy to use framework to build middle-ware applications
Provide a managed execution environment
![Page 11: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/11.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 11
Industry Support
Originally, Java EE was developed and maintained by Sun
Today, Java EE is governed by the Java Community Process (JCP)
Current focus of Java EE community is simplification
Latest release is Java EE 6 (12/2009)
![Page 12: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/12.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 12
Application Programming Model
Programming model adopts a “managed” modelDesign and implementation
Lifecycle
Transactions and security
Deployment
Application implementation adheres to modelBuilt around abstracted interactions with Java EE execution environment
Interactions provided through Java EE platform APIs
![Page 13: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/13.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 13
Application Execution Platform
Provides a “managed” execution environment Java EE compliant applications
Execution environment is a realized implementation of the Java EE APIs
Relies on the Java SE platform
![Page 14: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/14.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 14
Java EE Platform Diagram
Java SE Platform
Solaris Linux Windows Mac OS X
Java EE API Implementations
Java EE APIs
App
Java EE Application Server
App App App
![Page 15: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/15.jpg)
Java EE Platform
An Application Development Perspective
![Page 16: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/16.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 16
Java EE Platform
Java EE Platform supports three primary application types
Web applications
EJB applications
Enterprise applications
Each application type has its own set of characteristics
Applications execute in “domain” relevant containers
![Page 17: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/17.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 17
Java EE Containers
Java EE defines two primary types of containers:Web containers - managed environment for web applications
EJB containers - managed environment for EJB applications
A certified Java EE application server must provide an implementation for both types of containers
![Page 18: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/18.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 18
Java EE Technologies
Enterprise Container Boundary
![Page 19: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/19.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 19
Java EE Server Technologies
WORA is a design motivation to justify use of Java EE
There are two ways to create WORA:1. Define an application programming model and its
supporting technologies
2. Define a set of services to support the programming model
Java EE defines the supporting services as “Standard Services”
![Page 20: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/20.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 20
Java EE Standard Services
Communication
XML Processing
Transactions
Messaging
Management
Database
Naming and Directory
Security
Web Services
Legacy systems
Deployment
![Page 21: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/21.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 21
Container Technologies
Remaining technologies defined in Java EE specification address application development
Technologies are associated with an application-centric container
Relatively clean separation of technologies between container types
Technology sharing is at the Standard Services level
Technologies may exist as “standard” implementations or vendor implementations
![Page 22: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/22.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 22
Web Container Technologies
Web containers provide “robust” web application environments
A web container supports the following technologies:
Servlets
Filters
Java Server Pages (JSP)
JSP Standard Tag Library (JSTL)
Java Server Faces (JSF)
![Page 23: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/23.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 23
EJB Container Technologies
EJB containers provide state, workflow, and persistent application environments
An EJB container supports the following technologies:
Session Beans (SB)
Entity Beans (EB)*
Message Driven Beans (MDB)
* Deprecated in EJB3.x
![Page 24: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/24.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 24
Java EE Application Diagram
JSP
Servlet
JSF
JSTL
SB
MDB
EB
WSI
Standard Services
Java SE Platform
Web Container& Technologies
EJB Container& Technologies
Enterprise Container
Java EE Platform
![Page 25: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/25.jpg)
Java EE Development
![Page 26: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/26.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 26
Java EE ApplicationsBuilt using components, modules, and deployment units
The Java EE application structure provides:Functional encapsulationReusabilitySimplified configurationTransportability
Modular-oriented development encouragesGood Object oriented designSeparation of functional concernSeparation of development concern
Has been considered the “struggle” of Java EE
![Page 27: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/27.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 27
Components and Modules
Components enable application development
Modules represent basic unit of a Java EE application
![Page 28: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/28.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 28
Deployment Units
Java EE applications may consist ofA Single deployment unitMultiple deployment units packaged together as a single deployment unit
Deployment units are the standard way to package and deploy Java EE applications
Java EE defines three deployment units:Web Application Archive (WAR)Java Archive (JAR) Enterprise Application Archive (EAR)
![Page 29: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/29.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 29
WAR – Web Module
![Page 30: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/30.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 30
EJB JAR – EJB Module
![Page 31: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/31.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 31
EAR
![Page 32: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/32.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 32
Java EE Development Process
Creation Assembly Deployment
![Page 33: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/33.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 33
Java EE Development Roles
Java EE application development process requires broad range of competencies
To simplify “jack of all trades” skill requirement, Java EE specification defines set of Platform Roles
Not all platform roles are realized within a software development organization
![Page 34: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/34.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 34
Java EE Platform Roles
Java EE Product Provider
Application component provider
Application assembler
Deployer
System Administrator
Tool Provider
System Component Provider
:: QUESTION ::Which of these roles exist within your organization?
![Page 35: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/35.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 35
Summary
Java EE platform defines an application programming model and an execution environment
Java EE platform enables WORA in middleware
Java EE is comprised of Standard Services, Web Technologies, and EJB Technologies
Java EE uses a deployment unit strategy for application packaging and deployment
![Page 36: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/36.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 36
Resources
WebJava EE Tutorial http://docs.oracle.com/javaee/6/tutorial/doc/
Java EE Specificationhttp://jcp.org/aboutJava/communityprocess/pfd/jsr244/
Books“Head First Servlets and JSP” : ISBN 0596005407
“Head First EJB” : ISBN 0596005717
“EJB3 in Action” : ISBN 1933988347
![Page 37: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/37.jpg)
History of EJB
![Page 38: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/38.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 38
Objectives
When we are done, you should be able to:
Explain benefits of EJB 3 over EJB 2
Understand Java EE’s layered architecture and the functions provided in its layers
Describe three kinds of enterprise beans and their uses
Explain pros and cons of stateful vs. stateless session beans
Compare the development process under Java EE 5 versus J2EE
![Page 39: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/39.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 39
Motivations for EJB
Prior to EJB specification, reusable middleware was hard to create
EJB specification borrowed many concepts defined by servlets and adapted them to address middle tier
![Page 40: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/40.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 40
What is EJB?
An Enterprise Java Bean is:A reusable component A Java objectAn encapsulation of enterprise business logic and dataExecuted in a Containers
EJB Containers provide:PoolingTransaction Management SecurityNaming and DirectoryConfigurationEtc.
![Page 41: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/41.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 41
History of EJB
Four major releases of EJB specificationEJB 1.0 - all Java based; cumbersome
EJB 2.0 - Java based + xml; tedious
EJB 2.1 - Java based + xml; slightly easier
EJB 3.0 - Java based + xml + annotations; easier
EJB 3.1 – Java based + xml + annotations; easiest
Most applications “legacy” EJB applications are built using EJB 2.X specification
![Page 42: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/42.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 42
EJB 2.X Strengths
More capabilities than EJB 1.0 spec
Clean separation of business services
Persistence supports usable object/relational mapping
Message oriented middleware capabilities
Performance improvements
![Page 43: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/43.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 43
EJB 2.X Weaknesses
Cumbersome programming model
Complex deployment model
Mediocre object/relational mapping
![Page 44: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/44.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 44
EJB 3.x Strengths
Simplified programming model
Simplified deployment model
More robust persistence structure
![Page 45: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/45.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 45
EJB 3.x Weaknesses
Yet another new programming model
Aspect Oriented Programming and dependency injection not as robust as other frameworks
Limited vendor support
![Page 46: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/46.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 46
New Features in EJB3.0
Annotations instead of Deployment Descriptors
Callback Methods and Listeners
Interceptors
Dependency Injection
![Page 47: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/47.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 47
New Features of EJB3.1
Simplified EJB development – no interfaces
Singleton Session Beans
Enhanced Timer Service
Application Profiles
![Page 48: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/48.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 48
EJBs within Java EE
Java client
.NET client
Desktop
HTML client
Java client
Browser
Delphi client
.NET client
Server
Client Tier Web Container
JSP
Servlet
JSF
WS
JSTL
CTL
Beans
POJO
Beans
POJO
EJB Container
EJB
EJB
EJB
EJB
JMS
JCA
JNDI
JDBC
DB
MessageSystem
DirectoryServer
CustomSystem
EIS Tier
![Page 49: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/49.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 49
Summary
EJB3.x simplifies the development model
There are three kinds of EJBsSession Beans
Message Driven Beans
“Entity” Beans
![Page 50: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/50.jpg)
EJB Development Concepts
![Page 51: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/51.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 51
Presentation Topics
In this section, we will discuss:
Type of Enterprise Java Beans
EJB Client Access Modes
EJB Instance Management
EJB Development Elements
![Page 52: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/52.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 52
Objectives
When we are done, you should be able to:
Describe the three types of EJBs
Identify the three types of client access
Understand the elements involved in building an EJB
![Page 53: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/53.jpg)
Types of Enterprise Java Beans
![Page 54: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/54.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 54
Enterprise Java Beans
Designed to provide:Database abstraction
Reusable business logic components
Workflow components
Transportable implementation
Exists as APIs in javax.ejb
![Page 55: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/55.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 55
Types of Enterprise Java Beans
Three main categoriesBusiness logic – javax.ejb.SessionBeanWorkflow logic – javax.ejb.MessageDrivenBeanPersistence logic - JPA Entity
Classifications specified with through:Interface – 2.x style
Annotations – 3.x style
![Page 56: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/56.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 56
Session Beans
Reusable business logic components
Can be used to manage state across client interactions
Three typesStateless (SLSB)
Stateful (SFSB)
Singleton (SSB) [3.1 or higher]*
* Similar to Jboss @Service
![Page 57: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/57.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 57
Message Driven Beans [MDB]
Reusable workflow logic componentsRely on Java Messaging System
Support transactions
Similar to Stateless Session BeansMDBs have no client-oriented state
No distinction across client or bean
Different from Stateless Session BeansNo direct client access
Asynchronous interactions
![Page 58: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/58.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 58
Entity Beans
Persistence model for EJB
Focused on automatic persistence through object model
Three primary types:Bean Managed Persistence (BMP)
Container Managed Persistence (CMP)
JPA Entities [EE 5 and higher]
![Page 59: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/59.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 59
Category-Related Annotations
Bean Type Annotation
Session Bean @Stateless@Stateful@Singleton*
Message Driven @MessageDriven
JPA Entities @Entity**
@EntityManager**
* EJB3.1
** Part of JPA (javax.persistence)
![Page 60: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/60.jpg)
EJB Client Access Modes
![Page 61: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/61.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 61
Types of EJB Access Modes
Three client access modes: Local client-access
Remote client-access
Web service client-access
Client-access modes specified byInterface – 2.x style
Annotations – 3.x style
![Page 62: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/62.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 62
EJB Classifications [cont.]
Local beans are accessible Only by other EJBs in the same context (module)
All categories of beans can be local
Remote beans are accessibleBy EJBs in the same context and outside the context (modules)
By other “objects” outside of the container
Only Session Beans can be remote
Web Service end-pointsTranslate SB and MDBs into web-services
Container manages WSDL/SOAP mappings
REST/JAXB mappings
![Page 63: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/63.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 63
Access Mode Annotations
Bean Type Annotation
Session Bean @Local@Remote@LocalBean*
@WebService@Path*
Message Driven @WebService
JPA Entities N/A
* EJB3.1
![Page 64: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/64.jpg)
EJB Instance Management
![Page 65: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/65.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 65
EJB Management Modes
Two management modes:Container-managed
Bean-managed
Management-modes specified by Annotations
![Page 66: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/66.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 66
EJB Classifications
In container-managed, container manages:Transactions
Roles
Security
Persistence and Entities (PersistenceContext)
In bean-managed, bean manages:Transactions
Persistence
![Page 67: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/67.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 67
Management Mode Annotations
Bean Type Annotation
Transactions @TransactionManagement(CONTAINER)@TransactionManagement(BEAN)
Security @RunAs()@RolesAllowed
![Page 68: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/68.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 68
EJB Access
EJB Instance access is controlled by container
There is no direct client access
“proxy”
EJB
Client
Container
![Page 69: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/69.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 69
EJB Instantiation
Instantiation of EJB is handled by container
Container determines when and “how many”
Uses standard instantiation mechanism (public no argument constructor)
Creates an EJBObect (proxy)
Creates an EJB instance (actual bean instance)
![Page 70: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/70.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 70
EJB Lifecycle
Lifecycle of an instance is managed by container
Lifecycle phases:Does Not exist – no bean instance in memory
Post-construct / Not-ready – bean instance exists but isn’t ready for client interaction
Ready – exists, initialized, and ready for interactions
Pre-removal – bean instance is about to be removed
Lifecycle transition notifications are handled through call-backs
![Page 71: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/71.jpg)
EJB Development Elements
Developing an EJB
![Page 72: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/72.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 72
Elements of an EJB 2.x
Each EJB is implemented by:
Home interface*
Business interface
Bean Implementation
XML Filesejb-jar.xmlapp-server-xml.xml
*Deprecated in EJB3.0
![Page 73: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/73.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 73
Elements of an EJB 3.x
Each EJB is implemented by:
Business interface*
Bean Implementation
XML Fileejb-jar.xml*
*Optional in EJB3.1
![Page 74: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/74.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 74
Elements of EJB [cont.]
Each EJB is described by meta-dataTells container how to manage EJB
Annotation-based in Java code
Or, XML based*
ejb-jar.xmlserver specific xml file
Key description elementsEJB name
EJB JNDI name
EJB type
Security and transaction information
*Optional in EJB3.0
![Page 75: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/75.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 75
EJB Development Cycle
1. Write client2. Write EJB
1. Interfaces (depending on version)2. Implementation
3. Define configuration1. Annotate2. Or create descriptors
4. Compile5. Package 6. Generate client access classes7. Deploy8. Test9. Repeat 2 - 8 until satisfied
![Page 76: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/76.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 76
EJB Deployment
EJBs have their own deployment model
Each deployment executes in its own context
![Page 77: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/77.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 77
Summary
There are 3 types of EJBS:Session Beans
Message driven Beans
JPA Entities
There are 3 client access modesRemote
Local
Local no interface
EJBs are implemented using:Business interface
Bean implementation
EJBs are configured using meta-data
![Page 78: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/78.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 78
LAB: Setup Environment
In this lab, you will:
Setup your development environment:Install JBoss
Install Eclipse
Configure EclipseAdd JBoss as an app server
Create an EJB Project
![Page 79: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/79.jpg)
Session Beans
Enterprise Business Logic
![Page 80: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/80.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 80
Presentation Topics
In this section, we will discuss:
Session Bean Concepts
Developing a Session Bean
Developing a Session Bean Client
![Page 81: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/81.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 81
Objectives
When we are done, you should be able to:
Describe the 3 types of session beans
Implement a simple stateless session bean
Package and deploy a simple session bean
Implement a simple stateful session bean
Package and deploy a simple stateful bean
Explain pros and cons of stateful vs. stateless vs. singleton session beans
![Page 82: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/82.jpg)
Session Bean Concepts
![Page 83: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/83.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 83
Review of Session Beans Concepts
Reusable business logic components
Can be used to manage state across client interactions
Three typesStateless (SLSB)
Stateful (SFSB)
Singleton (SSB)
![Page 84: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/84.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 84
Choosing a Session Bean
Use a Session bean if:Only one client has access to bean at any given time
State of bean is not persistent
Bean represents a web service
Use a Stateful Session bean if:Bean state represents client interaction
Bean needs to hold client data across interactions
Bean acts as a client mediator to other beans
Need thread-safe interactions
![Page 85: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/85.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 85
Choosing a Session Bean
Use a Stateless Session bean if:Concerned about performance
No distinction across client or interaction
Need synchronous or asynchronous interactions
Use a Singleton Session bean if:You need only a single instance of the bean in memory
Want to maintain consistency across all client interactions
Need to support concurrent client-access
![Page 86: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/86.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 86
Session Bean Cardinality
@Stateless @Stateful @Singleton
Remote Client Access ✔ ✔ ✔
Local Client Access ✔ ✔ ✔
Concurrent client access
✖ ✖ ✔
Unique per client ✖ ✔ ✖
Client-bean instances Pooled 1:1 Many:1
![Page 87: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/87.jpg)
Developing a Session Bean
![Page 88: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/88.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 88
Developing a Session Bean
Steps to create a session bean
1. Define business logic interface
2. Annotate the business interface
3. Create the session bean, implementing the interface
4. Annotate class defining type
5. Compile, deploy and debug as discussed above
![Page 89: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/89.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 89
Developing Business Interface
Similar to standard Java interfacepublic interface SomeInterfacepublic int getSomeProperty();
Annotated with client-access mode@Local – local (in context of application only)
@LocalBean – local (no interface bean)
@Remote – remote (inside and outside of context)
![Page 90: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/90.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 90
Local Interface
![Page 91: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/91.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 91
Remote Interface
![Page 92: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/92.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 92
Developing a Session Bean
Class requirements similar to JavaBeans:
Must be a top level class
Must be defined as publicCan not be final or abstractMust have a public no-argument constructor that takes no parameters.
Must not define the finalize method
Must implement the methods of the business interface
![Page 93: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/93.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 93
Requirements [cont.]
Similar to implementing a POJOImplement business interface
Define variables and other “internal” methods
Annotate bean with session bean type@Stateless@Stateful@Singleton
![Page 94: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/94.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 94
Local Stateless Bean
![Page 95: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/95.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 95
Remote Stateful Bean Implementation
![Page 96: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/96.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 96
LocalBean Singleton Bean Implementation
![Page 97: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/97.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 97
Summary
There are three types of session beans:Stateless - @StatelessStateful - @StatefulSingleton - @Singleton
Every session bean has:Business interface
Implementation class
Deployment descriptor information
:: QUESTION ::What is the business interface of a LocalBean?
![Page 98: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/98.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 98
LAB
Implement the Three versions of the Calculator
![Page 99: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/99.jpg)
Accessing Session Beans
![Page 100: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/100.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 100
Section Topics
In this section, we will discuss:
Client EJB Interactions
Using JDNI to Access EJBs
Using DI to Access EJBs
Hiding EJBs through a Session Facade
![Page 101: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/101.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 101
Objectives
When we are done, you should be able to:
Be able to write three common types of clients for the various types of session beans
Understand how and when a session bean instance is created
Use JNDI and DI to access a bean
![Page 102: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/102.jpg)
Client-EJB Interactions
![Page 103: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/103.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 103
Quick Review of EJB interactions
EJB instance is created and managed by container
Physical EJB instance is “hidden” to client
Client interactions are performed through a proxy (sometimes called the EJBObject)
![Page 104: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/104.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 104
Client -> EJB Interactions
“proxy”
EJB
Client
Container
![Page 105: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/105.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 105
Client -> EJB Interactions [cont.]
A client for an EJB can be:A Java EE Application Client
A Java EE component (JSP, Servlet, JSF, EJB)
Any Java object
A Java application
Client finds a proxy to interact with an EJB using:
Java Naming and Directory Interfaces (JNDI)
Dependency Injection (DI)
![Page 106: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/106.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 106
Steps for Interacting with an EJB
3 Step Plan:
1. Declare a reference for the EJB’s interface
2. Get the EJB “instance” Using JNDI for remote clients or legacy EJBs
Using DI for local clients or application clients
3. Invoke methods on the interface
![Page 107: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/107.jpg)
Using JNDI to Access EJBs
![Page 108: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/108.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 108
JNDI
Java Naming and Directory Interface is part of Java SEStandard API to interact with naming and directory servicesProvide the ability to look things up in a “registry”Used in enterprise solutions to locate resources such as EJBs, JMS queues or Web Services
JNDI resources are organized in a tree structureAnalogous to the folder structure of a computer’s file system Supports events, lookups, and complex searching against structure
JND
IDS
JND
I IMP
l
App
![Page 109: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/109.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 109
Key JNDI Concepts
Finding an EJB with JNDI uses four key concepts
Context
Initial Context
Path & Name
Search
![Page 110: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/110.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 110
JNDI Contexts
JNDI supports multiple contexts . . .. . . each containing different resources
Local context - an application can obtain access to its resources—EJBs, DataSources, etc.
Remote context - an application can also obtain access to a remote application server’s resources – remote EJBs, etc.
![Page 111: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/111.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 111
javax.naming.Context
Contexts contain name-to-object bindings
Interface representing a naming contextProvides methods for binding, removing, finding, etc.
Also supports environment properties
![Page 112: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/112.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 112
javax.naming.InitialContext
Represents the starting point for naming operations
Could be “root node”
Or, “sub node” in tree
Is an implementation of Context
![Page 113: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/113.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 113
Configuring the InitialContext
Use a default configuration:Context c = new InitialContext();
Programmatically control the configurationHashtable env = new Hashtable(); env.put("java.naming.factory.Initial",”…"); env.put("java.naming.provider.url",”…); Context c = new InitialContext(env);
Externalize the configuration in jndi.properties
![Page 114: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/114.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 114
JBoss Context Properties
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
env.put("java.naming.provider.url", "localhost:1099");
Context ctx = new InitialContext(env);
![Page 115: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/115.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 115
Finding EJB References
Traditionally, EJB containers defined their own “naming” scheme
Varied by app server vendor
Path and entry name could be overridden in xml deployment descriptor
EJB3.1 has global JNDI Naming conventions:
qualified class namejava:global[/<app-name>]/<module-name>/<bean-name>java:app[/<module-name>]/<bean-name> java:module/<bean-name>
![Page 116: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/116.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 116
Global JNDI in JBoss AS
java:global/Working-with-EJB3-Tutorials/EJB3/HelloWorld!com.developintelligence.tutorials.ejb3.HelloWorld
java:global/Working-with-EJB3-Tutorials/EJB3/HelloWorld
java:app/EJB3/HelloWorld!com.developintelligence.tutorials.ejb3.HelloWorld
java:app/EJB3/HelloWorld
java:module/HelloWorld!com.developintelligence.tutorials.ejb3.HelloWorld
java:module/HelloWorld
![Page 117: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/117.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 117
Simple JNDI Client Example
Example represents a “stand alone” Java application
Java application uses JNDI to find a remote bean
Uses the remote reference to invoke operations
![Page 118: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/118.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 118
Viewing JNDI Tree in JBoss 5
http://localhost:8080/jmx-console
Click on:• Service=JNDIView
![Page 119: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/119.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 119
JNDIView
Click on “Invoke”
![Page 120: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/120.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 120
JMX MBean Operation View
Scroll down until you see …
![Page 121: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/121.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 121
Naming View in Jboss 7
http://localhost:9990/console/App.html#naming
![Page 122: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/122.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 122
Simple JNDI Lookup [Glassfish]
![Page 123: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/123.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 123
Simple JNDI Lookup [JBoss]
![Page 124: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/124.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 124
Making the Stand Alone Client Work
There’s some magic to make the client work:
1. Need to bundle the “remote” interfaces of EJB in a Jar (commonly called the ejb-client-jar)
2. Need to define the JNDI properties
3. Need to bundle Java EE jars for things like UserTransaction
: : NOTE : :Some app servers provides an appserver-client.jar
to simplify #2 & #3
![Page 125: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/125.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 125
Simple Servlet JNDI Example
Example represents a servlet “controller”
Java application uses JNDI to find an EJB
Uses the local reference to invoke operations
![Page 126: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/126.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 126
Simple JNDI Servlet [Glassfish]
![Page 127: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/127.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 127
Simple JNDI Servlet [JBoss]
![Page 128: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/128.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 128
LAB: JDNI Client Labs
In this lab you will:
Write a stand-alone client
A Servlet / JSP client
![Page 129: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/129.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 129
Problems with JNDI
Keeping track of:
JNDI path
Bean name
![Page 130: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/130.jpg)
Using DI to Access EJBs
![Page 131: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/131.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 131
Dependency Injection
Java EE 5 introduced support for dependency injection (DI)
Container can automatically “inject” references
Used commonly in context of JNDI
Based on annotations
Dependency injectionSimplifies programming
Makes access to JNDI largely transparent
![Page 132: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/132.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 132
Working with DI
Removes JNDI lookups; DI can be used to inject:@EJB
@Resource, @PersistenceContext, @EntityManager
Container “inserts” references based on defaultsDefault naming schemes, typing, mappingsDefaults can be over-ridden using annotation attributes
DI is supported within:Enterprise Java Beans
Servlets / JSPs / Filters / ManagedBeans
Application Client Container Clients
![Page 133: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/133.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 133
Key DI Annotations
@EJBUsed for DI of an EJB
Can specify additional information such as EJB’s name or beanName or lookup
@ResourceUsed for DI of non-EJBs
Specify things like SessionContext or DataSource
![Page 134: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/134.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 134
Working with EJB Injection
Need to understand defaultsDI attempts to inject based on context
Attempts to resolve EJB reference based type of interface
Can only have one bean bound with that type
Beyond defaultsDI attempts to inject based on context
Attempts to resolve EJB reference based on name
![Page 135: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/135.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 135
In-Container DI [EJB -> Servlet DI]
![Page 136: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/136.jpg)
Hiding EJBs through a Session Facade
![Page 137: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/137.jpg)
137
Façade Pattern Description
Intent: Provide a unified interface to a set of interfaces in
a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Motivation: Structuring a system into subsystems helps
reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. A façade provides a single, simplified interface to the more general facilities of a subsystem.
![Page 138: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/138.jpg)
138
Façade Real World Example
The Facade defines a unified, higher level interface to a subsystem, that makes it easier to use.
Consumers encounter a Facade when ordering from a catalog. The consumer calls one number and speaks with a customer service representative. The customer service representative acts as a Facade, providing an interface to the order fulfillment department, the billing department, and the shipping department.
![Page 139: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/139.jpg)
139
Session Façade
Implementation of the Façade patternApplied to Enterprise JavaBeans
Façade is implemented as a Session Bean
Commonly implemented to create:Single point of entry into complex systems
Hide / abstract functionality
Web service endpoint
Session Façade could be implemented as @Local or @Remote
![Page 140: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/140.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 140
Session Façade Structure
![Page 141: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/141.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 141
Implementing a Session Facade
Simple steps to create a session façade
Create Façade interfaceExposes a limited number of methods
Potentially hides exception and data dependencies
Create Bean implementationAccesses / relies on other beans to perform operations
Generally multiple dependent operations are performed in a single interface method
Translate results / exceptions in client-neutral way
![Page 142: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/142.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 142
Session Façade Implementation
![Page 143: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/143.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 143
Façade Delegate
![Page 144: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/144.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 144
Summary
EJB’s are located using JNDI or DI
JNDI relies on Context, InitialContext, and lookups
DI relies on @EJB annotation
![Page 145: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/145.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 145
LAB: Convert JNDI Client to DI
![Page 146: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/146.jpg)
Advanced Session Bean Concepts
![Page 147: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/147.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 147
Section Topics
In this section, we will discuss:
Session Bean Identity
Session Bean Lifecycle
Implementing Lifecycle Listeners
Advanced Singleton Concepts
![Page 148: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/148.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 148
Objectives
When we are done, you should be able to:
Understand when and how a session bean is created
Understand uniqueness constraints of each bean type
Describe the lifecycle phases for Stateless, Stateful, and Singleton beans
Implement lifecycle call-back handlers
![Page 149: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/149.jpg)
Session Bean Identity
![Page 150: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/150.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 150
Session Bean Identity
Can test session bean references using equals method or hashCode method
Stateless session bean identity - references of the same interface type, for the same stateless session bean, have the same object identity
@EJB MyBean bean1;@EJB MyBean bean2;
if(bean1.equals(bean1)) // trueif(bean1.equals(bean2)) //true
![Page 151: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/151.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 151
Session Bean Identity
Stateful session bean identity – stateful beans have a unique identity that is assigned by the container at the time the object is created.
@EJB MyBean bean1;@EJB MyBean bean2;
if(bean1.equals(bean1)) // trueif(bean1.equals(bean2)) //false
![Page 152: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/152.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 152
Session Bean Identity
Singleton session bean identity – all references of the same type, for the same singleton session bean, have the same object identity
@EJB MyBean bean1;@EJB MyBean bean2;
if(bean1.equals(bean1)) // trueif(bean1.equals(bean2)) //true
![Page 153: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/153.jpg)
Session Bean Lifecycle
![Page 154: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/154.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 154
Session Bean Creation
Session bean instances are created and managed by container
No way for client to directly instantiate a bean
Physical EJB object creation is “hidden” from client
Session beans are created as a result of some other action
Stateless – first client lookup
Stateful – on every client lookup
Singleton – on application load
![Page 155: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/155.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 155
Session Bean Destruction
Session bean instance is destroyed by container
Session bean “destruction” is result of some other action
Stateless – pool clean up or inactivity
Stateful – client inactivity or remove
Singleton – application shutdown
Certain exceptions can cause beans to be destroyed (on initialization)
![Page 156: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/156.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 156
Session Bean Lifecycle
In between creation and destruction, a bean has a lifecycle
Lifecycles are slightly different for each type
Lifecycle allows container and developer to initialize / un-initialize bean at key times
![Page 157: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/157.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 157
5 Key Lifecycle Phases
@PostConstruct - after object is created
@PreDestroy – before object is removed from container
@PrePassivate – before object’s states are preserved
@PostActivate – after object’s states are resurrected
@Remove – after client signals object removal
: : NOTE : : NOT ALL SESSION BEANS GO THROUGH ALL PHASES
![Page 158: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/158.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 158
Stateless Session Beans
Stateless Session Bean Lifecycle
Create instance
@PostConstruct – perform initialization
Process business methods
@PreDestroy – undo initialization
![Page 159: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/159.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 159
Stateful Session Beans
Stateful Session Bean Lifecycle
Create instance
@PostConstruct – perform initialization
Process business methods@PrePassivate – prepare for serialization
@PostActivate – recover from serialization
@Remove – remove ejb from container
@PreDestroy – undo initialization
![Page 160: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/160.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 160
Stateful Session Bean Lifecycle
![Page 161: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/161.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 161
Singleton Session Beans
Singleton Session Bean Lifecycle
Create instance
@PostConstruct – perform initialization
Process business methods
@PreDestroy – undo initialization
![Page 162: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/162.jpg)
Implementing Lifecycle Callback Listeners
![Page 163: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/163.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 163
Lifecycle Callbacks
As an EJB developer, you can receive notification as a bean transitions through each phases
Notification is done through use of a callback method
Callback methods are annotated with the appropriate lifecycle callback annotation
![Page 164: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/164.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 164
Implementing Callback Methods
Annotations are used to identify a callback method
A single method can handle multiple callbacks
Methods can be defined using any access modifier
Callback methods can throw runtime exceptions, but not application exceptions
![Page 165: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/165.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 165
Lifecycle Example
![Page 166: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/166.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 166
LAB: Implement Lifecycle Listeners
1. Add the remaining lifecycle methods to the Stateful bean1. @PrePassivate / @PostActivate2. Make the add method @Remove
2. Add the lifecycle methods to the Singleton and the other stateless bean
![Page 167: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/167.jpg)
Advanced Singleton Concepts
![Page 168: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/168.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 168
Singleton Initialization Strategies
Two types of initialization strategies for Singletons:
Lazy – container chooses when to instantiate bean
Eager Explicitly tell container to create instance at application load
Over-rides default behavior
Implemented using the @Startup annotation
![Page 169: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/169.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 169
@Startup Singleton
![Page 170: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/170.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 170
Ordered Instantiation
Singleton bean instantiation can depend on other beans
Dependencies can be determined by container
Or, explicitly defined by developer
Uses @DependsOn annotation
:: QUESTION ::Which singleton is create first?
![Page 171: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/171.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 171
Summary
Each session bean has a distinct lifecycle
There are 5 lifecycle annotations@PostConstruct@PreDestroy@PrePassivate@PostActivate@Remove
Beans can receive notification of lifecycle transitions through callbacks
![Page 172: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/172.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 172
LAB
Implement two singletons and prove out the @Startup and @DependsOn annotation
![Page 173: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/173.jpg)
Java Message Service
An overview of JMS
![Page 174: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/174.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 174
Section Topics
In this section, we will discuss:
Introduction to Java Messaging Service (JMS)
Working with JMS
Developing a Message Producer
![Page 175: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/175.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 175
Objectives
When we are done, you should be able to:
Understand the advantages of using a messaging system
Describe the 5 key concepts found in messaging systems
Create a message
Publish a message to a queue
![Page 176: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/176.jpg)
Introduction to Java Messaging Service
![Page 177: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/177.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 177
Overview of Messaging Systems
Function as intermediary between end-points of a system
Creates a loosely coupled system
Participants can function independent from one another
Solutions built using message are commonly referred to as MOM (message-oriented-middleware)
![Page 178: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/178.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 178
Key Messaging Concepts
Message – encapsulates data passed during communication
Message Producer – creator of the message
Messaging System – responsible for routing and delivery of message
Destination – delivery endpoint for message
Message Consumer – end recipient of message
![Page 179: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/179.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 179
What is a “Message?”
A collection of data passed in a one way (e.g., queued) communication
A message contains:Header fields: priority, timestamp, etc.
Optional message body
Optional properties
![Page 180: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/180.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 180
What is Java Message Service?
A Java-based messaging system
Robust messaging support:Asynchronous sending and receiving of messages
Decoupled communication between sender, receiver
Robust and transparent delivery mechanisms
Defined as an API - javax.jmsUsed by developers interacting with messaging systems
Implemented by Java EE server vendors
![Page 181: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/181.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 181
Destination Types
Queue TopicPoint-to-point communication Multipoint communication
Publishers emit messages to topics
Subscribers monitor topics of interest
One message is received per message sent
One published message can result in zero to many messages received
Useful for queuing some data that needs to be processed exactly once
Useful when an unknown number of observers may need to monitor the same events
![Page 182: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/182.jpg)
Working with JMS
Creating a Message Producer
![Page 183: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/183.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 183
What is a Message Producer?
Message producers create messages
“Send” messages to destination through messaging system
Can be just about anything:Stand alone Java application
Java Object
Java EE Component
Legacy Systems
![Page 184: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/184.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 184
Key JMS API Concepts
To create a message producer, you need:
Destination
Connection
Session
MessageProducer
Message
![Page 185: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/185.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 185
JMS Destination
javax.jms.Destination General description of a JMS endpoint
Represents provider-specific address
javax.jms.QueueConsidered a DestinationGeneral description of a JMS Queue
javax.jms.TopicConsidered a DestinationGeneral description of a JMS Topic
![Page 186: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/186.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 186
Accessing a JMS Destination
Two choices to access a destination:JNDI Lookup
Dependency Injection
Dependency Injection examples:@Resource(mappedName="jms/Queue") private Queue queue;
@Resource(mappedName="jms/Topic") private Topic topic;
@Resource(mappedName="jms/Queue") private Destination topic;
![Page 187: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/187.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 187
JMS Connection
javax.jms.Connection - physical connection to the underlying JMS implementation
Connections are retrieved through a javax.jms.ConnectionFactory
Two types of connection factories:javax.jms.QueueConnectionFactoryjavax.jms.TopicConnectionFactory
![Page 188: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/188.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 188
Getting a JMS Connection
Get ConnectionFactory 1st @Resource(mappedName="jms/ConnectionFactory") private ConnectionFactory conFactory;
Get connection from factoryConnection con = conFactory.createConnection();
Close connection when donecon.close();
![Page 189: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/189.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 189
JMS Session
A javax.jms.Session
Describes interactions between message producer/consumer and messaging system
Is single-thread context for producing/consuming messages
Created through a Connection
Session session = con.createSession(. . .);
![Page 190: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/190.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 190
Session Configuration
Sessions are configured as part of creation:
createSession(txn-support, acknowledgement)
Sessions support transactionsTransacted (true): messages are not sent until the session closes
Not transacted (false): messages are sent immediately
Session support acknowledgementAUTO_ACKNOWLEDGECLIENT_ACKNOWLEDGEDUPS_OK_ACKNOWLEDGE
![Page 191: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/191.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 191
MessageProducer
javax.jms.MessageProducerAssociated with a specific Destination
Created from SessionMessageProducer producer = session.createProducer(destination);
MessageProducer producer = session.createProducer(queue);
MessageProducer producer = session.createProducer(topic);
Used to send messages to a Destinationproducer.send(message);
![Page 192: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/192.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 192
Message
javax.jms.Message described by:
Header – name-value pairs used by messaging system and message consumer
Properties – name-value pairs to specify additional information
Body – payload of the message
Type - of message being transported
![Page 193: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/193.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 193
Common Message Header Fields
Filed Name Set By
JMSDestination send or publish method
JMSDeliveryMode send or publish method
JMSExpiration send or publish method
JMSPriority send or publish method
JMSMessageID send or publish method
JMSTimestamp send or publish method
JMSCorrelationID Client
JMSReplyTo Client
JMSType Client
JMSRedelivered JMS provider
![Page 194: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/194.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 194
Message Types
Message Type Contents of Body
TextMessage String
MapMessage Name-value-pairs
ByteMessage Stream of bytes
StreamMessage Stream of Java “primitives”
ObjectMessage Serializable object
TextMessage message = session.createTextMessage(); message.setText(“Hello Messaging World”); producer.send(message);
![Page 195: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/195.jpg)
Developing a Message Producer
![Page 196: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/196.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 196
Creating a Message Producer
Steps to create a Message Producer
1. Create Destination in Messaging System
2. Create Client1. Get Destination2. Create Connection3. Create Session4. Create MessageProducer5. Create Message6. Send Message7. Close Session8. Close Connection
![Page 197: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/197.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 197
Message Producer [Glassfish]
![Page 198: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/198.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 198
Message Producer [cont.]
![Page 199: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/199.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 199
JMS in JBoss
Connection factory Single connection factory
Can use with default destinations
Or, user created destinations
Accessed using JNDI key ConnectionFactory
DestinationsCreate using admin-console
Specify type (template), name, and JNDI path
![Page 200: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/200.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 200
Message Producing in JBoss
Connection Factory is:
@Resource(mappedName="ConnectionFactory”)ConnectionFactory conFactory;
Queue is:
@Resource(mappedName="HelloWorldQueue”)Queue queue;
![Page 201: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/201.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 201
Summary
6 Key Concepts in JMS Solutions
Message Producer
Message System
Message Consumer
Message
Destination
Session
![Page 202: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/202.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 202
LAB
Modify the Bean:
For every lifecycle transition that occurs:Send a text message the Queue
Specify in the message a property called “operation”
With a value “lifecycle”
For every other operation:Send a text message the Queue
Specify in the message a property called “operation”
With a value with the name of the operation
![Page 203: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/203.jpg)
Message Driven Beans
![Page 204: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/204.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 204
Section Topics
In this section, we will cover:
Developing a Message Driven Bean
Configuring a MDB
Message Handling
![Page 205: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/205.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 205
Objectives
When we are done, you should be able to:
Identify two ways to implement an MDB
Provide the appropriate configuration for an MDB using a Queue
Filter out messages using a Selector
![Page 206: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/206.jpg)
Developing a Message Driven Bean
![Page 207: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/207.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 207
Message Driven Beans [MDB]
Reusable workflow logic componentsRely on Java Messaging System
Support transactions
Similar to Stateless Session BeansMDBs have no client state data
No distinction across client or bean
Different from Stateless Session BeansNo direct client access
Invoked through message notification
Asynchronous interactions
![Page 208: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/208.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 208
MDB Lifecyle
Very simple lifecycleCreate
@PostConstruct, @PreDestroyonMessage – when a message is delivered
![Page 209: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/209.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 209
Developing an MDB
Set up messaging in Java EE application server
Develop the MDB class
Configure the MDB
Process the Message
Compile, deploy and debug as discussed above
![Page 210: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/210.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 210
Develop the MDB
Two ways to implement an MDB
1. Inheritence-based1. MessageListener interface
2. implement onMessage method
2. Annotation-based1. Flexible implementation
2. Declare with method handles the message
![Page 211: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/211.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 211
Inheritance Based Example
import javax.ejb.*;import javax.jms.*;
@MessageDrivenpublic class MyMDB implements MessageListener {
public void onMessage(Message msg) { System.out.println("Got message!"); }
}
![Page 212: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/212.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 212
Annotation Based Example
import javax.ejb.*;import javax.jms.*;
@MessageDriven(messageListenerInterface= javax.jms.MessageListener.class)
public class MyMDB { public void onMessage(Message inMessage) { System.out.println("Got message!"); }}
![Page 213: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/213.jpg)
Configuring a MDB
![Page 214: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/214.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 214
MDB Configuration
Creating an MDB with @MessageDriven is not enough
Need to configure the MDB with:Destination information
Message acknowledgement
Message selection
![Page 215: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/215.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 215
Destination Configuration
Simple configuration@MessageDriven(mappedName = "jms/HelloMDBQueue”)
Complete configuration@MessageDriven(activationConfig ={ @ActivationConfigProperty (propertyName="destination", propertyValue = "jms/HelloMDBQueue"), @ActivationConfigProperty (propertyName="destinationType”, propertyValue = "javax.jms.Queue") } )
![Page 216: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/216.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 216
Simple MDB Configuration Example
![Page 217: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/217.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 217
Traditional MDB Configuration Example
![Page 218: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/218.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 218
LAB
Build a MDBAssociate it with the HelloWorldQueue
Implement the onMessage method
Print off each message that’s received
Run the “test” servlet
![Page 219: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/219.jpg)
Message Handling
![Page 220: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/220.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 220
Message Filtering
MDBs are not associated with a specific type of Message
Can receive message sent to associated DestinationNeed to filter out unwanted messages
Simple filtering performed using instanceof
![Page 221: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/221.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 221
Advanced Filtering
Advanced, delivery-time filtering can be performed by container
Only “matching” messages will be delivered to MDB
Matching based on headers and properties, not JMS Message type
Matching criteria defined as a Message SelectorDefined in the activationConfig attribute of @MessageDrivenDefined as an @ActivationConfigProperty named messageSelector
![Page 222: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/222.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 222
MDB Message Selector Example
![Page 223: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/223.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 223
Message Selectors
Defined as part of the activation configuration information
Use SQL-like syntax to specify criteriaWork with message headers
And developer created properties
Defined in terms of:Identifiers – part of expression being compared
Literals – hard-coded expression values
Comparison operators – comparison logic
![Page 224: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/224.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 224
Message Selector Identifiers
Potential identifiers for a message selector
JMS HeadersJMSDeliveryModeJMSPriorityJMSMessageIDJMSTimestampJMSCorrelationIDJMSType
JMS PropertiessetStringProperty, setBooleanPropertysetIntProperty, etc.
![Page 225: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/225.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 225
Message Selector Literals
Define literal values
String Enclosed in single-quotes
Apostrophe are delimited with a single-quote ‘DevelopIntelligence’’s’
Booleans – true or false
NumericsWhole numbers: 700, -743
Real number: 3.145, 9.82
Scientific: 3.5E6
![Page 226: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/226.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 226
Message Selector Comparison Operators
Common comparison operators:Algebraic comparison operatorsLIKE operatorBETWEEN operatorIN operatorNOT operatorIS NULL operator
![Page 227: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/227.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 227
Message Selector Examples
Only “add” operationsActivationConfigProperty(propertyName="messageSelector",
propertyValue = "Operation = 'add’”)}
Only “multiply” operations with a result equal to 100ActivationConfigProperty(propertyName="messageSelector",
propertyValue = "Operation = ’multiply’ AND Result = 100”)}
Only “multiply” or “add” operations with a result greater than 100ActivationConfigProperty(propertyName="messageSelector",
propertyValue = "Operation IN(’multiply’,’add’) AND Result >
100”)}
![Page 228: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/228.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 228
MDB Message Selector Example II
![Page 229: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/229.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 229
Message Acknowledgement Configuration
Default – AUTO_ACKNOWLEDGE
Non-Default Example:@MessageDriven( activationConfig={ @ActivationConfigProperty( propertyName=”acknowledgeMode”, propertyValue=“Dups-ok-acknowledge) } )
![Page 230: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/230.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 230
Summary
Message Driven Beans have a distinct lifecycle
Simplest way to implement an MDB@MessageDriven(mappedName=“queue/Example”)implements MessageListeneronMessage(Message m)
![Page 231: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/231.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 231
LAB:
Modify the MDB to only handle lifecycle messages
![Page 232: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/232.jpg)
Advanced EJB Development
![Page 233: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/233.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 233
Section Topics
In this section, we will cover:
Managing Singleton Concurrency
Aspect-Oriented Programming with Interceptors
EJB Transactions
EJB Security
Web Service Development using EJBs
![Page 234: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/234.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 234
Objectives
When we’re done, you should be able to:
Understand how to manage concurrency in a Singleton EJB
Apply AOP to EJBs using interceptors
Identify the two types of transaction strategies
Identify a security mechanism supported in EJB
Indentify how to transform an EJB into a Web Service
![Page 235: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/235.jpg)
Singleton Concurrency Management
![Page 236: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/236.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 236
Concurrent Access in Singletons
Singletons support concurrent access
Unprotected concurrent access can cause corruption
Concurrency can be managed by:CONTAINER – default
BEAN – explicit (developer)
@ConcurrencyManagement(ConcurrencyMangagementType.BEAN)
![Page 237: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/237.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 237
Controlling Concurrency with Locks
Container uses an Object-locking strategy Sometimes referred to as an object monitor
Conceptually similar to java.util.Lock mechanism
Default behavior is to lock object for all method operations
Obtaining a lock can be expensive
Can cause unnecessary “waiting” in caller
By default, bean access isn’t really concurrent
Can over-ride default behavior using @Lock
![Page 238: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/238.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 238
@Lock
Used to define method-level locking strategiesBean can have a concurrent-safe methods
Or, concurrent unsafe methods
Or, a mixture of the two
Locking strategy is applied using:@LOCK(LockType.READ) – supports concurrency
@LOCK(LockType.WRITE) – does not support concurrency
Locking strategy can be applied to:Class – defines general locking strategy for all methods
Method – defines specific locking strategy for method
![Page 239: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/239.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 239
Singleton Concurrency Example
![Page 240: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/240.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 240
Controlling Dead Lock
Deadlock - is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does
Deadlock can occur in every concurrent system
In singletons, may be caused by long-running operations
Manage “wait-times” using
![Page 241: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/241.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 241
Lab: Apply Locking to a Singleton
![Page 242: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/242.jpg)
Aspect-Oriented Programming with Interceptors
![Page 243: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/243.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 243
Aspect Oriented Programming
Definition: is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns
Common cross-cutting concerns:Logging, Security
Validation, Transformation
Advantages of AOPReusability
Separation of Concern
Consistency
![Page 244: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/244.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 244
What is an Interceptor?
Interceptor functionality enables Aspect-oriented programming within Java EE
Interceptors can be associated with methods or an entire class
Interceptors “intercept” interactions with beanCan forward the call to the bean
Can process the call directly
Can process and forward the call
![Page 245: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/245.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 245
Interceptor
“proxy”
EJB
Client
Container
![Page 246: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/246.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 246
Interceptor Characteristics
Interceptors have:
Same lifecycle as the associated bean
Their own context, known as an InvocationContext
![Page 247: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/247.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 247
Types of EJB Interceptors
3 Types of Interceptors
1. Business Method level interceptorsApply to a single method in a class
Or, apply to all methods in a call
2. Lifecycle Callback Interceptors
3. Timer Timeout Interceptors
![Page 248: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/248.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 248
Implementing a Business Method Interceptor [in EJB]
You can implement an interceptor method within an EJB
Simplest way to setup AOP in EJB
But, the concern is no longer reusable
And, you can only have one per class
@AroundInvokepublic Object anyMethodName(InvocationContext ic) throws Exception
![Page 249: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/249.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 249
Refactoring to an Interceptor
If every method in an EJB has code that performs same concern. . .
Consider refactoring to an internal interceptorMakes code cleaner
Potentially easier to maintain
Higher consistency of execution
![Page 250: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/250.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 250
Redundant Concern
![Page 251: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/251.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 251
Refactored Concern
![Page 252: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/252.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 252
Refactored Concern [cont.]
![Page 253: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/253.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 253
Externalizing the Concern
Better approach is to externalize the concern
Create a class that is an InterceptorDeclare a Java technology class
Include a public no-arg constructor
Declare an interceptor method class@AroundInvoke public Object methName(InvocationContext ic) throws Exception { . . }
Invokes the InvocationContext object’s proceed return ic.proceed();
Associate interceptor with EJB
![Page 254: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/254.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 254
Associating an Interceptor
With a class:@Stateless @Interceptors(MyInterceptor.class) public class MyBean { . . . }
With a specific method@Stateless public class MyBean { @Interceptors(MyInterceptor.class)
public void someMeth() { . . . }}
![Page 255: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/255.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 255
EJB with Interceptor Example
![Page 256: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/256.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 256
Interceptor Class
![Page 257: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/257.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 257
Implementing Other Interceptors
Lifecycle Interceptors
Timeout Interceptors
throws Exception {}
![Page 258: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/258.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 258
Interceptor Chaining
More than one inceptor can be applied to a bean or a method
This creates an interceptors chain
Class-level interceptors are executed first, then method level interceptors
![Page 259: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/259.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 259
Default Interceptors
May want to configure cross-cutting concerns at application level
More flexibility
Externalized from code
Default interceptors are configured in ejb-jar.xmlCan bind an interceptor to all beans or a single bean
Can specify ordering and override other interceptors
![Page 260: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/260.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 260
ejb-jar.xml Example
![Page 261: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/261.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 261
LogInterceptor Example
![Page 262: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/262.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 262
LAB
Create a log interceptor, defining it as a default interceptor for all ejbs deployed in the application
![Page 263: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/263.jpg)
Transactions
![Page 264: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/264.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 264
Overview of Transactions
Purpose of transactions:Treat multiple operations as if they were one
All succeed together or fail together
ACID of transactions:Automicity – all operations succeed or none do
Consistency – system will be consistent before and after request
Isolation – txns are not seen outside of their scope until completed
Durability – once a txn successfully completes, client must commit to its changes
![Page 265: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/265.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 265
Transactions and Java EE
Two primary types of transaction management:
Container managedContainer responsible for creating, committing, and rolling back txns
Completely hidden from developer
Bean managedDeveloper responsible for creating, committing, and rolling back txns
Developer must have good understanding of Transaction API
![Page 266: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/266.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 266
Java EE Transaction Management
Transaction management configuration performed using either:
Annotations
XML file
Annotation-based @TransactionManagementApplied to the bean class
Default is container-managed
Strategy defined by TransactionManagementType
@TransactionManagement(TransactionManagementType.BEAN)@TransactionManagement(TransactionManagementType.CONTAINER)
![Page 267: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/267.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 267
CMT Transactional EJB
@TransactionManagement(CONTAINER)public class MyEJB implements MyI { . . . public void doSomething(){ . . . } . . . }
![Page 268: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/268.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 268
BMT Transactional EJB
@TransactionManagement(BEAN)public class MyEJB implements MyI { . . . public void doSomething(){ . . . } . . . }
![Page 269: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/269.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 269
Transaction Management Attributes
6 different settings relating to transaction needs:
NOT_SUPPORTED – method can not operate correctly within a txn; container possibly suspends current txn when performing operation
SUPPORTS – method can operate correctly within a txn
REQUIRED – method requires txn, but doesn’t need to be new
![Page 270: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/270.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 270
Transaction Management Attributes
6 different settings relating to transaction needs:
REQUIRES_NEW – method requires txn, creates new txn, possibly suspending current txn until operation completes
MANDATORY – method can only operate within txn; if method is invoked without txn, an exception is generated
NEVER – method can not operate within txn; if method is invoked within txn, an exception is generated
![Page 271: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/271.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 271
TransactionAttributes
Defined in TransactionAttributes
Inform container about a bean’s transaction requirements
Can be applied to an entire bean or a set of methods or both
![Page 272: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/272.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 272
EJB Attribute Support
Attribute Stateless Stateful Singleton MDB
NOT_SUPPORTED ✔ ✔ ✔ ✔
REQUIRED ✔ ✔ ✔ ✔
REQUIRES_NEW ✔ ✔ ✔ ✖
SUPPORTS ✔ ✔ ✔ ✖
MANDATORY ✔ ✔ ✔ ✖
NEVER ✔ ✔ ✔ ✖
![Page 273: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/273.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 273
CMT Transactional EJB
All methods – REQUIRED
@TransactionAttribute(REQUIRED)public class MyEJB implements MyI { . . . public void doSomething() { . . . } . . . }
![Page 274: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/274.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 274
CMT Transactional EJB
Single method – REQUIRED
public class MyEJB implements MyI { . . . @TransactionAttribute(REQUIRED) public void doSomething() { . . . } . . . }
![Page 275: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/275.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 275
Managing Transactions
Only BMT bean’s have direct access to txn
Hidden from CMT beansCMT can flag a txn for rollback using EJBContext’s setRollbackOnly method
Or determine if txn is going to be rolled-back using getRollbackOnly
Applications can be configured to roll back a txn automatically
![Page 276: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/276.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 276
LAB
Add transaction REQUIRED TransactionAttribute annotation to sayHello remote bean
Run servletShould fail
![Page 277: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/277.jpg)
Security
![Page 278: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/278.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 278
Security Terms
Identity – who you are
Authentication – validating you are you who say you are
Authorization – validating you have permission to take action
![Page 279: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/279.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 279
Java EE & Security
Identity – can be specified as security credential
Authentication – handled by server or 3rd party
Authorization – focus of EJB security support
![Page 280: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/280.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 280
JSR250 Security Annotations
![Page 281: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/281.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 281
Configuring Authorization
Declares roles that could interact with bean@DeclareRoles( . . . )public class MyBean implements MyI { . . . }
Identify roles are allowed to interact with bean@RolesAllowed(ADMIN)public class MyBean implements MyI { . . . }
Or identify roles allowed to interact with method@RolesAllowed(ADMIN)public void doSomethingDangerous() { . . . }
![Page 282: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/282.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 282
Working with Roles
Can determine if caller belongs to a rolecontext.isCallerInRole(ADMIN)
Can change the role a bean operates under@RunAs(ADMIN)public class MyBean implements MyI { . . . }
![Page 283: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/283.jpg)
EJB Web Services
![Page 284: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/284.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 284
EJB Web Services Support
EJB supports three types of web services:
JAX-RPCCreates Web Service Endpoints defined by WSDL
Uses SOAP as “transport” mechanism
JAX-WS“New” version of JAX-RPC
Better support for more modern web service development
JAX-RSREST-based web services
Simple to implement and use
![Page 285: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/285.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 285
Web Service Development
Two ways to develop web services
Manually assemble web serviceWSDL
Java Interface
Bindings
AutomaticallyTranslate an EJB into a Web Service
Annotation driven
![Page 286: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/286.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 286
JAX-WS Web Service Annotations
@WebServicejavax.jws.WebServiceApply to EJB class
Exposes all public methods
@WebMethodjavax.jws.WebMethodApply to specific methods to expose within bean
SOAP Configuration annotations@SOAPBinding@WebParam@WebResult
![Page 287: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/287.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 287
Simple Example
![Page 288: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/288.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 288
JAX-RS Annotations
@Path – entry point for REST service
HTTP request methods@Get@Post@Put@Delete
@Produces / @Consumes – data transfers@Produces(“application/xml”)@Consumes(“application/xml”)@Produces(“application/json”)@Consumes(“application/json”)@Produces(“text/plain”)@Consumes(“text/plain”)
![Page 289: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/289.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 289
Simple REST EJB
![Page 290: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/290.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 290
Summary
AOP can be applied using Interceptors
Transactions can be managed by the container or the bean
Java EE is focused on authorization, not authentication
Stateless and MDBs can be converted to web services with the @WebService annotation
![Page 291: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/291.jpg)
EJB Timer Service
Timers
![Page 292: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/292.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 292
Section Topics
In this section, we’ll cover:
Introduction to the Timer Service
Key Steps to Developing Timed Executions
Handling Timeouts
![Page 293: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/293.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 293
Objectives
When we’re done, you should be able to:
Identify the corrector timer given a set of requirements
Implement a timer callback
Manage a timer
![Page 294: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/294.jpg)
Introduction to Timer Service
![Page 295: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/295.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 295
Java EE Timer Service
Timers in Javajava.util.Timer javax.swing.Timerjavax.management.timer.Timerjavax.ejb.Timer & javax.ejb.TimerServices
TimerService introduced as part of EJB 2.1Considered a core service
Provides scheduled, automatic execution
No real-time scheduling
![Page 296: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/296.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 296
javax.ejb.TimerService
Container provided timer serviceCan be used with Session and Message driven beans
Modeled after long-lived processes (survivability after crashes)
Supports three types of TimersNon-Interval (ie: in 5 minutes)
Interval (ie: every 5 minutes)
Scheduled (ie: every 5 minutes on Mondays)
Timer notificationStructured around a timeout callback
Callback invoked when Timer times-out
![Page 297: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/297.jpg)
Key Steps to Developing Timed Executions
![Page 298: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/298.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 298
Steps to Building Timed Execution
1. Access the Timer Service
2. Schedule Timer
3. Process timeout expiration
4. Manage lifecycle of timer
![Page 299: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/299.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 299
Accessing the TimerService
Can use either JNDI or DI
@Resource private TimerService ts;
![Page 300: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/300.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 300
Schedule a Timer
Create a Timer
Single Action Timer – occurs 1 time onlyts.createTimer(delay,object);
Interval-based Timer – executes on intervalsts.createTimer(delay,interval, object);
Calendar-based timer using a schedulets.createCalendarTimer(schedule);
![Page 301: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/301.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 301
Process Timeout Expiration
Handle timeout (version 1)@Timeout public void listOrders(Timer timer) { String name = timer.getInfo(); // do something fancy}
Handle timeout (version 2)@Timeout public void listOrders() { // do something fancy}
![Page 302: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/302.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 302
Process Timeout Expiration
Handle timeout (version 3)EJB2.1 style
Implement javax.ejb.TimedObjectMust implement ejbTimeout method
:: QUESTION ::Which approach should you use, and why?
![Page 303: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/303.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 303
Simple Timer Example
![Page 304: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/304.jpg)
Automatic Timers
![Page 305: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/305.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 305
Scheduled Timers
Timer Service supports UNIX cron-job like schedules
Defined through a ScheduleExpression
ScheduleExpression schedule = new ScheduleExpression();//define schedule . . .timerService.createCalendarTimer(schedule);
![Page 306: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/306.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 306
Defining Schedules
Schedules consist of 7 attributes:
Time componentssecond – [0,59]minute – [0,59]hour – [0,23]
Calendar components: dayOfMonth – [1,31],”Last”, “Sun”, “Mon”, “Tue”, etc.
month – [1,12], “Jan”, “Feb”, “Mar”, etc.
dayOfWeek – [0,7], “Mon”, “Tue”, etc.
year – [2001]
![Page 307: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/307.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 307
Schedule Examples
Every Tuesday at MidnightScheduleExpression schedule = new ScheduleExpression();schedule.setDayOfWeek(“Tue”);schedule.setHour(“0”);
Every Weekday at 3:15 AMScheduleExpression schedule = new ScheduleExpression();schedule.setDayOfWeek(“Mon-Fri”);schedule.setHour(“3”);schedule.setMinute(“15”);
Every 15 minutes of Every Hour Every DayScheduleExpression schedule = new ScheduleExpression();schedule.setHour(“*”);schedule.setMinute(“*/15”);
![Page 308: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/308.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 308
Automatic Creation of Timers
@Schedule annotation is interpreted by container
Automatically creates a ScheduleExpressionRegisters ScheduleExpression with TimerService
Apply @Schedule to a method in an EJBCan have multiple per Bean
Method functions as timeout callback
@Schedule(dayOfWeek=“Mon”)public void listOrders(Timer timer) { // do something fancy}
![Page 309: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/309.jpg)
Working with Timers
![Page 310: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/310.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 310
Persistent Timers
Survive crash of JVM / Application Server
Can configure timers to be non-persistent
Must explicitly define a timer as non-persistentProgrammatically using TimerConfigAutomatically using @Schedule
![Page 311: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/311.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 311
Timer Management
Timers references can be:Stored as a result of a create call
Retrieved from the TimerServiceRetrieved from the callback
Timer management is performed on the TimergetInfogetHandlegetNextTimeoutgetTimeRemainingcancel
![Page 312: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/312.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 312
Summary
Timers can be used to execute automated tasks
The functionality of a method or a bean can be augmented through an interceptor
![Page 313: Java EE EJB Applications](https://reader038.vdocument.in/reader038/viewer/2022102607/5476f312b4af9fa4388b46dd/html5/thumbnails/313.jpg)
2003 – 2013 DevelopIntelligence http://www.DevelopIntelligence.com 313
Lab: Create an Automatic Timer