This tutorial explains you the process which are
involved in making a message driven bean using EJB. Mesaage driven bean in EJB
have the following features:-
1) is a JMS listener
2) provides a single-use service
3) is relatively short lived
For developing the message driven bean we are using both the EJB module and web module. The
steps involved in creating message driven bean are as follows:-
Step1:-Create a persistence unit named as
persistence.xml.
Persistence unit defines the data source and entity manager used in our application. The
use of persistence unit to describe a convenient way of specifying a set of metadata files, and classes.
persistence.xml
| <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ persistence_1_0.xsd"> <persistence-unit name="NewsApp-ejbPU" transaction-type="JTA"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <jta-data-source>jdbc/sample</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="toplink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit> </persistence> |
Step2:-Create an Entity class named
NewsEntity.java
By Entity class we mean a java class that represent table from the
database. The annotation which is used for Representing a class as Entity is @Entity. Here
in the program given below :-
@Id annotation is used to declare the field as a primary key.
@GeneratedValue association is used for a
primary key property. it specifies auto-generation parameters and methods for
primary key.
private Long id; private String title; private String body; private String email; private String dob:-These
are the field declaration to the
class
NewsEntity.java
package ejb;
|
Step3:-Create a messagedriven bean named NewMessageBean.java
@MessageDriven(mappedName = "jms/Queue", activationConfig = {
|
This is the message driven annotation that tells the container that the component is a message-driven bean and the JMS resource
is used by the bean.
private EntityManager em:-By this we define the entity manager into the class.
NewMessageBean.java
package ejb;
|
Step4:-Create a session bean named
NewsEntityFacade.java
@Stateless is the annotation used to declare the class as a stateless session bean component.
NewsEntityFacade.java
package ejb;
|
Step5:-Create a local Interface named NewsEntityFacade.java
NewsEntityFacadeLocal.java
package ejb;
|
Step6:-Create a servlet named
ListNews.java
This is the servlet for displaying our data.
@EJB:-This is the annotation that configure the EJB values for a field or
a method. Normally this annotation is a Resource annotation where it is known
that the resultant is an EJB interface.
ListNews.java
package web;
|
Step7:-Create a servlet named
PostMessage.java
This servlet is used to post message.
PostMessage.java
package web;
|
Output of the program



If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Ejb message driven bean
Post your Comment