Introduction
Enterprise JavaBeans (EJB) is a standard server-side component model for distributed business applications. EJBs are running on an JEE Container (or JEE application server by extension).
Notes
- The EJB specification requires that a Java EE product (such as JEE compliant container) provide support for enterprise beans as specified in the EJB specification.
- The contracts and requirements for entities (formely "entities beans") defined by Enterprise JavaBeans 3.x are specified in the document “Java Persistence API” (JPA, see JSR-317), which also contains the full specification of the Java Persistence query language (JPQL) and the metadata for object/relational mapping. While EJBs live in a JEE compliant container, an Entity can live outside any JEE container. So two distincts specifications. .
- Others interesting JSRs on persistence: Java Data Object (JDO - JSR-243), Service Data Object (SDO - JSR-235).
The EJB specification is a subset of a larger specification, the Java Platform, Enterprise Edition (JEE) specification. While the former specify the EJBs themselves (Sessions, MDBs) and the Persistence (in a separate JSR -JPA), the latter specify the platform. As such, JEE developpers will be more interested by the EJB specifications, while JEE product makers by the JEE specifications.
The JEE platform itself differs from the Java Platform, Standard Edition (JSE) in that it adds librairies to provide services to EJBs components.
Note
Fig. 1: The JEE 6 Platform architecture: Client (Applet and Application container),
Middle-Tier (Web and EJB Container) and Back-end.
Fig. 2: The JEE 5 Platform architecture: Client (Applet and Application container),
Middle-Tier (Web and EJB Container) and Back-end.
Note
JEE 5 VS. JEE 6: Java EE 6 adds significant new technologies that make the platform even more powerful. Three of these are:
- Java API for RESTful Web Services (JAX-RS)
- Contexts and Dependency Injection for the Java EE Platform (CDI)
- Bean Validation
- Java API for RESTful Web Services (JAX-RS)
- Contexts and Dependency Injection for the Java EE Platform (CDI)
- Bean Validation
For more, please read here.
Example of JEE 6 services (also called components or APIs):
HTTP(S), JTA, JPA, JMS, JavaMail, JCA, etc.
Note
EJB 3.x containers provide all application components with at least the JSE 6 APIs. Containers may provide newer versions of the JSE platform, provided they meet all the Java EE platform requirements. The JSE platform (so the JEE 6 platfrom) includes the following enterprise technologies: Java IDL, JDBC, RMI-IIOP, JNDI, JAXP, StAX, JAAS, JMX, JAX-WS, JAXB, JAF, SAAJ, Common Annotations
EJB 3.x makes it much easier to develop EJBs, using annotations rather than the complex deployment descriptors used in version 2.x.
The use of home and remote interfaces and the ejb-jar.xml file are also no longer required with yhe EJB 3.x. They are replaced with a business interface and a bean class that implements the interface.
Hello World
Simpliest possible Hello World example (without database access).
EJB 2.1
public interface HelloWorld extends EJBObject, Remote { public String sayHelloWorld() throws RemoteException; } /** Remote Home Interface for a Stateless Session Bean */ public interface HelloWorldHome extends EJBHome { public HelloWorld create() throws CreateException, RemoteException; } /** * Session Bean */ public class HelloWorldBean implements SessionBean { private transient SessionContext ctx; /** * Business methods. * Called by the client code. */ public String sayHelloWorld() throws RemoteException { return("Hello World!"); } /** * EJB-required methods (callback methods implementation). * Called by the container and never called by client code. */ public void ejbCreate () { //TODO } public void setSessionContext(SessionContext ctx) { this.ctx = ctx; } // Lifecycle Methods public void ejbActivate() { //TODO } public void ejbRemove() { //TODO } public void ejbPassivate() { //TODO } }
Deployment descriptors needs in more to be written.
EJB 3.x
The requirement for Home interfaces has been eliminated in the EJB 3.x specifications.
Session beans are no longer required to have home interfaces.
@Remote public interface HelloWorld { public String sayHelloWorld(); } @Stateless(mappedName=”HelloWorld”) public class HelloWorldBean { public String sayHelloWorld() { return("Hello World!"); } }
In EJB 3, deployments descriptors are automatically generated (based on the annotations), but if needed, XML deployment descriptors can still be used (overriding annotations definitions).
Notes:
- Since EJB 3.x, EJBs (Session, MDB, Entity) are now POJOs (Plain Old Java Object) and POJIs (Plain Old Java Interface).
- If necessary, with EJB 3, EJB 3 components can be used in conjunction with EJB 2.1 components (for backward compatibility).
EJBs
- Session (Stateful, Stateless)
- Message Driven Bean (MDB)
Note:
I personally think that Entities shouldn't be call EJBs anymore (as with EJB 2.x and older versions).
Below and extract of an wikipedia article that summarize well some of my reasons.
Entity beans (deprecated)
Previous versions of EJB also used a type of bean known as an Entity Bean. These were distributed objects having persistent state. Beans in which their container managed the persistent state were said to be using Container-Managed Persistence (CMP), whereas beans that managed their own state were said to be using Bean-Managed Persistence (BMP). In EJB 3.0, Entity Beans were replaced by the Java Persistence API, which was completely separated to its own specification to allow the EJB specification to focus only on the "core session bean and message-driven bean component models and their client API". Entity Beans are still available in EJB 3.1 for backwards compatibility, but they have been officially proposed to be removed from the specification (via a process called "pruning").
Other types of Enterprise Beans have been proposed. For instance, Enterprise Media Beans (JSR-86) address the integration of multimedia objects in JEE applications.
The buisiness logic reside in a Sessions and MDBs, while the data are represented in an Entity (a row in a table). The DDD (Data Driven Design) promote implementing the data manipulation business logic into entities childs. So far, DDD sounds to me a risky design in a JEE environment, so I won't discuss this design here.
Common EE Design
- Presentation Layer (JSP, Servlet, Applet, RIA, etc.)
- Business Logic Layer (Session, MDB)
- Persistence Layer (Entity)
- Database Layer (Oracle, MySQL, etc.)
Until the happening of EJB 3.x, the developpers were almost forced to adopt a procedural programming model. This is no more the case with EJB 3.x, thanks to its POJO nature: developers are now free to implements and/or extends the interfaces and classes they want.
No comments:
Post a Comment