Java消息服务(Java Message Service,JMS)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。
Java消息服务的规范包括两种消息模式,点对点和发布者/订阅者。
Java消息服务支持同步和异步的消息处理。
安装包:apache-activemq-5.13.1-bin.tar.gz
解压即可
#tar -zxvf apache-activemq-5.13.1-bin.tar.gz
bin目录下的activemq启动文件
#./activemq start
访问地址:http://192.168.150.138:8161/index.html
Sender.java
package com.caicongyang.demo; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Sender { public static void main(String[] args) { Sender sender = new Sender(); String msg = "Hello World!"; sender.send(msg); System.out.println("发送消息结束:" + msg); } public void send(String msg) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; // DEFAULT_BROKER_URL =failover://tcp://localhost:61616 String url = "failover://tcp://192.168.150.138:61616"; String subject = "TOOL.DEFAULT"; // 1. 初始化连接工厂 ConnectionFactory contectionFactory = new ActiveMQConnectionFactory(user, password, url); try { // 2. 创建连接 Connection connection = contectionFactory.createConnection(); connection.start(); // 3.创建会话 Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 4. 打开队列 Destination destination = session.createQueue(subject); // 5. MessageProducer负责发送消息 MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); for (int i = 0; i < 10; i++) { String tmp = i + ":" + msg; message.setStringProperty("hello", tmp); // 6. 发送消息 producer.send(message); System.out.println("send: " + tmp); Thread.sleep(3000); // 只有commit之后,消息才会进入队列 session.commit(); } // 7. 关闭会话和连接 session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }Receiver.java
package com.caicongyang.demo; 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 Receiver { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; // String url = ActiveMQConnection.DEFAULT_BROKER_URL; String url = "failover://tcp://192.168.150.138:61616"; String subject = "TOOL.DEFAULT"; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); Connection connection; try { connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(subject); // MessageConsumer负责接受消息 MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) { TextMessage message = (TextMessage) msg; try { String hello = message.getStringProperty("hello"); System.out.println("收到消息:\t" + hello); session.commit(); } catch (JMSException e) { e.printStackTrace(); } } }); // 为了演示接受消息,这里把关闭会话和连接注释掉了。 // session.close(); // connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }
package com.caicongyang.demo; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Publisher { public static void main(String[] args) { Publisher pb = new Publisher(); String msg = "Hello World!~~~~~"; pb.sendMessage(msg); System.out.println("发送消息结束:" + msg); } /** * 使用JMS向MQ发送消息 * * @param msg 消息内容 */ public void sendMessage(String msg) { // defualt user & password both are null String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; // DEFAULT_BROKER_URL =failover://tcp://localhost:61616 String url = "failover://tcp://192.168.150.138:61616"; String subject = "MQ.TOPIC"; // 1. 初始化连接工厂 ConnectionFactory contectionFactory = new ActiveMQConnectionFactory(user, password, url); try { // 2. 创建连接 Connection connection = contectionFactory.createConnection(); connection.start(); // 3.创建会话 Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 4. 创建要发布的主题,和Queue的区别就在此 Destination destination = session.createTopic(subject); // 5. MessageProducer负责发送消息 MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); message.setStringProperty("hello", msg); // 6. 发送消息 producer.send(message); // 7. 关闭会话和连接 session.commit(); session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }
package com.caicongyang.demo; import javax.jms.Connection; import javax.jms.ConnectionFactory; 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 javax.jms.Topic; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Subscriber { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; // String url = ActiveMQConnection.DEFAULT_BROKER_URL; String url = "failover://tcp://192.168.150.138:61616"; String subject = "MQ.TOPIC"; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); Connection connection; try { connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(subject); // MessageConsumer负责接受消息 MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) { TextMessage message = (TextMessage) msg; try { String hello = message.getStringProperty("hello"); System.out.println("订阅者---SecondSubscriber---收到消息:\t" + hello); session.commit(); } catch (JMSException e) { e.printStackTrace(); } } }); // 为了测试效果,注释掉了两行代码,使Session和connection一直处于打开状态 // session.close(); // connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }测试结果:
其他学习资源:
http://my.oschina.net/xiaoxishan/blog/378743
http://blog.csdn.net/column/details/activemq.html
http://www.cnblogs.com/chenying99/archive/2012/10/28/2744089.html(大全)
更多精彩内容请继续关注我的博客:http://blog.csdn.net/caicongyang