Chapter 3. Beginning Java EE 6 – EJBs

In the previous chapter, we learned some basics about how to set up and deploy a HelloWorld application on JBoss AS 7. In this chapter, we will go a little deeper and learn how to create, deploy, and assemble Enterprise JavaBeans, which are at the heart of most Enterprise applications.

In greater detail, here is what you will learn in this chapter:

  • What the new EJB 3.1 features introduced by the Java EE 6 specification are
  • How to develop a singleton EJB
  • How to create stateless and stateful Enterprise JavaBeans
  • How to add schedulers and timers to your application
  • How to make use of asynchronous APIs in an EJB project

EJB 3.1 – new features

Based on the Enterprise JavaBeans (EJB) specification, Enterprise JavaBeans are Java components that typically implement the business logic of Java Enterprise Edition (JEE) applications as well as data access.

There are basically three types of Enterprise JavaBeans:

  • Stateless Session Beans (SLSB): SLSB are objects whose instances have no conversational state. This means that all these bean instances are equivalent when they are not servicing a client.
  • Stateful Session Beans (SFSB): SFSB support conversational services with tightly coupled clients. A stateful session bean accomplishes a task for a particular client. It maintains the state for the duration of a client session. After session completion, the state is not retained.
  • Message-driven beans (MDB): MDB are a kind of Enterprise Bean that are able to asynchronously process messages sent by any JMS producer. (We will discuss MDB in Chapter 7, Adding Java Message Service to Your Applications.)

Besides standard EJB components, the application server also supports the new EJB 3.1 variants introduced by Java EE 6; they are:

  • Singleton EJB: This is essentially similar to a stateless session bean; however, it uses a single instance to serve client requests. So, you can guarantee the use of the same instance across invocations. Singletons can use a richer life cycle for a set of events and a stricter locking policy to control concurrent access to the instance.
  • No-interface EJB: This is just another view of the standard session bean, except that the local clients do not require a separate interface, that is, all public methods of the bean class are automatically exposed to the caller.
  • Asynchronous EJB: These are able to process client requests asynchronously, just as with MDBs, except that they expose a typed interface and follow a more complex approach for processing client requests; it is composed of:
    • Fire-and-forget asynchronous void methods that are invoked by the client
    • Retrieve-result-later asynchronous methods having the Future<?> Return type

Since it's easier to grasp concepts with concrete examples, we will provide, in the next sections, a concrete application example that introduces all the features we have mentioned previously.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.141.75