版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
开发包及源码下载地址:http://activemq.apache.org/activemq-5111-release.html
下载 apache-activemq-5.11.1-bin压缩包,解压,运行bin下的.bat文件,选择与操作系统对应的;(闪屏即关闭的应该是环境变量有问题,扔到桌面上启动);
ActiveMQ 服务启动地址:http://127.0.0.1:8161/admin/ 用户名/密码 admin/admin,
在界面上主要看queues和Topics,可以看到未消费的数据量和已消费数据量,以及消费者第几次消费
添加依赖包:activemq-all-5.11.1.jar
Receive 方式:
消息生产者:
package com.java.active;
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;
/**
* 消息生产者
*
* @author zhao
*
*/
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 SENNUM = 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(true, Session.AUTO_ACKNOWLEDGE);// 创建Session,这里设置true是指使用事务
destination = session.createQueue("FirstQueue1");// 创建消息队列
messageProducer = session.createProducer(destination);// 创建消息生产者
sendMessage(session, messageProducer);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
public static void sendMessage(Session session,
MessageProducer messageProducer) throws JMSException {
for (int i = 0; i < JMSProducer.SENNUM; i++) {
TextMessage message = session.createTextMessage("ActiveMQ 发送的消息 "
+ i);
System.out.println("发送消息" + "ActiveMQ 发送的消息 " + i);
messageProducer.send(message);
}
}
}
消息消费者:
package com.java.active;
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;
public class JSMConsumer {
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) {
ConnectionFactory connectionFactory;// 连接工厂
Connection connection = null;// 连接
Session session;// 会话,接受或者发送消息的线程
Destination destination;// 消息的目的地
MessageConsumer messageConsumer;// 消息消费者
// 实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(JSMConsumer.USERNAME,
JSMConsumer.PASSWORD, JSMConsumer.BROKEURL);
try {
connection = connectionFactory.createConnection();
connection.start();// 启动连接
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);// 创建Session,设置false不使用事务
destination = session.createQueue("FirstQueue1");// 创建连接的消息队列
messageConsumer = session.createConsumer(destination);
while (true) {
TextMessage textMessage = (TextMessage) messageConsumer
.receive(10000);
if (textMessage != null) {
System.out.println("接收到消息:" + textMessage.getText());
} else {
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}// 通过连接工厂获取连接
}
}
生产开发一般使用监听;使用一下两段代码
消费者:
package com.java.active;
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 org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JSMConsumer2 {
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) {
ConnectionFactory connectionFactory;// 连接工厂
Connection connection = null;// 连接
Session session;// 会话,接受或者发送消息的线程
Destination destination;// 消息的目的地
MessageConsumer messageConsumer;// 消息消费者
// 实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(JSMConsumer2.USERNAME,
JSMConsumer2.PASSWORD, JSMConsumer2.BROKEURL);
try {
connection = connectionFactory.createConnection();
connection.start();// 启动连接
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);// 创建Session,设置false不使用事务
destination = session.createQueue("FirstQueue1");// 创建连接的消息队列
messageConsumer = session.createConsumer(destination);
messageConsumer.setMessageListener(new Listener());
} catch (JMSException e) {
e.printStackTrace();
}// 通过连接工厂获取连接
}
}
监听:
package com.java.active;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* 消息监听
* @author zhao
*
*/
public class Listener implements MessageListener {
public void onMessage(Message message) {
try {
System.out.println("收到的消息:"+((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
以上简单理解ActiveMQ的工作方式,点对点的两种方式;
连接用户名,连接密码和连接地址都使用默认;null,null,failover://tcp://localhost:61616