Ejb message driven bean
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",
|
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; import ejb.NewsEntity; import java.io.*; import java.net.*; import javax.jms.Connection; import javax.annotation.Resource; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.Session; import javax.servlet.*; import javax.servlet.http.*; public class PostMessage extends HttpServlet { @Resource(mappedName = "jms/NewMessageFactory") private ConnectionFactory connectionFactory; @Resource(mappedName = "jms/NewMessage") private Queue queue; protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String title = request.getParameter("title"); String body = request.getParameter("body"); String email = request.getParameter("email"); String dob = request.getParameter("dob"); if ((title != null) && (body != null) && (email != null) && (dob != null)) { try { Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(queue); ObjectMessage message = session.createObjectMessage(); NewsEntity e = new NewsEntity(); e.setTitle(title); e.setBody(body); e.setEmail(email); e.setDob(dob); message.setObject(e); messageProducer.send(message); messageProducer.close(); connection.close(); response.sendRedirect("ListNews"); } catch (JMSException ex) { ex.printStackTrace(); } } PrintWriter out = response.getWriter(); try { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet PostMessage</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>EJB Servlet PostMessage at </h1>"); out.println("<hr></hr>"); out.println("<form>"); out.println("Name:<input type='text' name='title'><br/><br/>"); out.println("E-mail:<input type='text' name='email'><br/><br/>"); out.println("Dob:<input type='text' name='dob'><br/><br/>"); out.println("Address: <textarea name='body'></textarea><br/><br/>"); out.println("<input type='submit' value='Submit'><br/>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }
Output of the program