|
EJB and JavaBeansThese are very different software components. Common
Differences
|
EJB Application Server (J2EE Application Server)You need an EJB application server to deploy and run your EJBs. Some possibilities:
|
Session Beans |
Used to perform some action on the enterprise system and possibly deliver results to client
|
||
NO LONGER EXISTS FOR LASTEST VERSION OF EJB ( have been replaced by Java Persistence API entities. For information about entities, see Chapter 16, Introduction to the Java Persistence API.) |
Used to encapsulate some data contained in the enterprise system. Such data may be created, removed or found by clients.
|
||
Message Driven Beans |
Used to provide a service whose invocation is asynchronous and driven by the arrival of enterprise messages (JMS) OR other kinds of messages.
Characteristics
Lifecycle
best practice: use when you want to develop loosely coupled system and process asynchronous messages. |
OPTION1: uncompressed
use directory structure above and specify directory (assembly root) as directory of EJB code
OPTION 2: compressed
compress into JAR/EAR file and deploy this to server.
Enterprise beans often provide the business logic of a web application.
Packaging the enterprise bean within the web application's WAR module can make deployment easier.
OPTION 1: not compressed, class files in WEB-INF/classes directory
To include enterprise bean class files in a WAR module, the class files should be in the WEB-INF/classes directory.
OPTION 2: compressed, JAR file.
To include a JAR file that contains enterprise beans in a WAR module, add the JAR to the WEB-INF/lib directory of the WAR module.
NOTE: on ejb-jar.xml in this case:
WAR modules that contain enterprise beans do not require an ejb-jar.xml deployment descriptor. If the application uses ejb-jar.xml, it must be located in the WAR module's WEB-INF directory.
EXAMPLE:
Suppose a web application consisted of a shopping cart enterprise bean, a credit card processing enterprise bean, and a Java servlet front-end. The shopping cart bean exposes a local, no-interface view and is defined as follows:
package com.example.cart;
@Stateless public class CartBean { ... }The credit card processing bean is packaged within its own JAR file, cc.jar. It exposes a local, no-interface view and is defined as follows:
package com.example.cc;
@Stateless public class CreditCardBean { ... }The servlet, com.example.web.StoreServlet handles the web front-end and uses both CartBean and CreditCardBean. The WAR module layout for this application looks as follows:
WEB-INF/classes/com/example/cart/CartBean.class
WEB-INF/classes/com/example/web/StoreServlet
WEB-INF/lib/cc.jar
WEB-INF/ejb-jar.xml
WEB-INF/web.xml