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.
6. 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
7. The 3 minutes introduction to EJB 3
BeJUG Evening Session - Enterprise JavaBeans 3.1
15. 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
16. EJB 3 Programming Model
2. Invokes middleware services:
Lifecycle mgmt, trx mgmt,
Persistence, security, etc.
1. Call a method
4. Return from call
3. Invoke corresponding
method on bean class
4. Return outcome
BeJUG Evening Session - Enterprise JavaBeans 3.1
17. So, what’s new in EJB 3.1?
BeJUG Evening Session - Enterprise JavaBeans 3.1
18. 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
20. Example: Session Bean with Local Business
interface
BeJUG Evening Session - Enterprise JavaBeans 3.1
21. Example: Session Bean without Local
Business interface
BeJUG Evening Session - Enterprise JavaBeans 3.1
22. 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
26. 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
27. Specs are ok:
Blond hair,
blue eyes,
red lips, EJB HEAVY
pink thong...
BUT...
EJB lite
BeJUG Evening Session - Enterprise JavaBeans 3.1
28. Lite vs Full
• EJB Lite: • Full = Lite + ...
• Local Session Beans • Message Driven Beans
• CMT / BMT • EJB Web Service
Endpoints
• Declarative Security
• Interceptors • RMI-IIOP Interop
• Web Profile also includes • 2.x / 3.x Remote view
JPA 2.0 • 2.x Local view
• Timer Service
• CMP / BMP Entities
BeJUG Evening Session - Enterprise JavaBeans 3.1
29. 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
30. 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
31. Example: remote session bean with remote
client
Bean deployed as
part of hello.jar
BeJUG Evening Session - Enterprise JavaBeans 3.1
32. 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
33. 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
36. 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
37. Lifecycle of a singleton session bean
Does not exist
Class.newInstance()
@PreDestroy Dependency injections
@PostConstruct
Method-ready
Business method
BeJUG Evening Session - Enterprise JavaBeans 3.1
39. 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
40. 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
43. 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
48. Example: EJB 3.0 use of Timer Service
BeJUG Evening Session - Enterprise JavaBeans 3.1
49. 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
50. 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
51. 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
52. Automatic Timer creation
• Container creates timer automatically
when application is being deployed:
BeJUG Evening Session - Enterprise JavaBeans 3.1
54. 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
56. 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
58. 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
61. Spring
vs
Java EE 6
“Now that Java EE 6 is
here…do I still need those
proprietary frameworks?”
Lean Java EE 6 without Spring
“Java EE 6 provides similar benefits without tying you to a non-standard solution”
63. Soon, in a store near you...
Bert Ertman
“I wrote this book
for
developers who ha architects and
ve grown increasin
frustrated with that gly
annoying rebel
framework called
Sp
you can do to impl ring. It shows what
ement cleaner, m
productive and ab ore
ove all STANDARD
code for developm
en
web applications.” t of first class
Java
with EJB !!!
BeJUG Evening Session - Enterprise JavaBeans 3.1