EJB 3 Basics
J2EE is a technology from Sun to develop multi tier applications. It is a standard which is implemented by many container providers. The container provides functionality like transaction management, clustering, caching, messaging between applications or in an application and much more. EJB 3 is becoming the next version of EJB. In March 2006, there are first demo implementations by some application server providers. This tutorial uses JBoss as application server.
An EJB (Enterprise Java Bean) is a special kind of class. There are three major types of EJBs.
Entity Beans
They can be used to map an entry in a database table to a class. (Object Relational Mapping) Instead of using result Sets from a database query you work with a class. The application server provides the functionality to load, update or delete the values of a class instance to the database.
Session Beans
Session beans are used to implement the functionality of your application. There are two kind of session beans: Stateful and Stateless.
A stateful session bean is for example a shopping cart class. The state is the cart holding the shopping items and the quantity. The cart class is hold in your application session and disposed at the end when the user checked out.
A stateless bean is a short living class. A typical example is a MailSender class sending a message. You call a method and dispose it. With a application server you do not instantiate the class each time you need it. The application server passes an instance from a pool. This is more efficient.
Message Driven Beans
Message beans provide functionality to implement messaging in your business logic. When you want to send a message to one or more recipients to start another business logic, you can use message beans. A Shop application could send a order message to the Warehouse management. Once the warehouse management software is started, it receives the orders from the shop application.