Enterprise JavaBeans (EJB) is server-side component model of the Java platform for developing distributed enterprise applications.
It enables enterprises develop “secure, scalable, multi-platform, business-critical applications as server-side components”
The EJB specification is part of Java EE specification which encapsulates the business logic of an application.
Applications developed using EJB run on Java EE Application Servers which include an EJB container by default or a standalone EJB container can be used. (More on this later).
The main point of EJB was to allow enterprise developers to concentrate on business logic and free them from the need to write transaction, security, caching, pooling and threading primitives by delegating that task to the server vendor.
When to use EJB?
If your application,
- Needs to encapsulate business logic
- Business logic is separated from presentation and persistence tier
- Needs Remote access
- Applications running on different servers can access EJB.
- Needs to be scalable
- Almost all Java EE application servers support clustering, load balancing, and failover
- Needs Broad vendor support
- JBoss, Oracle AS, WebLogic, WebSphere, Glassfish, etc are some of Java EE Application Servers.
- Needs to be portable
- Applications developed using EJB can be written once and deployed on different Java EE Application Servers
- Needs deployment time configuration
- Enables deployment-time configuration using XML-based deployment descriptor or within the bean’s code using metadata deployment annotations.
Disadvantages of EJB:
- EJB specification is too large and complicated. Even though EJB might be simpler than other remote object systems, remote-object frameworks are much more complex than local-object frameworks.
- Spring is easier and more powerful for local access
- Requires Java EE server
- Can’t run on Web Containers such as Tomcat, Jetty, Resin, JRun, Resin
- Java EE servers are usually hard to configure, significantly slower to start/restart during development and testing, and usually cost money
- Bad name due to earlier releases
- EJB2 was so complex that EJB has bad reputation till this day
- Needs Java Clients
- EJBs are built on top of RMI. Both involve Java clients and beans. If your clients need to be written in something else (e.g., .NET, PHP, etc.) then you have to go with web services or something else that is a platform independent protocol, like HTTP, XML over HTTP or SOAP.
- If you don’t want to use EJB , you have two other choices:
- Lightweight open source Java framework – Spring.
- Microsoft .NET–managed components, part of the Microsoft .NET platform.