6

I'm new to Java EE and see that EJBs are alive and well within the pure Java/Oracle community. However everyone at work makes a disgusted look on their face whenever someone else even utters the phrase "EJB", which makes me think that they're either becoming extincted or have been replaced by modern development teams with some other middleware technology.

Just like JSP has given way to JSF-centric view technologies, is the same true for EJBs? Either way, what are some popular alternatives to EJBs and how are they different? What benefits or features do they offer over EJBs?

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 1
    Similar questions have been asked here before. Check out these questions - [what-use-are-ejbs](http://stackoverflow.com/questions/5579890/what-use-are-ejbs) , [in-what-situations-are-ejbs-used](http://stackoverflow.com/questions/4773927/in-what-situations-are-ejbs-used-are-they-required-in-websites-web-applicatio), and [spring-vs-ejb](http://stackoverflow.com/questions/1779169/spring-vs-ejb-can-spring-replace-ejb) – CoolBeans Jan 09 '12 at 16:59

3 Answers3

14

Your coworkers might have seen only EJB2 and earlier, which indeed were unholy beasts that very few people enjoyed using.

In EJB2, For the simplest of things very invasive framework provided interfaces had to be implemented, with insane life cycle methods for which the developer was required to provide an implementation, but which had nothing to do with the (business) goals of the developer. A few of such artifacts had to be provided.

Additionally every bean had to be kept in sync with entries in a very verbose and hard to read deployment descriptor (an XML file). As if that wasn't insulting enough to a developer, special tools had to be used to 'enhance' the bean and to generate proxy classes, skeletons and stubs. Common OO things like inheritance wasn't supported. There was a kind of injection, but a weird one that existed of putting things in a kind of map (directory actually) associated with each bean.

The original EJB 1 model enforced all communication to be remote and all objects to be distributed (EJBs were originally seen as a remoting technology only). So to get the benefits of EJB you were also forced to make your architecture distributed, even if this was completely unnecessary.

Perhaps the biggest insult of all was the concept of the Entity Bean (not to be confused with JPA entities). The faults with this type of bean were so great that even the biggest supporters of EJB at the time could barely recommend it to anyone.

Then as a very practical problem, compatibility between EJB implementations was not very good to say the least. Especially Entity Beans required massive amounts of vendor specific configuration.

To top it all off, EJB implementations required heavyweight (in terms of installed size and required memory) application servers, which were closed source and rather expensive (thus preventing companies from upgrading or switching because the investment had to be first paid back). I can't remember many people blogging about the technology at the time, and as far as I remember the technology was mostly pitched by sales teams to managers of big corporations.

It's not surprising that the technology wasn't really loved by the average developer.

Sun just in time became aware that the technology was heading in a completely wrong direction and did a 180° turn and started a massive re-engineering effort.

As a result, EJB 3 (2006) is a very sane and lightweight approach. There are no required framework interfaces to implement, there is no required XML, only a single bean has to be coded (just the simple POJO), there are sane defaults everywhere (convention over configuration), no special tools are required (normal javac will do), and using simple beans locally is actually the common case now.

Entity Beans were so flawed that they were dropped completely and replaced by the much saner approach advocated by TopLink and Hibernate among others.

Combine this with a wide availability of free, lightweight and open source implementations, in combination with many well known bloggers advocating the technology (e.g. Adam Bien, Rezha Rahman, Gavin King) and the rise back into popularity is easy to explain.

There have been a number of "Spring to Java EE" migration guides published recently, and those got a very favorable number of votes at the various news sites with many people expression their support for EJB as being a very good technology now. This would have been unthinkable half a decade ago (when EJB 3 was just released and wasn't very known yet).

The most common alternative to EJB is Spring Core (Spring Beans). At the moment I think there are no definite big advantages Spring has over EJB and the other way around. The two technologies are very similar. Spring offers some more convenience utilities, while EJB is conceptually more lightweight (no XML). Both advantages are somewhat subjective. They typically are inspired by each other's functionality and who is slightly ahead often depends on which technology has last released a major new version.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
10

The first version of EJB was introduced back in the end of the 1990s.

The disgusted look on your coworkers face when EJB is mentioned could be the consequence of the very complicated and troublesome usage patterns of the first two versions of EJB (e.g. complex xml deployment descriptors).

3.0 and 3.1 were major improvements regarding ease of use.

A Popular alternative is Spring framework + Hibernate / JPA

bpgergo
  • 15,669
  • 5
  • 44
  • 68
2

When they were introduced, EJB's were a solution in search of a problem, and a high-end one at that.

EJB's were supposed to provide a way for programs to define a unit of work such that the application server "containing" the EJB would handle all the more difficult bits, like load balancing, location, fail-over, security, remoting, etc. In actuality, when developers were all fired up over the shiny new language, the implementations were not really ready for all the heavy lifting, they may have profided the features but configuration and deployment were definitely a nightmare. It didn't help that the EJB spec was in flux during the time and the performance of the primary use-case candidate, the Entity Bean, was severely lacking.

In my case, we developed EJBs to handle what had always been a batch process - once a day get a set of transactions, do some database stuff, then send the output to various stakeholders and generate reports. We didn't need threading or load balancing, not to mention most of the other EJB features. What got used the most was the web-centric approach to applications. The whold application could have been done with some batch jobs and a few web pages.

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • I somehow remember the stated original purpose of EJB was to provide a distributed component model, with the idea that you could buy your components on a marketplace from vendors. E.g. a tax calculation component. This idea never took off, but it seems the many indirections in the early technology, so a 'bean deployer' could customize the (closed source) bean to work in the target environment still hint to this idea. – Arjan Tijms Jan 10 '12 at 22:49