New Features in EJB3.1 (Part 1)

Source: Internet
Author: User
Tags sessions jboss

Author Reza Rahman excerpted from http://www.theserverside.com
This series of articles is a preview of the changes the EJB 3.1 Expert group are working on for the next version of the Jav A EE specification. The idea are to give your a head ' s up on the changes as as the gather your feedback early so the Expert group has the best Chance of getting it right. EJB 3.0 brought simplicity to Java EE 5 by moving away from a heavyweight programming model. EJB 3.1 aims to builds on those successes by moving further down the path of simplicity as as-adding a handful of much -needed features. In all article in this series, I'll let you know about the progress made by the Expert Group over the next few months.
In this and I ' ll cover the two features that have been discussed into detail so far--making interfaces optional F or EJBs and Singleton Beans. I ' ll also give you a look-ahead into the possible features yet to be discussed. Remember, none of this has been finalized yet; All of this are really just a peek into the inner workings of the JCP, so, have a chance provide early feedback.EJB interfaces are Optional

The very the expert group covered is removing the last remaining obstacle to making EJBs look just s in their simplest form by making session Bean business interfaces optional.

Interface-based programming is clearly a useful technique in writing loosely-coupled, unit-testable applications. This is precisely why both EJB 2.1 and Spring promote the idea of component interfaces. In fact, at least one interface are required even for EJB 3.0 sessions Beans (this isn't a requirement for EJB 3.0 message Driven Beans).

The trouble is this component interfaces are just a needless abstraction in a lot of circumstances. I can ' t remember the times I silently cursed under me breath as I carried out the mechanical task of writing interfaces Ju St Because the framework needed it or the architect sitting in some ivory tower it. A lot of times, just a regular Java object are really all your need for your foreseeable needs, especially if don ' t real Ly do much testing and loose-coupling are just not enough to a concern for the application.

EJB 3.1 allows you to do just that. Now, don't need any interfaces for session Beans, just like JPA entities and message driven Beans. All your have to be annotate a POJO with the @Stateless or @Stateful to get a fully functional EJB. Take A look at the example below modified to the session Beans chapter in EJB 3 in Action: @Stateless   Public   Class  PlaceBidBean  ... {
@PersistenceContext
    private entitymanager entitymanager
    public void placeBid  (bid bid)   .... {
    entitymanager.persist (BID);
    }
}

To keep things interesting, let's add a few more features to this bargain-basement bean: @Stateless   @WebService public   Class  PlaceBidBean  ... {
     @PersistenceContext
  private entitymanager entitymanager
   @WebMethod  public void placeBid  (bid bid)   .... {
  entitymanager.persist (BID);
  }
}

In the above example, we are using JAX-WS 2.0 to expose the simple little as a Web service. We ' re also using JPA to persist a Bid entity. Placebidbean has the full range of stateless session Bean functionality available behind-the-scenes including pooling, THR Ead-safety, Component Life-cycle, interceptors, security and declarative transactions. For example, as your might have guessed if you are familiar with EJB 3.0, the Placebid method is wrapped in a container-man Aged JTA Transaction by default. Rmi-based remoting'll also be supported for this beans, although the exact semantics ' hasn ' t been solidified.

Besides making it easy to get going with a simple bean classes dropping the interface requirements the also using EJB 3.1 Beans in Webbeans easy. Gavin King is leading the Webbeans JSR (JSR 299), largely inspired by the lessons in JBoss Seam. Webbeans is expected to make JSF, JPA and EJB 3.1 virtually seamless (no pun intended) and make Java EE 6 truly feel A fully integrated stack. the Singleton Beans are here

EJB 3.1 Singleton Beans are the middleware equivalent of the GOF Singleton pattern. In the Java EE context, they are primarily used to store application-wide shared data.
I'm about all of the times, you needed to cache some data in memory so, didn ' t have to doing the same old database look UPS over and over again from different parts of the application. Neither stateless session Beans nor Stateful sessions Beans meet this need. While Stateful sessions Beans can maintain a session cache, it can ' t really to be shared amongst the application tier TS very easily.

There are various ways of solving this problem now. The simplest way is to use static fields or GOF Singleton pattern pojos. More complex strategies can work across a application cluster include using the Servlet container ' s application SCOP E, JBoss Cache, Oscache, JCS and Swarmcache. Commercial solutions include Tangosol coherence and Terracotta. While this solutions do work a majority to the time, they have a lot of weaknesses. Some are not thread-safe out of the box (static fields, Singleton POJOs), Some are not transactional (Singleton POJOs, the Servlet Application scope, Oscache, JCS, Swarmcache), none are remoteable and none have any security mechanisms. The other than the simplest solutions and all of them require the use of non-standard, Third-party code. Enter EJB 3.1 singletons.

The container is guaranteed to maintain a single shared instance of the EJB 3.1 Singleton. This means is singletons can cache state across the application tier. Because it is a EJB, singletons have the middleware services you might expect--declarative transaction management, SE curity, remoting, concurrency management, Dependency injection, Component life-cycle callbacks, interceptors and so on. Like all other EJBs, singletons are simply annotated POJOs. Here's a simple example:

  @Singleton
Public   class  DiscountRateBean  ... {
     @PersistenceContext
    private EntityManager  Entitymanager;
    private Rate rate;
     @PostConstruct
    private void init ()   .... {
    rate = entitymanager.find (rate.class, 1);
    }
     @PreDestroy
    private void destroy ()   .... {
    entitymanager.merge (rate);
    }
    public void setrate (rate rate)  .. {
    this.rate = rate;
    }
    public rate getrate ()  .. {
    return rate;
    }
}

The example is using JPA and beans life-cycle callbacks to load data on startup and save the The Bean is Destroye D (usually the application is being Shut-down). Any bean calling Getrate and setrate are guaranteed access to the shared data stored in a single instance of Discountratebe An. You'll notice an interesting problem--any updates'll be lost if the container does don't get a chance to call the Pre-dest Roy Callback. We'll have to minimize the problem using cron-like timers in a future article.

By default, all Singleton methods are made Thread-safe and transactional. This means so all multithreaded access to the bean are serialized and all methods are assumed to have a REQUIRED Ion are sensible defaults? If not, why?). Can change transactional behavior using the @TransactionManagement and @TransactionAttribute annotations just Would do to a Stateful or stateless session Bean. If you ' ve ever do this sort of thing for a relatively large scale application, you know have both the getrate and SETR Ate methods serialized can be a serious performance bottleneck. In reality, your simply need a read-lock on the Getrate method while a read-write lock should is placed on the setrate Od. You can does this by using the @ConcurrencyAttribute like so:

  @Singleton
Public   class  DiscountRateBean  ... {
     @PersistenceContext
    private EntityManager  Entitymanager;
    private Rate rate;
     @PostConstruct
    private void init ()   .... {
    rate = entitymanager.find (rate.class, 1);
    }
     @PreDestroy
    private void destroy ()   .... {
    entitymanager.merge (rate);
    }
     @ConcurrencyAttribute (read_write_lock)
    public void  setrate (rate rate)   {
    this.rate = rate;
    }
     @ConcurrencyAttribute (read_lock)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.