ejb 3.1 by bert ertman
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
Enterprise JavaBeans 3.1
Bert [email protected]
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
#fail
#fail#fail
EJB
J2EE
BeJUG Evening Session - Enterprise JavaBeans 3.1
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
The 3 minutes introduction to EJB 3
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 2.x Session Bean
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 2.x Deployment Descriptor
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 2.x Client View
BeJUG Evening Session - Enterprise JavaBeans 3.1
painful truth of reality?blissful ignorance
or
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 3.x Session Bean
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 3.x Deployment Descriptor
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 3.x Client View
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
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
So, what’s new in EJB 3.1?
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
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: Session Bean with Local Business interface
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: Session Bean without Local Business interface
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Java EE 5 packaging
BeJUG Evening Session - Enterprise JavaBeans 3.1
Simplified packaging
BeJUG Evening Session - Enterprise JavaBeans 3.1
EJB 3.1 lite
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
EJB HEAVY
EJB lite
Specs are ok:
Blond hair,blue eyes,red lips,
pink thong...
BUT...
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
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
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>
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: remote session bean with remote client
Bean deployed as part of hello.jar
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
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: Embeddable API
BeJUG Evening Session - Enterprise JavaBeans 3.1
New features
• Singletons
• Startup / shutdown callbacks
• Calendar-based timers / automatic timer creation
• Asynchronous invocations
• JAX-RS integration
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
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: simple singleton usage
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
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: read-only singleton (container managed)
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: read-mostly singleton (container managed)
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: bean managed concurrency
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: startup/shutdown callbacks
BeJUG Evening Session - Enterprise JavaBeans 3.1
Timer Service enhancements
• Calendar-based timeouts
• Automatic timer creation
• Non-persistent timers
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: EJB 3.0 use of Timer Service
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
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
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=”*”)
BeJUG Evening Session - Enterprise JavaBeans 3.1
Automatic Timer creation
• Container creates timer automatically when application is being deployed:
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: non-persistent timers
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: local concurrent computation
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: Asynchronous operation result
AsyncResult<V> implements Future<V>
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
BeJUG Evening Session - Enterprise JavaBeans 3.1
Example: Session Bean as JAX-RS resource
Rod, I am your father
Lean Java EE 6 without Spring
Springvs
Java EE 6
“Now that Java EE 6 is here…do I still need those proprietary frameworks?”
“Java EE 6 provides similar benefits without tying you to a non-standard solution”
BeJUG Evening Session - Enterprise JavaBeans 3.1
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.”
BeJUG Evening Session - Enterprise JavaBeans 3.1
Questions?