Hi Friends .Hope you are fine. Today i am going to demonstrate how JMS Queues are configured in Oracle WebLogic Server.Considering reader's have basic knowledge about Weblogic Server and Message driven beans of EJB.
JMS is a Standard API for accessing enterprise messaging systems.
You need to configure JMS Server, JMS Modules like Queues/Topics and Connection Factories and in order to get connect to the JMS Server you need to create write client by creating class in java.\
JMS Server will act as a managed container for JMS Modules where Administrator objects has been defined.
JMS Modules have resources defined that can be accessed using JMS Clients.
Technology Stack being used:
1) JDK 1.7
2) Oracle weblogic Server 12C
3) JMS (Java Messaging Service)
4) Message Driven Beans(part of EJB)
5) J2EE
First of all you need to create Enterprise Application Project. where eclipse will ask you to define new module as screenshot attached.
https://blogs.oracle.com/soaproactive/jms-step-1-how-to-create-a-simple-jms-queue-in-weblogic-server-11g
below is the configuration screenshot for JMS Configuration which shows JMS Servers,JMS Queues,Connection Factory :
I will going to define Message-Driven Bean which will be called when message is received.
Now i will going to show you a servlet which will connect to the administrator object defined in JMS server. Project Structure is as given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package org.test.mdb; import javax.annotation.Resource; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.ejb.MessageDrivenContext; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; /** * Message-Driven Bean implementation class for: MessageQueue1 */ @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "qjndi1") public class MessageQueue1 implements MessageListener { @Resource private MessageDrivenContext mdc; /** * Default constructor. */ public MessageQueue1() { // TODO Auto-generated constructor stub } /** * @see MessageListener#onMessage(Message) */ public void onMessage(Message inMessage) { System.out.println("onMessage method"); TextMessage msg = null; try { if (inMessage instanceof TextMessage) { msg = (TextMessage) inMessage; System.out.println("MDB Queue1: " + msg.getText()); } else { System.out.println("Message of wrong type: " + inMessage.getClass().getName()); } } catch (JMSException e) { e.printStackTrace(); mdc.setRollbackOnly(); } catch (Throwable te) { te.printStackTrace(); } } } |
below is the Sender Servlet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | package org.test.sender; import java.io.IOException; import java.io.PrintWriter; import java.util.Hashtable; import java.util.UUID; import javax.jms.DeliveryMode; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class QueueSenderServlet */ public class QueueSenderServlet extends HttpServlet { private static final long serialVersionUID = 1L; // Defines the JNDI context factory. public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory"; // Defines the JMS context factory. public final static String JMS_FACTORY = "cfjndi"; // Defines the queue. public final static String QUEUE = "qjndi1"; private QueueConnectionFactory qconFactory; private QueueConnection qcon; private QueueSession qsession; private QueueSender qsender; private Queue queue; private TextMessage msg; public QueueSenderServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { QueueConnection queueconnection = null; QueueSession qsession = null; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, "t3://localhost:7001/"); InitialContext ctx = null; try { ctx = new InitialContext(env); } catch (NamingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } PrintWriter pw = response.getWriter(); pw.println("Message Sending from Web client"); try { qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createQueueConnection(); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = (Queue) ctx.lookup(QUEUE); qsender = qsession.createSender(queue); msg = qsession.createTextMessage(); String data = request.getParameter("msg"); msg.setJMSCorrelationID(UUID.randomUUID().toString()); msg.setJMSMessageID("ch001"); msg.setJMSExpiration(10000); qsender.setDeliveryMode(DeliveryMode.PERSISTENT); msg.setText(data); qsender.send(msg); pw.println("To see message go to server console"); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (queueconnection != null) { try { queueconnection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } } |
Index.jsp is as given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title> Queue DEMO</title> </head> <body> <form action="QueueSenderServlet"> <input type="text" name="msg"> <input type="submit" value="send Message"/> </form> </body> </html> |
Deployment Descriptor :Web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>EJB3MDBQueueWeb</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>QueueSenderServlet</display-name> <servlet-name>QueueSenderServlet</servlet-name> <servlet-class>org.test.sender.QueueSenderServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueueSenderServlet</servlet-name> <url-pattern>/QueueSenderServlet</url-pattern> </servlet-mapping> </web-app> |
one can monitor message coming to JMS server in below location:
Home >SystemModule0 >Summary of Services: JMS >Summary of JMS Servers >JMSServer0 >Summary of Services: JMS >JMS Modules >SystemModule0 >ConnectionFactory0 >SystemModule0 >Queue0
Thanks for reading this article .for any query ping me on npjava90@gmail.com
No comments:
Post a Comment