12

This might sound similar to this, but it's not.

I kind of understand EJB and RMI, and I have been working with web services under SOA for a while. I want to know why is useful to work using EJB exposing remote interfaces under RMI instead of publishing Web Services (SOA/REST, but mostly SOA). I'm not asking which one is better, just I want to know a very good reason of why should I prefer to implement EJB with remote interfaces over Web Services.

I have reviewed a lot of webpages but all seemed outdated. Until now, what I have is that EJB exposing remote interfaces is only better than WS when integrating with Java legacy system. If I want to manage transactions I could implement EJB with local interfaces. Also I don't think choosing EJB over RMI is more efficient than a Web Service interface.

Am I right? Is there something I'm missing?

Really thanks in advance.

Community
  • 1
  • 1
Christian Vielma
  • 12,606
  • 12
  • 52
  • 59

3 Answers3

22

EJBs are better if

  • you need to perform number of calls which should be done in one transaction (in theory we have transactional web services, but not every implementation provides them), (stateful) EJBs shine when it comes to transaction management. Almost anytime you need statefulness EJB would be better then Web Service;
  • you need performence - Web Services are slow - they use XML/JSON pushed through HTTP, RMI over IIOP protocol used by EJB is way more efficient;
  • you need to connect to some legacy system which uses outdated spec for Java Web Services (ugly Axis 1.0 stuff, not compatible with JAX-WS) it can be a nightmare to connect everything with web services, deal with not compatible WSDL definitions, strange SOAP envelopes. EJBs are backward compatible, old EJB 2 could be connected without any problem with EJB 3.1;
  • modern EJBs (EJB 3.X) can expose their interface as a JAX-WS SOAP/WSDL service or JAX-RS REST service by adding two or three simple annotations

Why (REST) Web services are so popular then? EJBs can be connected only to another Java application. Majority of modern Rich Internet Applications are written in JavaScript, so the only way to connect them with any backend is to use some kind of web service (usually REST + JSON). For such applications EJBs are pretty useless.

Piotr Kochański
  • 19,970
  • 6
  • 67
  • 75
  • Thanks, but before to mark it as answered, the first one wouldn't be a valid reason, because I can implement a Web Service with an EJB exposing a local interface right? So the transaction would be the same. – Christian Vielma Apr 17 '12 at 20:16
  • 3
    True, the problem start when you need to do a number of separated calls in one transaction. Web Services are usually stateless, and one has to manage transaction "by hand", which can be cumbersome. Stateful EJB would give that for free. – Piotr Kochański Apr 17 '12 at 20:22
  • Well, you can also have stateful web services, even using the last reason you gave before. So I will go for the second and third reasons, efficiency (a little bit I guess) and legacy. Thanks! – Christian Vielma Apr 17 '12 at 20:27
  • 1
    Good answer! Btw, purely theoretically CORBA clients in any language could connect to remote EJB as well. The protocol used is IIOP, which is not Java specific. Practice is probably different ;) – Mike Braun Apr 18 '12 at 08:30
  • 1
    Very true, CORBA is in fact a really cool technology, but at the beginning it was kind of hard to use, so it never get real popularity. Which is sad, since we end up with strings of XML thrown from one place to the other using inefficient protocol (that's basically SOAP/WSDL web service is). – Piotr Kochański Apr 18 '12 at 14:18
6

Both the client and service must be written in Java if you use RMI as the wire protocol.

SOAP uses XML over HTTP, and REST uses pure HTTP for communication between client and service. Either end of the conversation can be written in any language that can send appropriate requests over HTTP, which is far less restrictive.

I think this is one reason why web services over HTTP have won out over RMI. Simple and open win, every time.

duffymo
  • 293,097
  • 41
  • 348
  • 541
  • So it isn't worthy to keep learning EJB with remote interfaces anymore? Like if I want to learn Smalltalk (would just be for fun and learning, but not useful)? – Christian Vielma Apr 17 '12 at 20:12
  • I wouldn't go that far. The answer to that question depends on your circumstances. – duffymo Apr 17 '12 at 20:23
  • Ok but the only circunstance where it will be useful nowadays would be to integrate with Java legacy systems, right? – Christian Vielma Apr 17 '12 at 20:28
  • 2
    No, I can see EJB3 if you're writing an all-Java application from scratch, and you'll only have Java clients, and distributed, transactional componnents are worth something to you. Dogma is rarely helpful. – duffymo Apr 17 '12 at 20:43
  • 1
    Thanks @duffymo! but for example, I can have distributed, transactional web services also. Even I can implement Web Services using EJB3, under this, the RMI interface will only be worthy if I must connect with Java clients only, right? – Christian Vielma Apr 17 '12 at 20:48
  • 4
    No, stateless web services cannot participate in transactions. The only way to do it is to add "compensating transactions" to the API. I don't like that, because it forces my clients to know too much about managing transactions. An EJB service can easily enlist the transaction manager in the Java EE app server to do such a thing. So there's still a good reason for EJBs. – duffymo Apr 17 '12 at 22:06
  • Thanks, I'll check that because I'm not sure I fully understood what you said, but I need to go deeper in this knowledge. Really thanks! – Christian Vielma Apr 17 '12 at 22:20
6

They are different things for different purposes.

EJBs are for use within an N-tier system that is tightly coupled and you are in control of all elements. They offer direct coding in the language using what appear to be ordinary method calls, and LAN-type performance over IIOP or whatever other protocol the EE vendor has deployed.

SOAP/REST is best for use across the Internet, or in B2B or other situations where you aren't in control of both ends and you need loose coupling. You get much slower performance due to all the XML, but you also get an industry standard protocol in the middle which gives both ends protection against single-sourcing problems.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • This might be another question, but it is possible to easily adapt the EJB to expose it interface as a Web Service, so, you could start the first architecture that you define and if in the future you need to expand you can expose it through web services, or not? – Christian Vielma Apr 18 '12 at 14:38
  • @ChristianVielma That's another question all right and you won't get an answer on it from me ;-) Ask it as a question. – user207421 Apr 18 '12 at 23:17