ejb3

EJB Architecture Example

1. Introduction

EJB stands for Enterprise Java Bean. An Enterprise Java Bean is in its basic form any POJO(Plain Old Java Object) that is registered with the container in which it is deployed. Enterprise Java Beans are deployed into an EJB container. The EJB container is governed by the EJB specification. At the time of writing this article, the latest version of the specification is EJB 3.2, which is mostly enhancements of the EJB 3.1 specification. For any EJB application to be functional it has to be deployed in a Java EE Application Server. In this article, we will discover which components make up the EJB Architecture. Further, we will also create a basic Java EE application which will contain 3 EJBs: 1 Stateful Bean, 1 Stateless Bean, and 1 Message-Driven Bean.

2. Technologies used

  • Java EE 7
  • Wildfly 12 Final
  • InteliJ 2016.2.5

3. EJB Architecture Layout

Below is a High-level architecture diagram of Enterprise Java Beans.

EJB Architecture

In this diagram, we can see the logical representation of the way EJBs are deployed and invoked by means of the remote method invocation (RMI). It is important to note the fact that EJB containers cannot be deployed without an application server. However, as from Java EE 7, it is now possible to configure an application in such a way that it is only made of Web components only. Further, this is known as the Web Profile within the Java EE space. Java EE 7 has got two types of profiles, the Web profile, and the Full Java EE profile. The Full Java EE profile is made up of the Web Profile and everything else that is not required by the Web Profile.

4. Application Server

In the previous section, we saw that the Application Server is the outermost construct within our Architecture. The key responsibilities of an Application Server are:

  • Mangement API
  • Process and thread management
  • Database connection pooling and caching
  • System Resources management

The above list of responsibilities helps us understand what the role of the Application server within our architecture is.

5. EJB Container

The EJB container is one of the logical constructs which makes up the Full Java EE profile. From our architecture diagram, we saw that the EJB container construct is the second outmost construct of the architecture. Furthermore, its key responsibilities are:

  • It provides a runtime environment for Enterprise Java Beans
  • It provides persistence management
  • It is responsible for the Lifecycle management of EJBs
  • It is in charge of ensuring that all EJBs are secured

In the next sections, we will discover the different types of EJBs which are deployable in an EJB container.

6. Types of Enterprise Java Beans

There are 3 types of Enterprise Java Beans namely:

  • Message-driven beans
  • Stateless beans
  • Stateful beans

In the sections below we will dive into each type of Enterprise Java Bean together with basic code samples.

7. Stateless Enterprise Java Beans

A stateless enterprise java bean is primarily used to implement stateless business logic. An example of a business logic for which a stateless bean could be used is the storing the physical address of a user into a database of an inventory system. Further, a stateless bean would be a perfect fit for this type of business logic because at all stages of the transaction state is not required within more than two forms on the user interface. Therefore the server side, in turn, has to be stateless.

Below is a basic example of a stateless bean:

StatelessBean.java

package com.javacodegeek.ejbarchitecture;

import javax.ejb.Stateless;

@Stateless
public class StatelessBean {
}

8. Stateful Enterprise Java Beans

Stateful enterprise java beans are used in scenarios where part of the application requirement is to maintain state on the backend during a user session. An example of this type of requirements is a shopping cart in an online shopping system. the steps to achieve this with a stateful EJB would be to:

  1. Create a stateful session bean
  2. Instantiate a global variable collection of type product which will be used to temporarily store the selected products within the user session
  3. Implement a method which will be adding user selected items in the collection defined in the previous step
  4. Implement a remove product method to remove products from the products collection
  5. Implement a checkout method to submit the list of selected products for processing

The above steps illustrate the kind of business logic that a stateful bean can have. Furthermore, it is important to note that this type of business logic is impossible within a stateless enterprise java bean because like the name says: stateless beans do not maintain any state (i.e we would not have been able to define the product collection in our previous steps).

Below is code snippet of a stateful bean:

StatefulBean.java

package com.javacodegeek.ejbarchitecture;

import javax.ejb.Stateful;

@Stateful
public class StatefulBean {
}

Note the fact that from the above snippet we use the @Sateful annotation when creating our bean. The @Sateful annotation is used to register our POJO with the EJB container so that other beans requiring the features provided would be able to obtain an instance with the help of the EJB container and without using the new keyword.

9. Message Driven Beans

Message-Driven beans are used in order to send and receive messages from message brokers that implement the JMS specification. An example of a JMS based message broker is the JBoss Hornetq. The benefits of a broker based system are the fact that such systems are loosely coupled, in the sense that because a broker by nature is asynchronous it means components communicating via the broker will not have to wait for one request to finish before submitting another request. This would then translate to having a need for a type of Java enterprise beans with the purpose of facilitating this process of asynchronous communication. Further, that is the primary reason for the birth of Message-Driven Beans.

Below is a basic example of a Message-Driven Bean:

MessageDrivenBean.java

package com.javacodegeek.ejbarchitecture;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(mappedName = "jms/Queue",
        activationConfig = {
                @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                @ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/queue/testqueue")
        })

public class MessageDrivenBean implements MessageListener {
    
    public MessageDrivenBean() {
    }

    public void onMessage(Message message) {
        System.out.println("Message received.");
    }
}

In the above code snippet, we can quickly notice the use of the @MessageDriven annotation. This annotation is used to register our bean with the EJB container so that it can start sending and receiving JMS messages. The propertyactivationConfig with its sub-configuration properties is used to define the destination, the Message-Driven bean must use.

10. Conclusion

In this article, we saw that the EJB architecture is made up of a client application, an application server, and an EJB container in which the different types of beans live. With the help of the Wildfly application server, we managed to successfully deploy the 3 different types of EJBs that are available in the Java EE specification. Java EE is moving more rapidly nowadays than before. This, in turn, translates to the EJB specification also been regularly revisited and updated to meet current industry standards for enterprise application development.

11. Download the Source Code

That was the EJB Architecture Example

Download
You can download the full source code of this example here: EJBArchitectureExample

Georges Sofo

Georges Albert Sofo is a bachelors degree holder in software development, more specifically using Java technologies and following the OOP paradigm approach to software development. he has being working as a full time Software Engineer for more than 4 years, at companies ranging from telecommunication, consultancy, financial institution and educational type companies. His current focus lies in anything Java (Spring Framework preferably) on the backend and Javascript on the Frontend.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button