EJB 3.1 by Bert Ertman

65
Enterprise JavaBeans 3.1 Bert Ertman [email protected]

description

With the official release of Java EE 6 in December 2009 a new version of the Enterprise JavaBeans specification also saw the light. Enterprise JavaBeans is an architecture for the development and deployment of component-based business applications. Applications written using the Enterprise JavaBeans architecture are scalable, transactional, and concurrent.While a lot of faithful EJB developer's have been scared away from the specification and some of its unfortunate implementations in the past five years, EJB 3.1 has all the ingredients that make for a successful lightweight component based implementation. At last a decent implementation of a server-side component framework as part of the Java EE specification. This no longer makes you dependent on rebel frameworks such as the Spring framework.EJB 3.1 continues down the path where EJB 3.0 left us off. The purpose of the Enterprise JavaBeans 3.1 specification is to further simplify the EJB architecture by reducing its complexity from the developer's point of view, while also adding new functionality in response to the needs of the community. Although the Java Persistence API was developed within EJB 3.0, it now evolves under a separate JSR rather than within EJB 3.1 and will therefore not be covered in this presentation.This presentation will mainly focus on the new features introduced by EJB 3.1 and the basics of EJB are only covered very briefly. Topics covered include: EJB Lite, simple packaging, no-interface local view, portable JNDI names, Embeddable API, Startup/shutdown callbacks, Singleton beans, the new and improved timer and scheduler component, Async invocations, and REST integration.

Transcript of EJB 3.1 by Bert Ertman

Page 1: EJB 3.1 by Bert Ertman

Enterprise JavaBeans 3.1

Bert [email protected]

Page 2: EJB 3.1 by Bert Ertman

Enterprise JavaBeans 3.1Bert  Ertman

Technology  Manager

Competence  Center  Java

Co-­‐lead  NLJUG,  Sun/Oracle  recognized  Java  Champion

Author  and  editorial  board  member  

for  Dutch  Java  Magazine

E-­‐mail:  [email protected]

Follow  me  on  TwiLer:  @BertErtman

Page 3: EJB 3.1 by Bert Ertman

#fail

#fail#fail

EJB

J2EE

Page 4: EJB 3.1 by Bert Ertman
Page 5: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Page 6: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Agenda

• EJB 3: short recap

• What’s new in EJB 3.1

• Ease-of-use improvements

• EJB Lite

• New features

Page 7: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

The 3 minutes introduction to EJB 3

Page 8: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 2.x Session Bean

Page 9: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 2.x Deployment Descriptor

Page 10: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 2.x Client View

Page 11: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

painful truth of reality?blissful ignorance

or

Page 12: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 3.x Session Bean

Page 13: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 3.x Deployment Descriptor

Page 14: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 3.x Client View

Page 15: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

How did they do that?

• Configuration by exception with sensible defaults

• Transaction type defaults to CONTAINER

• Transaction attribute defaults to REQUIRED

• Security permissions defaults to UNCHECKED

• Use Annotations

• To choose explicitly (recommended)

• To override defaults

• Deployment descriptor is no longer required

• But can override above configurations

Page 16: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

EJB 3 Programming Model

1. Call a method

2. Invokes middleware services: Lifecycle mgmt, trx mgmt,

Persistence, security, etc.

3. Invoke corresponding method on bean class

4. Return outcome

4. Return from call

Page 17: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

So, what’s new in EJB 3.1?

Page 18: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

EJB 3.1

• Separate JSR (318)

• Evolve separately from JPA

• Focus on ease-of-use

• Expert group formed - Aug 2007

• Final release - December 2009

Page 19: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Ease-of-use improvements

• Optional Local Business interface

• Simplified packaging

• EJB-Lite

• Portable JNDI names

• Simple component testing

Page 20: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: Session Bean with Local Business interface

Page 21: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: Session Bean without Local Business interface

Page 22: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Simplified packaging

• Goal is to remove artificial packaging restrictions

• All resources are shared within .war

• Web application APIs are not exposed to EJB components

• ejb-jar.xml is still optional

• If needed, placed in WEB-INF

Page 23: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Java EE 5 packaging

Page 24: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Simplified packaging

Page 25: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

EJB 3.1 lite

Page 26: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

EJB 3.1 “Lite”

• Small subset of EJB 3.1 API required by Java EE 6 Web Profile

• Broadens the availability of EJB technology without losing portability

Page 27: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

EJB HEAVY

EJB lite

Specs are ok:

Blond hair,blue eyes,red lips,

pink thong...

BUT...

Page 28: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Lite vs Full

• EJB Lite:

• Local Session Beans

• CMT / BMT

• Declarative Security

• Interceptors

• Web Profile also includes JPA 2.0

• Full = Lite + ...

• Message Driven Beans

• EJB Web Service Endpoints

• RMI-IIOP Interop

• 2.x / 3.x Remote view

• 2.x Local view

• Timer Service

• CMP / BMP Entities

Page 29: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Portable global JNDI names

• Spec-defined lookup names for Remote and Local session beans

• Three main goals :

• Simplify intra-application Local Session lookups

• Simplify mapping of cross-application Remote session bean dependencies

• Improve portability of Remote Java clients

Page 30: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Portable naming syntax

• Each session bean gets the following entries :

• Globally unique name:java:global[/<app-name>]/<module-name>/<ejb-name>

• Unique name within same application:

java:app[/<module-name>]/<ejb-name>

• Unique name within defining module:

java:module/<ejb-name>

Page 31: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: remote session bean with remote client

Bean deployed as part of hello.jar

Page 32: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

EJB component testing

• Session Beans were always hard to test

• Unit-testing was possible, but what about container resources?

• Automated integration testing required

• Now supported with in-process EJB container

Page 33: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Embeddable API

• Portable API for running EJB components in same process as client code

• Same component behavior / life cycle as server-side

• CMT/BMT, injection, threading guarantees, ...

• “Single Application” model

• Only required to support 3.1 “Lite” API

• Vendors can optionally support additional functionality

Page 34: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: Embeddable API

Page 35: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

New features

• Singletons

• Startup / shutdown callbacks

• Calendar-based timers / automatic timer creation

• Asynchronous invocations

• JAX-RS integration

Page 36: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Singletons

• One instance per application, per JVM

• Not intended to provide cluster-wide singleton

• Fits into existing dependency injection architecture

• Access through @EJB or lookup

• Intended for sharing data within entire application (not just EJB components)

• Designed for concurrent access

Page 37: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Lifecycle of a singleton session bean

Does not exist

Method-ready

Class.newInstance()Dependency injections@PostConstruct

@PreDestroy

Business method

Page 38: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: simple singleton usage

Page 39: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Singleton concurrency options

• Single threaded (default)

• For consistency with all existing bean types

• Container Managed Concurrency

• Control access via method-level locking metadata

• Bean Managed Concurrency

• All concurrent invocations have access to bean instance

Page 40: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Container Managed Concurrency

• Concurrent access determined by method-level locking metadata

• READ lock: allow any number of concurrent accesses to bean instance

• WRITE lock (default): ensure single-threaded bean instance access

• Incoming invocations are blocked by container until the necessary lock can be acquired

• … or until an optional timeout has been reached

Page 41: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: read-only singleton (container managed)

Page 42: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: read-mostly singleton (container managed)

Page 43: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Bean Managed Concurrency

• All incoming invocations allowed to access bean instance concurrently

• Bean developer is solely responsible for maintaining integrity of instance state

• Can use synchronization primitives such as synchronized, volatile

Page 44: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: bean managed concurrency

Page 45: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Application Startup/shutdown callbacks

• Uses singleton bean life cycle

• Full container services available

• Container-managed transactions

• Entity Managers, Timer Service, etc.

• Startup callback:

• @Startup and @PostConstruct

• Shutdown callback:

• @PreDestroy

Page 46: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: startup/shutdown callbacks

Page 47: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Timer Service enhancements

• Calendar-based timeouts

• Automatic timer creation

• Non-persistent timers

Page 48: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: EJB 3.0 use of Timer Service

Page 49: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Some (hard) lessons learned...

• Difficult to configure calendar-based events using only relative time units

• How to register the timer in the first place?

• Typical container events not a great fit; they happen every time application initializes and in every server instance

• Burden is on developer to figure out if timer already exists

• No way to guarantee that only one is created per cluster

Page 50: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Calendar based timeouts

• Cron-like semantics with improved syntax

• Can be created programmatically or automatically

• Named attributes:

• second, minute, hour (default = “0”)

• dayOfMonth, month, dayOfWeek, year (default = “*”)

• Relative to optionally specified time zone

Page 51: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Calendar expression examples

• The last Thursday in November at 2 pm:

• (hour=”14”, dayOfMonth=”Last Thu”, month=”Nov”)

• Every day at 3:15 am U.S. Eastern Time:

• (minute=”15”, hour=”3”, timezone=”America/New_York”)

• Every twenty seconds:

• (second=”*/20”, minute=”*”, hour=”*”)

Page 52: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Automatic Timer creation

• Container creates timer automatically when application is being deployed:

Page 53: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: non-persistent timers

Page 54: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Simple asynchronous operations

• Use metadata to mark a Local/Remote business method as asynchronous

• Container returns control to client before executing business method

• No separate API to learn

Page 55: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: local concurrent computation

Page 56: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Asynchronous results

• Based on java.util.concurrent.Future

• Result value is returned via Future.get()

• Also supports Future.get(long, TimeUnit)

• Client exception wrapped by ExecutionException

• getCause() returns same exception as would have been thrown by a synchronous invocation

Page 57: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: Asynchronous operation result

AsyncResult<V> implements Future<V>

Page 58: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

JAX-RS support

• Java API for XML RESTful Services

• JSR-311 annotations can be used within Stateless Session Beans

Page 59: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Example: Session Bean as JAX-RS resource

Page 60: EJB 3.1 by Bert Ertman

Rod, I am your father

Page 62: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Page 63: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Soon, in a store near you...

Javawith EJB !!!

Bert Ertman“I wrote this book for architects and developers who have grown increasinglyfrustrated with that annoying rebelframework called Spring. It shows what you can do to implement cleaner, more productive and above all STANDARD code for development of first class web applications.”

Page 64: EJB 3.1 by Bert Ertman

BeJUG Evening Session - Enterprise JavaBeans 3.1

Questions?

Page 65: EJB 3.1 by Bert Ertman