创建一个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

运行后效果:

JMS ActiveMQ案例的更多相关文章

  1. JMS - ActiveMQ集成Spring

    下面是ActiveMQ官网提供的文档.http://activemq.apache.org/spring-support.html 下面是我添加的一些dependency: <!-- jms a ...

  2. SpringBoot使用JMS(activeMQ)的两种方式 队列消息、订阅/发布

    刚好最近同事问我activemq的问题刚接触所以分不清,前段时间刚好项目中有用到,所以稍微整理了一下,仅用于使用 1.下载ActiveMQ 地址:http://activemq.apache.org/ ...

  3. jms - activeMQ入门案例

    activeMQ入门案例 叨叨一波,很久没写博客了,最近比较慢,时间抽不出来,这个借口说的很尴尬...我知道作为一名合格的码农就必须养成每天一博客的习惯.希望友友们别像我这样懒,闲话不多时进入今天的主 ...

  4. (jms)ActiveMQ 安装配置.

    前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...

  5. Spring JMS ActiveMQ整合(转)

    转载自:http://my.oschina.net/xiaoxishan/blog/381209#comment-list ActiveMQ学习笔记(四)http://my.oschina.net/x ...

  6. Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)

    首先声明:以下内容均是在网上找别人的博客综合学习而成的,可能会发现某些代码与其他博主的相同,由于参考的文章比较多,这里对你们表示感谢,就不一一列举,如果有侵权的地方,请通知我,我可以把该文章删除. 1 ...

  7. JMS & ActiveMQ小结

    JMS简介 JMS源于企业应用对于消息中间件的需求,使应用程序可以借助消息进行异步处理而互不影响.Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法,使得Java程序能 ...

  8. JMS ActiveMQ研究文档

    1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...

  9. SpringBoot JMS(ActiveMQ) 使用实践

    ActiveMQ 1. 下载windows办的activeMQ后,在以下目录可以启动: 2. 启动后会有以下提示 3. 所以我们可以通过http://localhost:8161访问管理页面,通过tc ...

随机推荐

  1. SZU : A11 Sequence

    Description We are given a integer sequence, your job is find the length of the longest contiguous s ...

  2. Python网络爬虫

    http://blog.csdn.net/pi9nc/article/details/9734437 一.网络爬虫的定义 网络爬虫,即Web Spider,是一个很形象的名字. 把互联网比喻成一个蜘蛛 ...

  3. 关于C#时间格式化中的“f”

    示例: DateTime.Now.ToString("yyyyMMddHHmmssfff") 上面的示例就是将日期格式化到毫秒级.那么问题来了,格式化到微秒级.纳秒级怎么整?f又是 ...

  4. MVC 4 插件化架构简单实现

    转ASP.NET MVC 4 插件化架构简单实现-思路篇   用过和做过插件的都会了解插件的好处,园子里也有很多和讨论,但大都只些简单的加载程序集什么的,这里主要讨论的就是使用 ASP.NET MVC ...

  5. Excel报表开发(主要讲Excel的导入和导出)

    一.Excel数据导入 连接字符串Excel2003版: OleDbConnection conn = new OleDbConnection("provider=Microsoft.Jet ...

  6. iOS LBS相关: 定位和中国特色的位置偏移纠正

    LBS模块,首先当然是定位,获取自己所在的位置.主要用到的就是CLLocationManager,实例一个,然后调用startUpdatingLocation即可.其中可以指定精度CLLocation ...

  7. 安装dnvm

    打开powershell,运行:&{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.gith ...

  8. jQuery Fancybox插件介绍

    下面介绍一款jquery图片播放插件叫Fancybox,项目主页地址:http://fancybox.net/ Fancybox的特点如下: 1.可以支持图片.html文本.flash动画.ifram ...

  9. 基于easyui的webform扩展(续)

    基于easyui的webform扩展(续) 回顾 <前端基于easyui的mvc扩展>.<前端基于easyui的mvc扩展(续)>.<基于easyui的webform扩展 ...

  10. Web API CSRF保护实现

    Web API CSRF保护实现 这次自己实现了类似jQuery中ajax调用的方法,并且针对RESTFul进行了改造和集成,实现的A2D AJAX接口如下: $.ajax.RESTFulGetCol ...