Are you wondering why EJB plays a crucial role in developing and deploying robust Java applications? Do you want to know about software components or Beans in EJB? No worries! You have landed on the right page. This tutorial is an easy guide to learning EJB concepts in greater detail. Here, we have covered the types of EJB, various client-access methods, different life cycles of EJB, and much more. We have added practical examples to learn about creating enterprise beans, packaging them, and so on. Ready to go?
EJB is an architecture used to distribute program components across multiple clients. It has powerful server-side capabilities with which you can develop and deploy programs across all platforms. In this tutorial, you can learn EJB, its various types, different client access methods, the lifecycles of different EJB, creating EJB, packaging EJB, and mapping EJB with references in greater detail.
Let’s dive into the topics!
|If you want to enrich your career and become a professional in EJB, then enroll in "Core Java Training". This course will help you to achieve excellence in this domain.|
EJB is the short form of Enterprise JavaBeans. It is part of the J2EE platform and built on JavaBeans technology. EJB is a server-side platform that runs based on a server-client model. This platform provides software components to build distributed and robust business applications. The software components are generally written in Java language. EJB architecture includes three components: EJBs, Java application server, and EJB container. Here, the EJB container is nothing but a runtime environment where you can run EJB applications.
Mainly, EJB focuses on the business logic of applications. Business logic represents the core functionality of an application. You can use EJB to simplify large-scale business applications. You can also develop and deploy highly scalable and high-performance applications effortlessly.
Are you wondering why EJB is one of the powerful platforms for developing large-scale Java applications? The following points will help you in this regard:
Know that there are three types of EJB as given below:
In the following sections, let's be ready to look at the different types of beans and their features.
By using session beans, you can make interactive sessions with clients. The interactive sessions get terminated when the clients leave. Essentially, session beans provide web services and perform tasks for clients.
Stateful beans and stateless beans are the types of session beans. Let's have a brief of them in the following:
Stateless beans offer good scalability. So, they can support any number of clients. As a result, stateless beans offer better performance than stateful beans. Stateless beans are not written in secondary storage, whereas stateful beans are written in secondary storage. No wonder only a stateless bean can provide web services.
[ Check out Configuring the EJB Components ]
Entity beans represent business entity objects stored in a persistent storage device, such as a relational database. The business entity may be customers, products, orders, etc. Know that entity beans offer many features such as persistence, primary keys, shared access, and relationship with other beans.
Let’s discuss the features below:
No wonder a single message-driven bean can manage multiple messages from multiple clients. The messages could be JMS messages or any other messages. Here, JMS is nothing but an API that supports communication between various application components. You can use it as a middleware to create, receive, send, and read messages. Also, JMS messages can be sent to application clients, web components, and other Enterprise beans seamlessly. In a way, message-driven beans are similar to stateless beans since both don't hold the state of beans instances at any time. Above all, Message-driven beans help java applications to run asynchronously.
No doubt that any type of client can access Enterprise Beans invariably. The client may be a remote, web service, or local client. By using interfaces, clients can access entity beans and session beans. On the contrary, message-driven beans cannot be accessed through interfaces. Besides, EJB allows client access using only the methods defined in the interfaces.
Three types of client access methods are used in EJB: remote, web service, and local. You can choose either local or remote access by considering many factors, such as container-managed relationships, the type of coupling between related beans, component distribution, type of clients, and performance.
Be it a session, message-driven, or entity Enterprise Bean, they come with different lifecycles. Let’s discuss them below:
A client can initiate the lifecycle of a stateful session bean by raising the ‘create’ method. Once instantiated, the EJB container raises the setSessionContext method in the session bean. After that, it raises the ejbCreate method in the bean. Now, the session bean is ready to enable business methods. When the session bean goes to the ready state, the EJB container can activate or passivate the bean. For passivation, the EJB container raises the ejbPassivate method. During the passivation, the bean is moved from memory to secondary storage. When the client raises the business methods, the EJB container enables the bean through the ejbActivate method.
After that, the EJB container moves the bean to a ready state. When the client wants to end the session bean, it performs the remove method. Once done, the EJB container raises the ejbRemove method to terminate the session.
There is no passivation stage in the lifecycle of a stateless session bean. Stateless session bean has only two stages. The first stage is known as 'non-existent', and another is known as 'ready'. Generally, the business methods are raised in the ‘ready’ stage.
As a first step in the life cycle of an entity bean, the EJB container creates an entity bean’s instance. Then, the EJB container raises the setEntityContext method to transfer the entity context to the bean. After that, the bean moves to the pool of instances. The EJB container creates an identity to the entity bean’s instance while moving it to the ready state.
By using two ways, you can move entity bean from pooled state to ready state. In one way, a client raises the create method, and then the EJB container raises the EJBCreate method. After that, it raises the ejbPostCreate method. In another way, the EJB container raises the ejbActivate method. As you know, when beans are in the ready state, clients can raise business methods.
In reverse, beans can move from a ready state to pooled state in two ways. In one way, a client raises the remove method, and then the EJB container calls the ejbRemove method. In another way, the client raises the ejbPassivate method. At the end of this lifecycle, the EJB container raises the unsetEntityContext method. And then it removes entity bean instances from the pool.
This life cycle of a message-driven bean has only two stages. One is the 'non-existent' stage, and another one is 'ready' to receive messages. Similar to the life cycle of a stateless session bean, the life cycle of a message-driven bean is not passivated. In this lifecycle, the EJB container creates a group of message-driven bean instances. Then the EJB container passes the context object to every instance by raising the setMessageDrivenContext method. Then it calls the ejbCreate method. The EJB container raises the ejbRemove method at the end of this lifecycle.
Here, we will learn how to create ConverterBean – an Enterprise Bean. ConverterBean is the application used to convert currencies. Consider that this bean is the stateless session bean. You will need to follow the below two steps to create the ConverterBean.
Let's see know how to perform these two steps one by one.
ConverterBean is the Enterprise Bean class used in this example. This class supports implementing two business methods: converting YenToEuro and DollerToYen. And this class represents a no-interface and local view since it doesn’t implement a business interface. Besides, clients can use public methods in this class.
The source code for the Enterprise Bean class is given below:
Know that a Java Servlet is nothing but a web component that can respond to HTTPS requests. You can find the web client in the following Servlet class.
Generally, ConverterServlet class refers to ConverterBean using dependency injection. And javax.ejb.EJB annotation is included when declaring private member variable converter. Enterprise Bean implementation class is the variable type since ConverterBean represents no-interface and a local view. You can understand this from the following codes.
The amount is collected from the request parameters when you enter the amount converted from Yen to Euro or Dollar to Yen. The conversion will be performed by calling methods such as ConverterBean.YenToEuro and ConverterBean.DollarToYen.
You can compile the Servlet class and Enterprise Bean class at this stage. Then, you can package the compiled classes in a WAR file. You can use either Maven or NetBeans IDE for building, packaging, running, and deploying the codes.
If you want to use NetBeans IDE to run the codes, then you can follow the below steps:
To package Enterprise Beans, you need to run the ‘edit Enterprise bean wizard’ of the ‘deploy tool’ utility. When you start this process, the wizard will do the following actions.
Let’s understand how the packaging of Enterprise Beans is performed using the steps mentioned above in the following sections.
First of all, start the ‘edit Enterprise Bean wizard’. Then go to ‘file’, select ‘new’, and then choose ‘Enterprise Bean’.
Now, you can see the following dialog boxes will open in the wizard.
Whether you use a web client or an application client to access the same Enterprise Bean, you must use different names in codes to refer to the beans. Simply put, the application client refers to bean’s home as ejb/SimpleConverter, whereas the web client refers to bean’s home as ejb/TheConverter. If only you map the references in codes to the JNDI name of the Enterprise Beans, you can use the lookup method to retrieve the home bean.
You can follow the below steps to map the references in codes to the JNDI names.
|Learn EJB Interview Questions and Answers that help you grab the high-paying jobs|
There are a lot of advantages when clients use the EJB platform for developing and deploying java applications. Let’s have a look at them below:
Undeniably, there are a few limitations that exist for EJB. Let’s take a look at them below:
In a nutshell, EJB is the platform for providing software components (beans) to develop distributed and large-scale enterprise applications. EJB functions based on the business logic of an application, which in turn supports building robust java applications. By offering system-level services, EJB ensures effective transaction management and security aspects in the EJB applications. This tutorial covered the different types, various client access methods, and lifecycles of enterprise beans. The practical examples on how to create, package, and map enterprise beans might have helped to learn the EJB concepts in a detailed way.
Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!
|Core Java Training||Nov 29 to Dec 14|
|Core Java Training||Dec 03 to Dec 18|
|Core Java Training||Dec 06 to Dec 21|
|Core Java Training||Dec 10 to Dec 25|
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .
Copyright © 2013 - 2022 MindMajix Technologies