博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JMS ActiveMQ案例
阅读量:5069 次
发布时间:2019-06-12

本文共 5581 字,大约阅读时间需要 18 分钟。

创建一个web工程

导入ActiveMQ依赖的jar包  activemq-all-5.9.jar

写一个生产者(send)servlet

package com.sun.jms;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jms.DeliveryMode;

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.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class JMSTest
*/
@WebServlet("/Send")
public class Send extends HttpServlet {
private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()
*/
public Send() {
super();
// TODO Auto-generated constructor stub
}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();

try {

// get the initial context
InitialContext context = new InitialContext();

// lookup the queue object

Queue queue = (Queue) context.lookup("java:comp/env/queue/queue0");

// lookup the queue connection factory

QueueConnectionFactory conFactory = (QueueConnectionFactory) context
.lookup("java:comp/env/queue/connectionFactory");

// create a queue connection

QueueConnection queConn = conFactory.createQueueConnection();

// create a queue session

QueueSession queSession = queConn.createQueueSession(false,
Session.DUPS_OK_ACKNOWLEDGE);

// create a queue sender

QueueSender queSender = queSession.createSender(queue);
queSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

// create a simple message to say "Hello World"

TextMessage message = queSession.createTextMessage("Hello World");

// send the message

queSender.send(message);

// print what we did

out.write("Message Sent: " + message.getText());

// close the queue connection

queConn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

======================================================

写一个消费者(Receive)servlet

package com.sun.jms;

import java.io.IOException;

import java.io.PrintWriter;

import javax.jms.Queue;

import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**

* Servlet implementation class Receive
*/
@WebServlet("/Receive")
public class Receive extends HttpServlet {
private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()
*/
public Receive() {
super();
// TODO Auto-generated constructor stub
}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();

try {

// get the initial context
InitialContext context = new InitialContext();

// lookup the queue object

Queue queue = (Queue) context.lookup("java:comp/env/queue/queue0");

// lookup the queue connection factory

QueueConnectionFactory conFactory = (QueueConnectionFactory) context
.lookup("java:comp/env/queue/connectionFactory");

// create a queue connection

QueueConnection queConn = conFactory.createQueueConnection();

// create a queue session

QueueSession queSession = queConn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

// create a queue receiver

QueueReceiver queReceiver = queSession.createReceiver(queue);

// start the connection

queConn.start();

// receive a message

TextMessage message = (TextMessage) queReceiver.receive();

// print the message

out.write("Message Received: " + message.getText());

// close the queue connection

queConn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

=====================================================

配置tomcat的tomcat\conf\context.xml

<Resource name="queue/connectionFactory"

auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker" />
<Resource name="queue/queue0"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="My Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="TomcatQueue" />

=======================================================

启动ActiveMQ 进入http://localhost:8161/admin/queues.jsp 

默认用户名和密码是admin

运行后效果:

 

转载于:https://www.cnblogs.com/austinspark-jessylu/p/6285839.html

你可能感兴趣的文章
centos6.8下安装matlab2009(图片转帖)
查看>>
Mongo自动备份
查看>>
enq: SQ - contention
查看>>
cer证书签名验证
查看>>
ant 安装
查看>>
新手Python第一天(接触)
查看>>
iOS中ARC内部原理
查看>>
【bzoj1029】[JSOI2007]建筑抢修
查看>>
synchronized
查看>>
你不得不了解的应用容器引擎---Docker
查看>>
easyui datagrid 弹出页面会出现两个上下滚动条处理办法!
查看>>
迭代器和生成器
查看>>
codevs 1080 线段树练习
查看>>
JS模块化库seajs体验
查看>>
Android内核sysfs中switch类使用实例
查看>>
POJ2288 Islands and Bridges(TSP:状压DP)
查看>>
[No0000195]NoSQL还是SQL?这一篇讲清楚
查看>>
IOS开发UI篇--UITableView的自定义布局==xib布局
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
Python-Web框架的本质
查看>>