本文出自:http://www.open-open.com/lib/view/open1388994166156.htmlhtml
1. JMS基本概念
JMS(Java Message Service) 即Java消息服务。它提供标准的产生、发送、接收消息的接口简化 企业 应用的开发。它支持两种消息通讯模型:点到点(point-to-point)(P2P)模型和发布/订阅(Pub/Sub)模型。P2P 模型规定了一个消息只能有一个接收者;Pub/Sub 模型容许一个消息能够有多个接收者。
对于点到点模型,消息生产者产生一个消息后,把这个消息发送到一个Queue(队列)中,而后消息接收者再从这个Queue中读取,一旦这个消息被一个接收者读取以后,它就在这个Queue中消失了,因此一个消息只能被一个接收者消费。 java
与点到点模型不一样,发布/订阅模型中,消息生产者产生一个消息后,把这个消息发送到一个Topic中,这个Topic能够同时有多个接收者在监听,当一个消息到达这个Topic以后,全部消息接收者都会收到这个消息。spring
2.编程的结构apache
2.1消息产生者向JMS发送消息的步骤
(1)建立链接使用的工厂类JMS ConnectionFactory
(2)使用管理对象JMS ConnectionFactory创建链接Connection
(3)使用链接Connection 创建会话Session
(4)使用会话Session和管理对象Destination建立消息生产者MessageSender
(5)使用消息生产者MessageSender发送消息
2.2消息消费者从JMS接受消息的步骤
(1)建立链接使用的工厂类JMS ConnectionFactory
(2)使用管理对象JMS ConnectionFactory创建链接Connection
(3)使用链接Connection 创建会话Session
(4)使用会话Session和管理对象Destination建立消息消费者MessageReceiver
(5)使用消息消费者MessageReceiver接受消息,须要用setMessageListener将MessageListener接口绑定到MessageReceiver
消息消费者必须实现了MessageListener接口,须要定义onMessage事件方法。编程
3.ActiveMQ的下载服务器
下载地址:http://activemq.apache.org/download.html
解压缩到本地,而后启动/bin/activemq.batsession
而且有的Eclipse没有自带jms.jar,须要去下载异步
4.发送端代码:tcp
package com.xkey.JMS; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.Topic; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class JmsSender { private ConnectionFactory connectionFactory = null; private Connection connection = null; private Session session = null; private Destination destination = null; private MessageProducer producer = null; JmsSender(){ } public void init(){ connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616"); try{ connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE.booleanValue(), Session.AUTO_ACKNOWLEDGE); //Queue destination = session.createQueue("xkey"); producer = session.createProducer(destination); //Topic /** * Topic topic = session.createTopic("xkey.Topic"); * producer = session.createProducer(topic); */ producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); sendMessage(session,producer); session.commit(); }catch(Exception e){ e.printStackTrace(); }finally{ try { connection.close(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void sendMessage(Session session,MessageProducer producer) throws JMSException{ for (int i = 1; i <= 5; i ++) { TextMessage message = session.createTextMessage("First ActiveMQ Test:::: " + i); // 发送消息 System.out.println("Sender:" + "First ActiveMQ Test::: " + i); producer.send(message); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JmsSender jms = new JmsSender(); jms.init(); } }
5.接收端代码 分布式
package com.xkey.JMS; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class JmsReceiver { private ConnectionFactory connectionFactory = null; private Connection connection = null; private Session session = null; private MessageConsumer consumer = null; private Destination destination = null; public JmsReceiver(){ } public void init(){ connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616"); try{ connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE.booleanValue(), Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("xkey"); consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener(){ @Override public void onMessage(Message msg) { // TODO Auto-generated method stub TextMessage message = (TextMessage)msg; try{ System.out.println("Receiver " + message.getText()); }catch(Exception e){ e.printStackTrace(); } } }); /**while (true) { TextMessage message = (TextMessage) consumer.receive(1000); if (null != message) { System.out.println("Receiver " + message.getText()); } else { break; } } */ }catch(Exception e){ e.printStackTrace(); }finally{ try { connection.close(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JmsReceiver jms = new JmsReceiver(); jms.init(); } }
六、JMS+ActiveMQ+Spring+TomCat能够用来实现企业级的消息队列,这样能使服务请求端和服务操做端实现低耦合。不知道怎么实现分布式,也就是多个请求,每一个请求用不一样的服务器去相响应。
Spring + JMS + ActiveMQ实现简单的消息队列(监听器异步实现)
链接: http://blog.csdn.net/acceptedxukai/article/details/7775746
在Tomcat 6.0下用JNDI链接IBM MQ 6.0的配置方法(一)
链接: http://blog.sina.com.cn/s/blog_60dadc490100ecy6.html
http://www.javawind.net/help/html/spring_ref_2.0/html/jms.html
ActiveMQ接收消息+发送消息的简单实例
ActiveMQ持久化方式
ActiveMQ持久化方式
activemq存储
activemq特性
java发送内嵌图片邮件
PHP图片上传与预览
CSS学习总结
Arcgis Android API开发之离线地图
Struts2自定义过滤器 + 百度富文本控件UEditor + Smb上传图片到独立服务器