EJB 3.0 are thread save. This is a huge benefit. Every thread gets an own instance - also all injected resources (Entity Manager, Data Source, JMS) are thread save as well. The best of all: the container manages the resources for you. Only an annotation is required (@EJB for EJBs, @PersistenceContext for Persistence and @Resource for remaining resources :-)). Just try it with "plain old Web Container" :-). Hint: Servlets are thread-unsafe singletons.
EJB 3.0 are transactional. So in case the method invocation is successfull, all resources will be synchronized (data written to DB, messages sent to server). The container does it for you - you have only know what you would like to achieve (this is platform independent, but sometimes hard enough :-))In all of my projects we get rid of deployment descriptors and used annotations. Only a lean persistence.xml was deployed. Refactoring, deployment and portability just worked well.EJB 3.0 are really portable (and so vendor, framework neutral). There is lot more specified, than EJB 2.1 days - so proprietary deployment descriptors aren't needed any more. Actually no descriptors are needed. You only have to deploy one clean jar, with persistence.xml in meta-inf. Everything else is optional (and somehow suspicious).
The synergy between Convention Over Configuration (or officially: Configuration By Exception) and Dependency Injection is great. Actually in most cases you get with EJBs less code, than without (no "new" invocations, just declarations).
@Stateless
public class SampleBean implements Sample {
@PersistenceContext
private EntityManager em;
@EJB
private Another anotherBean;
Getters And Setters are optional: for EJBs, as well as for JPA. It's your decision. Resources can be directly injected into the fields.The defaults and annotation configuration can overruled by XML-descriptors. The XML descriptors do not have to be complete - only interesting portions can be specified. You can easily override the production settings for e.g. staging (test, integration etc).
Actually there is no special tooling required. You only need the annotations, a Java 6 compiler and a Jar. However the support in IntelliJ, Eclipse (with 500 additional plugis :-)), or Netbeans 6.1 are really superb. The applications servers are already well integrated - deployment, undeployment, configuration etc. can be managed directly from the IDE.EJB 3.1 will be embeddable out-of-the box. However Glassfish v3 is already emeddable. I'm testing now the WebContainer - it's surprising (starts in 500ms). JBoss is embeddable as well.
EJB 3.1 will come with a lot of useful features: Singletons (good for configuration, startup classes etc.), better timer support (cron-like), async methods with Futures, optional local-interfaces and WAR-deployment.
Pool settings and thread pool configuration is really useful to
control the scalability (and not scale indefinitely until the container
crashed :-))
They play well with scripting (JavaScript, Groovy etc.)EJBs are managable and monitorable by default. You just deploy them into container - and you can monitor the method invocations, performance etc.No additional frameworks, libraries etc. are needed first. The ejb-jar and the container only contains your application code. So NoClassDefFoundErrors, ClassCastExceptions etc. can be minimized.EJB 3.0 are ultra-lean. You actually cannot take something away :-). However: I'm open for suggestions.They are really easy to test - it's just class with an interface. You can even start them outside the container.Even simple use cases can be efficiently be realized with EJB 3. See e.g. CRUD:
@Stateless
public class CrudServiceBean implements CrudService<Integer,Customer> {
@PersistenceContext
private EntityManager em;
public Customer create(Customer t) {
this.em.persist(t);
return t;
}
public void delete(Customer t) {
t = this.em.merge(t);
this.em.remove(t);
}
public Customer find(Integer id) {
return this.em.find(Customer.class, id);
}
public Customer update(Customer t) {
return this.em.merge(t);
}
}
...and they really work well: I'm using EJB 3.0 + JPA 1.0 since about 2 years in projects. It worked
not only surprisingly well for me - the team members were surprised as
well.
However there is still room for improvement in the Java EE 6 Platform. JMS spec could be easily redesigned to be more "fluent". The JNDI registry is archaic. Fixing/simplifying the JNDI-API would be really beneficial not only to EJBs, but for the whole platform as well...