- 浏览: 696116 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (272)
- Struts1.x (7)
- 事务 (2)
- Hibernate (11)
- 数据库 (14)
- JavaScript&Ajax (43)
- JSP&Servlet (2)
- Flex (1)
- 其它 (9)
- Java (22)
- 框架集成 (1)
- WebService (3)
- Tomcat (3)
- 加密和安全登录 (13)
- 基于原型的JavaScript (0)
- JavaDoc和Java编码规范 (3)
- CAS (1)
- 加密 (1)
- Axis2 (10)
- Ext2.x (3)
- SSH整合 (2)
- Ext (0)
- 正则表达式 (1)
- 设计模式 (4)
- 对象序列化技术 (3)
- CVS (2)
- Struts2 (6)
- Spring 2.x (7)
- Spring Security (2)
- Java 课程 (20)
- 程序员之死 (1)
- 软件测试 (6)
- UML (5)
- NetBeans (1)
- cxf (1)
- JMS (13)
- 设计 (5)
- ibatis2.x (3)
- Oracle (1)
- WebSphere (7)
- 概要设计 (1)
- DB2 (10)
- PowerDesigner (0)
- 软件工程 (5)
- rose (1)
- EA (1)
- LDAP (7)
- Portal&Portlet (3)
- MQ (10)
- ESB (4)
- EJB (2)
- JBoss (2)
最新评论
-
typeRos:
只有配置文件,没有代码么大神
Spring实现IBMMQ的JMS消息发布/订阅模式 -
panamera:
如果ActiveMQ服务器没有启动,这个时候消息生产者使用Jm ...
Spring JMSTemplate 与 JMS 原生API比较 -
lian819:
顶1楼, 引用文件, 配置属性, 太方便了
EXTJS 同步和异步请求 -
wilhard:
说得清楚明白
<%@ include file=""%>与<jsp:include page=""/>区别 -
刘琛颖:
总结的很好。受益了
javascript 父窗口(父页面)— 子窗口 (子页面)互相调用的方法
原文地址:http://www.iteye.com/topic/275045
1.JMS介绍
JMS源于企业应用对于消息中间件的需求,使应用程序可以通过消息进行异步处理而互不影响。Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法,使得Java程序能够和其他消息组件进行通信。JMS有四个组成部分:JMS服务提供者、消息管理对象、消息的生产者消费者和消息本身。
1)JMS服务提供者实现消息队列和通知,同时实现消息管理的API。JMS已经是J2EE API的一部分,J2EE服务器都提供JMS服务。
2) 消息管理对象提供对消息进行操作的API。JMS API中有两个消息管理对象:创建jms连接使用的工厂(ConnectionFactory)和目的地(Destination),根据消息的消费方式的不同ConnectionFactory可以分为QueueConnectionFactory和TopicConnectionFactory,目的地(Destination)可以分为队列(Queue)和主题(Topic)两种。
3)消息的生产者和消费者。消息的产生由JMS的客户端完成,JMS服务提供者负责管理这些消息,消息的消费者可以接收消息。消息的生产者可以分为――点对点消息发布者(P2P)和主题消息发布者(TopicPublisher)。所以,消息的消费者分为两类:主题消息的订阅者(TopicSubscriber)和点对点消息的接收者(queue receiver)
4)消息。消息是服务提供者和客户端之间传递信息所使用的信息单元。JMS消息由以下三部分组成:
消息头(header)――JMS消息头包含了许多字段,它们是消息发送后由JMS提供者或消息发送者产生,用来表示消息、设置优先权和失效时间等等,并且为消息确定路由。
属性(property)――用来添加删除消息头以外的附加信息。
消息体(body)――JMS中定义了5种消息体:ByteMessage、MapMessage、ObjectMessage、StreamMessage和TextMessage。
2.Messages 通信方式
上面提到JMS通信方式分为点对点通信和发布/订阅方式
1)点对点方式(point-to-point)
点对点的消息发送方式主要建立在 Message Queue,Sender,reciever上,Message Queue 存贮消息,Sneder 发送消息,receive接收消息.具体点就是Sender Client发送Message Queue ,而 receiver Cliernt从Queue中接收消息和"发送消息已接受"到Quere,确认消息接收。消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行
2)发布/订阅 方式(publish/subscriber Messaging)
发布/订阅方式用于多接收客户端的方式.作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。作为subscriber ,在接收消息时有两种方法,destination的receive方法,和实现message listener 接口的onMessage 方法。
3.为什么选用ActiveMQ
1)ActiveMQ是一个开放源码
2)基于Apache 2.0 licenced 发布并实现了JMS 1.1。
3)ActiveMQ现在已经和作为很多项目的异步消息通信核心了
4)在很多中小型项目中采用ActiveMQ+SPRING+TOMCAT开发模式。
4.编程模式
4.1消息产生者向JMS发送消息的步骤
(1)创建连接使用的工厂类JMS ConnectionFactory
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息生产者MessageSender
(5)使用消息生产者MessageSender发送消息
4.2消息消费者从JMS接受消息的步骤
(1)创建连接使用的工厂类JMS ConnectionFactory
(2)使用管理对象JMS ConnectionFactory建立连接Connection
(3)使用连接Connection 建立会话Session
(4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver
(5)使用消息消费者MessageReceiver接受消息,需要用setMessageListener将MessageListener接口绑定到MessageReceiver
消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。
5.ActiveMQ运行
ActiveMQ5.0版本默认启动时,启动了内置的jetty服务器,提供一个demo应用和用于监控ActiveMQ的admin应用。运行%activemq_home%bin/目录下的 activemq.bat , 之后你会看见如下一段话表示启动成功。
打开http://localhost:8161/admin/queues.jsp ,可以查看相应的queue中是否有消息
6.SendMessage(用于发送消息)
import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class SendMessage { private static final String url ="tcp://localhost:61616"; private static final String QUEUE_NAME ="choice.queue"; protected String expectedBody = "<hello>world!</hello>"; public void sendMessage() throws JMSException{ Connection connection =null; try{ ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory(url); connection = (Connection)connectionFactory.createConnection(); connection.start(); Session session = (Session)connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(QUEUE_NAME); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(expectedBody); message.setStringProperty("headname", "remoteB"); producer.send(message); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ SendMessage sndMsg = new SendMessage(); try{ sndMsg.sendMessage(); }catch(Exception ex){ System.out.println(ex.toString()); } } }
7.ReceiveMessage(用于接收消息)
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class ReceiveMessage { private static final String url = "tcp://localhost:61616"; private static final String QUEUE_NAME = "choice.queue"; public void receiveMessage() { Connection connection = null; try { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); connection = connectionFactory.createConnection(); } catch (Exception e) { System.out.println(e.toString()); } connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(QUEUE_NAME); MessageConsumer consumer = session.createConsumer(destination); consumeMessagesAndClose(connection, session, consumer); } catch (Exception e) { System.out.println(e.toString()); } } protected void consumeMessagesAndClose(Connection connection,Session session, MessageConsumer consumer) throws JMSException { for (int i = 0; i < 1;) { Message message = consumer.receive(1000); if (message != null) { i++; onMessage(message); } } System.out.println("Closing connection"); consumer.close(); session.close(); connection.close(); } public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); System.out.println("Received: " + msg); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String args[]) { ReceiveMessage rm = new ReceiveMessage(); rm.receiveMessage(); } }
文章欢迎转载,但请注明出处和作者
发表评论
-
EJB3.0中的MessageDrivenBean
2012-06-25 15:56 977EJB3.0中MessageDrivenBean: ... -
Spring实现IBMMQ的JMS消息发布/订阅模式
2012-06-21 16:08 3109<?xml version="1.0" ... -
Spring JMSTemplate 与 JMS 原生API比较
2012-04-20 17:03 18265JMSUtil与Spring JmsTempl ... -
WAS7.0 + MQ 7.0 + JNDI 最简配置和收发示例
2012-04-12 17:29 57161、MQ上新建QM和MQ Q ... -
ActiveMQ实践:松耦合和ActiveMQ
2011-01-10 10:28 19212010-10-28 作者:B ... -
使用Spring JMS轻松实现异步消息传递
2011-01-07 17:31 3095异步进程通信是面向服务架构(SOA)一个重要的组成部分 ... -
Apache ActiveMQ教程(六)
2011-01-07 16:59 1867Apache ActiveMQ教程(六) 4 ... -
Apache ActiveMQ教程(五)
2011-01-07 16:58 1509Apache ActiveMQ教程(五) 2008-0 ... -
Apache ActiveMQ教程(四)
2011-01-07 16:57 1639七、ActiveMQ与Tomcat整合 说明:To ... -
Apache ActiveMQ教程(三)
2011-01-07 16:57 1236Apache ActiveMQ教程(三) 2008-0 ... -
Apache ActiveMQ教程(二)
2011-01-07 16:56 1528Apache ActiveMQ教程(二) 2008-0 ... -
Apache ActiveMQ教程(一)
2011-01-07 16:43 1903Apache ActiveMQ教程(一) 2008-0 ...
相关推荐
实战ActiveMQ集群与应用实战视频教程实战
1:ActiveMQ入门和消息中间件 2:JMS基本概念和模型 3:JMS的可靠性机制 4:JMS的API结构和开发步骤 5:Broker的启动方式 6:ActiveMQ结合Spring开发 7:ActiveMQ支持的传输协议 8:ActiveMQ消息存储持久化 9:...
activemq实战
ActiveMQ 实战。本手册介绍了JMS以及ActiveMQ的常用操作以及配置。可以作为ActiveMQ应用开发人员的参考手册。
ActiveMQ集群实战教程
activemq实战
activeMQ集群的使用与配置[收集].pdf
activeMQ集群的使用与配置[归类].pdf
ActiveMQ实战(英文版) ActiveMQ实战(英文版) ActiveMQ实战(英文版)
含如下内容:JMS API;Apache-activeMQ;ActiveMQ实战pdf
这个demo完整实现了activeMQ发送消息,接收消息,以及MQ详解文档
dubbo,zookeeper,activemq,redis,fastdfs实战操作文档 linux部署文档
【ActiveMQ】—01—实战ActiveMQ集群与应用.消息队列中间件ActiveMQ入门到精通视频教程及资料一头扎进JMS之ActiveMQ视频教程.
ActiveMq实战性视频
ActiveMQ RabbitMQ RokcetMQ Kafka实战 消息队列中间件视频教程
ActiveMQ 入门实战(3)--SpringBoot 整合 ActiveMQ(csdn)————程序
ACTIVEMQ实战部分翻译,只翻译了第一章到第四章的第三节
从activemq入门到 实战-activemq集群与应用,分布式。货真价实