Most developers don't have the time or patience to go thru the 640-page EJB 2.1 specification, but they still need to know about enhancements introduced in EJB 2.1. So in this article, we'll give you an overview of the advancements introduced in the Enterprise JavaBean (EJB) 2.1 specification. A quick word of caution: The version of EJB 2.1 specification that we'll discuss is still a public draft, so there may be slight changes before it is finalized.
What is new in EJB 2.1?
Here's a brief list of EJB 2.1's new features:
- Container-managed timer service—The timer service provides coarse-grained, transactional, time-based event notifications to enable enterprise beans to model and manage higher-level business processes.
- Web service support – EJB 2.1 adds the ability of stateless session beans to implement a Web service endpoint via a Web service endpoint interface.
- Expanded JMS support - The architecture for message-driven beans to support the use of messaging types in addition to JMS has been generalized.
- EJB-QL - Enhanced EJB-QL includes support for aggregate functions and ordering of results.
Now, let’s take a closer look at each of these items individually.
The EJB timer service is a container-provided service that allows the bean provider to register enterprise beans for timer callbacks that occur after a specified time, after a specified elapsed time, or at specified intervals.
The service creates a timer to schedule callbacks. When the time specified at timer creation elapses, the container invokes the ejbTimeout method of the bean. Here are two examples where the EJB timer service might be used:
- A bean that can send reminders to all contractors on every Friday to complete their time sheets.
- A bean that can send time synchronization messages to clients after a specified interval.
An enterprise bean accesses the timer service through its EJBContext interface. The timer service provides methods for the creation and cancellation of timers, as well as for locating the timers associated with a bean. The bean class using the timer service must implement the javax.ejb.TimedObject interface. This interface has a single method, the timer callback method ejbTimeout.
A bean may cancel a timer before it actually expires. If a timer is cancelled, the ejbTimeout method is not called.
Support for Web services
Stateless session beans may have Web service clients. Essentially, stateless session beans may work as Web services, and a Web service client may invoke stateless session beans using the Simple Object Access Protocol (SOAP).
A bean’s Web service endpoint interface is described with Web Service Description Language (WSDL). The bean provider chooses a Web service endpoint interface for a stateless session bean when it wishes to expose the bean's functionality as a Web service endpoint. Web service clients send or receive messages with SOAP.
From the client perspective, the stateless session bean is completely hidden behind the Web service endpoint; the client accesses the Web service functionality through the endpoint. In the case of Java clients, this endpoint is accessed as a JAX-RPC service endpoint using the JAX-RPC client-view APIs.
Stateless session beans deployed as Web services are accessed by various clients, independent of platform and language.
EJB 2.0 introduced an abstract programming model for entity beans with container-managed persistence (CMP). In this case, a single line of business code is unnecessary inside the entity bean. Everything is written in the ejb-jar.xml file by defining EJB-QL and other tags. EJB-QL is a query language for finder and select methods of CMP entity beans. The container compiles the queries to a target language (like SQL) for the underlying database. Consequently, entity beans are optimizable and portable. The EJB-QL syntax is as follows:
select_clause from_clause [where_clause] [orderby_clause]
EJB2.1 has introduced some additional functions to EJB-QL, including:
- Aggregate Functions: AVG, COUNT, MAX, MIN, SUM.
- An OrderBy clause, which allows objects or values returned by a query to be ordered in specific fashion.
Message-driven beans for non-JMS message sources
Message-driven beans are asynchronous message consumers. These beans are anonymous components that do not have client visibility; remote clients can't use them directly.
EJB 2.0 introduced message-driven beans, which consume messages only from JMS sources, like Queue or Topic. EJB2.1 extends this functionality so that the beans consume messages from other message sources like XML (JAXM). A message-driven bean can receive messages from a single messaging type only, depending upon the messaging interface it extends.
For example, a message-driven bean implementing javax.jms.MessageListener consumes JMS messages only. On the other hand, a message-driven bean implementing either one of interfaces javax.xml.messaging.OnewayListener or javax.xml.messaging.ReqRespListener consumes only JAXM messages.
Scratching the surface
In this article, we've provided only a brief introduction to the cool new aspects of the EJB 2.1 specification. Be sure to review the complete EJB 2.1 document for more information.