JMS (Java Message Service) is an API that provides the facility to create, send and read messages.
Generally, user sends message to application. But, if we want to send message from one application to another, we need to use JMS API.
To develop JMS queue example, you need to install any application server. Here, we are using Wildfly 15 server where we are creating two JNDI.
Before writing code we have create Queue connection and a JMS Queue in Wildfly server
for creating Queue connection we have three option
1. In VM Connection
2. Http Connection
2 Remote Connection
We using a In VM connection for our JMS application for creating JMS Queue connection
Go to bin directory of open cmd and write following command to start Wildfly server with configuration
standalone -c standalone-full.xml
after starting of Wildfly server hit following URL
Open Wild fly server admin console by the URL http://localhost:9990
Login with the username and password.
Go to following location
first of we need a JMS connector to create JMS connection wildfly by default create a In VM connector with name in-vm
for creating JMS coonection go to following location
Configuration-> Messaging (Active MQ)->Server->Default ->Connection ->View ->
Connection Factory -> Add
after entering JMS connection name , connector name and JMS Enteries value click on Add button
Now create JMS Queue for create Go to following location
Configuration-> Messaging (Active MQ)->Server->Default ->Destinations->View ->
JMS Queue -> Add
after entering JMS queue name and JMS Enteries value click on Add button
After creating JMS Queue connection and and JMS Queue
Create a web application with name JMSapp(according to this blog) and create sender and receiver servlet class
and an index page for enter message for sender class .
JMS Message sender servlet
SenderServrlet.java
package in.sha;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.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 SenderServlet
*/
public class SenderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SenderServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String message = request.getParameter("message");
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory f = (QueueConnectionFactory) ctx
.lookup("java:/ConnectionFactory");
System.out.println("-----");
System.out.println(f);
QueueConnection con = f.createQueueConnection();
con.start();
QueueSession ses = con.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue t = (Queue) ctx.lookup("java:/jms/queue/MyFirstQueue");
System.out.println(t);
QueueSender sender = ses.createSender(t);
TextMessage msg = ses.createTextMessage();
msg.setText(message);
sender.send(msg);
System.out.println("Message sending successfully ------->.");
} catch (Exception e) {
System.out.println(e);
}
}
}
package in.sha;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.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 SenderServlet
*/
public class SenderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SenderServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String message = request.getParameter("message");
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory f = (QueueConnectionFactory) ctx
.lookup("java:/ConnectionFactory");
System.out.println("-----");
System.out.println(f);
QueueConnection con = f.createQueueConnection();
con.start();
QueueSession ses = con.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue t = (Queue) ctx.lookup("java:/jms/queue/MyFirstQueue");
System.out.println(t);
QueueSender sender = ses.createSender(t);
TextMessage msg = ses.createTextMessage();
msg.setText(message);
sender.send(msg);
System.out.println("Message sending successfully ------->.");
} catch (Exception e) {
System.out.println(e);
}
}
}
JMS Message Listener
MyListener.java
package in.sha;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MyListener implements MessageListener {
public void onMessage(Message m) {
try{
TextMessage msg=(TextMessage)m;
System.out.println("Message is received by shashank:"+msg.getText());
}catch(JMSException e){System.out.println(e);}
}
}
package in.sha;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MyListener implements MessageListener {
public void onMessage(Message m) {
try{
TextMessage msg=(TextMessage)m;
System.out.println("Message is received by shashank:"+msg.getText());
}catch(JMSException e){System.out.println(e);}
}
}
JMS Message Reciever servlet
RecieverServlet.java
package in.sha;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Reciever
*/
public class RecieverServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Reciever() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx
.lookup("java:/ConnectionFactory");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
// 2) create Queue session
QueueSession ses = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// 3) get the Queue object
Queue t = (Queue) ctx.lookup("java:/jms/queue/MyFirstQueue");
// 5) create TextMessage object
QueueReceiver receiver = ses.createReceiver(t);
// 5) create listener object
MyListener listener = new MyListener();
// 6) register the listener object with receiver
receiver.setMessageListener(listener);
System.out.println("Receiver1 is ready, waiting for messages...");
System.out.println("procees start.......");
while (true) {
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
package in.sha;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Reciever
*/
public class RecieverServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Reciever() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx
.lookup("java:/ConnectionFactory");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
// 2) create Queue session
QueueSession ses = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// 3) get the Queue object
Queue t = (Queue) ctx.lookup("java:/jms/queue/MyFirstQueue");
// 5) create TextMessage object
QueueReceiver receiver = ses.createReceiver(t);
// 5) create listener object
MyListener listener = new MyListener();
// 6) register the listener object with receiver
receiver.setMessageListener(listener);
System.out.println("Receiver1 is ready, waiting for messages...");
System.out.println("procees start.......");
while (true) {
Thread.sleep(1000);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
index.jsp page for entering message
<%@ 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>Enter Message</title>
</head>
<body>
<form action="SenderServlet" method="get">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</body>
</html>
<%@ 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>Enter Message</title>
</head>
<body>
<form action="SenderServlet" method="get">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</body>
</html>
web.xml
<?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>Testjmsapp</display-name>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>in.sha.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>RecieverServlet</display-name>
<servlet-name>RecieverServlet</servlet-name>
<servlet-class>in.sha.RecieverServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RecieverServlet</servlet-name>
<url-pattern>/RecieverServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SenderServlet</display-name>
<servlet-name>SenderServlet</servlet-name>
<servlet-class>in.sha.SenderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SenderServlet</servlet-name>
<url-pattern>/SenderServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Reciever</display-name>
<servlet-name>Reciever</servlet-name>
<servlet-class>in.sha.RecieverServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Reciever</servlet-name>
<url-pattern>/RecieverServlet</url-pattern>
</servlet-mapping>
</web-app>
<?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>Testjmsapp</display-name>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>in.sha.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>RecieverServlet</display-name>
<servlet-name>RecieverServlet</servlet-name>
<servlet-class>in.sha.RecieverServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RecieverServlet</servlet-name>
<url-pattern>/RecieverServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SenderServlet</display-name>
<servlet-name>SenderServlet</servlet-name>
<servlet-class>in.sha.SenderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SenderServlet</servlet-name>
<url-pattern>/SenderServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Reciever</display-name>
<servlet-name>Reciever</servlet-name>
<servlet-class>in.sha.RecieverServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Reciever</servlet-name>
<url-pattern>/RecieverServlet</url-pattern>
</servlet-mapping>
</web-app>
To run and test our application code depoly application in Wildfly server
after successful depolyment of application
first run JMS meesage Reciver Servlet by
Enter URL http://localhost:9990/JMSapp/RecieverServlet
Now you reciever is running successfully ready to reciver message form Sender
Now start Sender for open following URL
Enter URL http://localhost:9990/JMSapp/index.jsp
After entering your message click on submit
Now you will see following output in wildfly console .....
No comments:
Post a Comment