Gather round young folk. The grumpy "old" programmer is going to explain why EJBs are so "hard" when compared to things like Ruby on Rails. If you don't give a rip about EJBs, you still might find this interesting.
Ok, so I'm not exactly old (but I'm fairly sure I'm grumpy). Well, I don't feel old. But more often than not when on a job, I'm starting not to just be the most senior programmer experience wise but also one of the most senior age wise. Which freaks me out--I'm not even 35 yet! But be that as it may, I have actually seen quite a bit in the world of programming--especially when it comes to "distributed programming".
It's funny that I have to put that in quotes, but I think it might be necessary since no one speaks about "distributed programming" anymore. It's an uncommon term even though many modern solutions are essentially distributed programming (for instance, SOAP is basically distributed programming grown up and stunted all at the same time
Ok, enough musing, back to EJB. Why is EJB so hard? Well, whether you think it is hard or not, many do. Just google "death of EJB" to get a few. I used to think these people just didn't get EJB or the benefits of n-tier programming. Ok, it's complicated, but nothing powerful and flexible is ever simple. Quit being wussies!
But there I was writing EJBs for a client on an older J2EE platform and it took almost 2 days to get it working with the rest of the system. To be clear, my EJB worked just fine, but it was the integration that was killing us. Handing it off to others and getting their configs setup correctly was an absolutely pain. I just couldn't believe how much freaking trouble it was given us. And this was an easy EJB. It was so straight-forward we probably could have skipped the SB if we wanted to manage the transactions ourselves.
And that in a nutshell, is what people hate about EJB. That and the packaging (although I have a snazzy little build system that makes my EJBs fairly easy and cross-platform). After doing a lot of JSP or struts based projects, people start to wonder why EJB is so freaking hard and why even bother?
I think it's important to understand EJB's roots before we condemn it outright. Many of the easier solutions people use today came as a result of EJB or learning from EJB. So here's the quick, abbreviated timeline:
- First there was RPC. Remote procedure calls allowed you to knit together several systems and expose functionality from one system to another.
- RPC was pretty snazzy, and OO was just starting to get all sexy. People wanted to marry RPC with OO. This eventually led to CORBA.
- CORBA was all that (and a bag of chips). Lots of people got into it. Lots of people started realizing that just because you could marry OO to RPC, it doesn't mean you should. Through a lot of practice (read: expensive trial and error), people basically started making remote objects that just had business functions as the methods. That is, they grouped a bunch of RPC calls in to one namespace and called it an object ;-)
- Java found its killer app: not "write once, run anywhere" client applications, but server-side software. So there people are, cranking out server apps and some CORBA implementors thought, "Hot dog, Java would make a great ORB platform!" And sure enough, they were right.
- Sun suddenly is very happy about server side software. They look at CORBA and think, "Too complicated!" So they make RMI (remote method invocation). Ironically, RMI isn't really all that much easier. You got to skip the IDL creation, but the rest was only marginally simpler.
- So people are cranking out server software with servlets, RMI, or CORBA. And then someone says, "Gosh, you know what makes all this stuff hard to do? Transactions and life-cycle management!" And thus, EJB was born.
- Compared to RMI or CORBA, the life-cycle and transaction management of EJB is a breeze. And again, compared to RMI or CORBA, deployment isn't all that hard either.
- People go nuts. They embrace EJB full bore. Entity beans are the bomb. Session beans are innovative. But in the end, their just CORBA with a very nice framework. With a lot of practice (read: expensive trial and error), people learn that stateful transactions are freaking hard, EJB or no EJB. Everyone starts moving away from Entity Beans and Stateful Session Beans.
- People make some truly awful apps via Servlets and JSPs, mixing control code and presentation code. Despite MVC being decades old and most of Java being built on the design, no one seemed to have learned from it. Struts is born and people are very happy.
- With struts, MVC design is very easy to implement, and EJB isn't needed so much. Struts matures and people start to find they don't need the heavy duty aspects of EJB most of the time. The apps are industrial scale, but they are easy to make and easy to deploy.
- Learning from the J2EE body of work, Ruby on Rails is born. RoR gets people very, very excited. The main reason people are so excited is that RoR is so freaking easy to use for 90% of most small or routine applications. You can use the defaults and it just works!
I hope you see what I am getting at. EJB only seems hard or stupid if you don't bear in mind that RoR got to learn from its many mistakes. I'm not saying EJB isn't hard, I'm just saying you have to understand where it was coming from. EJB was a vast improvement in productivity over rolling CORBA apps from scratch.
But EJB is still too hard. And here's why: it seeks to solve all the problems. It doesn't just solve the simple problems, the hard problems, or the industrial-sized problems--it seeks to solve them all. And the easiest way to do that is to solve the industrial-sized problems and leave your framework open so that others could innovate and make it simpler to use for the easier problems.
No one ever did. That's why EJB is so hard: Sun never tried to make it easy. They always assumed a vendor would make it easy or a tool would make it simpler. And to be fair to Sun, there were a lot of vendors making sure Sun didn't try to eat their lunch. But it never really happened. You could point to XDoclet, but you would be falling into the trap EJB fell into: making one piece or another of X easier, but never thinking there might be some flaws in the assumptions that make X so hard. XDoclet makes deployment descriptors and home classes easier to generate, but it doesn't really make EJBs as easy as they could (or should) be.
And here we are, and no one is making EJB easier. One could argue that Spring makes EJB easier by just not being EJB. But that's not what I mean. There should be a way to use a simple POJO as a Session Bean just by declaring it in an XML file. Again, I know Spring kind of does that, but I'm actually talking about still using EJBs.
What is needed to fix EJB is not just a solution to the industrial-sized solutions, but also a solution for the simple problems. And the way to do that is a framework on top of the framework. You would need a two-piece framework: one for the server side and one for the client side. The server toolkit takes a simple POJO and deploys it as a stateless session bean, all business methods get a default transaction mechanism of "requires new". The client toolkit takes a simple POJO and returns a proxy that refers to the EJB--all without having to know about names, JNDI, or initial contexts.
I'm sure you might think, "Cripes, that would be a lot of work!" But would it really be any more work than creating a Java version of RoR? Would it be any more work than what was required to create Spring? I doubt it.
You might ask, "Well, Mr. Smarty Pants, why don't you make it?" Good question. Maybe I will (in all my spare time). Sigh. Spare time--if only ;-)
But I found the following advertisment in there. It so caught my eye that I surprised my wife and actually bought something. When we got home, I immediately hooked up the scanner and scanned in the full page; this is only the "book" part of it--the text of the ad is classic: