1. 下载与运行activeMQ
http://activemq.apache.org/activemq-5143-release.html
下载apache-activemq-5.14.3-bin.zip
解压后运行 C:\javaweb\apache-activemq-5.14.3\bin\win64\activemq.bat
浏览器输入查看 http://127.0.0.1:8161,默认用户名admin,密码admin
JMS消息有2种类型
a). 点对点(Point-to-Point) ,消息发给一个单独的使用者,与队列 javax.jms.Queue相关联.
b). 发布/订阅(Publish/Subscribe),支持一个事件驱动模型,消息生产者和消费者都参与消息传递,与主题javax.jsm.Topic相关联。
2. 点对点实现方式实例
消息生产者代码实例
package com.yf.activemq;
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;
/**
* File: JMSProducer.java
* Description: 消息生产者
* @author
* @date 2017年1月29日下午8:03:36
* @version 1.0
**/
public class JMSProducer {
private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;//默认连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址
private static final int SENDNUM=10;//发送的消息数量
public static void main(String[] args) {
ConnectionFactory connectionFactory;//连接工厂
Connection connection = null;//连接
Session session;//会话 接收或者发送消息的线程
Destination destination;//消息的目的地
MessageProducer messageProducer;//消息生产者
//实例化工厂连接
connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
try {
connection = connectionFactory.createConnection();//通过连接工厂获取连接
connection.start(); //启动连接
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //创建session
destination = session.createQueue("FirstQueue1");//创建消息队列
messageProducer = session.createProducer(destination); //创建消息生产者
sendMessage(session,messageProducer);//发送消息
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void sendMessage(Session session,MessageProducer messageProducer){
for(int i=0;i<JMSProducer.SENDNUM;i++){
try {
TextMessage textMessage = session.createTextMessage("ActiveMQ 发送的消息 " + i);
System.out.println("发送消息: " + "ActiveMQ 发送的消息" + i);
messageProducer.send(textMessage);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
消息消费者代码实例:
package com.yf.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* File: JMSConsumer.java
* Description: 消息消费者
* @author
* @date 2017年1月29日下午8:22:08
* @version 1.0
**/
public class JMSConsumer {
private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;//默认连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认密码
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址
public static void main(String[] args) {
//failover://tcp://localhost:61616
ConnectionFactory connectionFactory;//连接工厂
Connection connection = null;//连接
Session session;//会话 接收或者发送消息的线程
Destination destination;//消息的目的地
MessageConsumer messageConsumer = null;//消息消费者
connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
try {
connection = connectionFactory.createConnection();//通过连接工厂获取连接
connection.start(); //启动连接
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); //创建session false=不需要事务
destination = session.createQueue("FirstQueue1");//创建连接的消息队列
messageConsumer = session.createConsumer(destination);//创建消息消费者
messageConsumer.setMessageListener(new Listener());//注册消息监听
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(connection != null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.yf.activemq;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.jms.JmsException;
/**
* File: Listener.java
* Description: 消息监听
* @author
* @date 2017年1月29日下午8:56:01
* @version 1.0
**/
public class Listener implements MessageListener{
@Override
public void onMessage(Message message) {
try{
System.out.println("收到的消息 :" + ((TextMessage)message).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3. 发布订阅模式实现
Producer修改代码
//destination = session.createQueue("FirstQueue2");//创建消息队列
destination = session.createTopic("FirstTopic"); //创建消息主题
Consumer修改代码
//destination = session.createQueue("FirstQueue2");//创建连接的消息队列
destination = session.createTopic("FirstTopic");//创建消息订阅者
必须先运行consumer来订阅,然后运行producer生产者来生产消息。