This post is the first in series of tutorials about Enterprise Java Beans (EJB) development. EJBs are core Java EE technology and are wildly used in enterprise application which require heavy business logic and support for other enterprise technologies, such as transactions, security, messaging etc. In this post, I will go through the process of setting up development environment, which includes IDE, application server and database. In the following parts, we will build simple enterprise application which will demonstrate some common EJB concepts and design patterns.
Quick introduction to EJB
Before we get started with coding, let’s first go through brief introduction to Enterprise Java Beans. EJBs are a part of Java EE specification and their main role is to implement business logic of the application. Current version of EJB specification is 3.0 and it’s part of Java EE specification.
EJBs run within EJB container, which is a part of application server. Application servers usually implement complete Java EE specification, along with some vendor-specific add-ons. The purpose of application server (and EJB container inside it) is to provide system-wide features, such as transactions, security, configuration etc. This allows developers to focus on building business logic and speeds up development time.
One more benefit of EJBs is that they are reusable, meaning that one piece of business logic can be used in multiple applications without change.
Types of EJBs
There are two types of EJBs: session beans and message-driven beans. Session beans usually perform some business task for the client, while message-driven beans act as listeners for some types of message (ie. they execute some business logic asynchronously).
Session beans implement one piece of business logic, and can be accessed programmaticaly by client code. Clients initiate business logic on server by accessing session bean’s methods. There are several types of session beans:
- stateful beans – internal state of object is represented by fields of this bean. Stateful beans retain their state during one client session (conversational state). After client session ends, bean is disposed and it’s state is no longer available. A session can have only one client
- stateless beans – these beans do not retain state with the client. Instead, each invocation of EJB method will work with separate inner state
- singleton beans – these are similar to stateless beans, but only one instance exists per application
Message-driven beans enable applications to process messages asynchronously. They are similar to event listeners, but receive JMS (Java Messaging Service) messages instead of events. Unlike session beans, message-driven beans are not accessed by clients directly. Instead, they respond to messages sent by other Java EE components. They are similar to stateless session beans, in that they retain no data,
Setting up your environment
For this series of posts, we will need the following software packages. I will not go through the specifics of installing each one, since probably all of you already know how to install these tools. For those who don’t, there are already a lot of documentation about installing each of these packages, so you can easily find it.
We will need the following tools:
- JDK 8 Standard Edition – can be downloaded from Oracle
- NetBeans IDE – download from NetBeans website. Please use Java EE version, since it contains all the tools we will need for these tutorials
- Wildfly Application Server – download from Wildfly website
- Postgresql database – download from Postgresql website
- pgAdmin – GUI tools for managing Postgresql instance (downlaod from here)
Once these are installed, we need to register Wildfly instance to NetBeans, so we can run application on it. In NetBeans main menu, choose Tools>Servers. This window will pop up:
After it is opened, click on “Add Server…” button, and choose “Wildfly Application Server” from the dropdown list:
Click next and navigate to Wildfly installation location under “Server location” :
Click next, and in the final window, you can set some of the server properties. You can simply accept the defaults and click “Finish”. After that, your Wildfly instance will be registered with NetBeans and available to deploy application on.
What will we build?
In order to showcase usage of EJBs, we will build simple money transfer application. I think it’s great of example of enterprise application, because, let’s face it, what’s more business critical then money? This simple application will allow clients to deposit money to their account, withdraw from it and transfer money to another account.
In the next post, we start building our application.
- EJB with NetBeans and Wildfly – part 2