一、下载MQ:
官网http://activemq.apache.org/下载,选择windows版本学习,项目当中使用linux版本进行集群。
二、解压目录介绍
Bin:启动脚本.bin/activemq.bat是启动MQ的脚本
Conf:mq核心配置。
activemq.xml核心配置
Jetty:管控界面
Users.properties:用户名密码
Data:kahaDB文件
Webapps:管控台使用
三、启动MQ,管控台介绍
Home :查看broker相关信息
Queues:队列相关信息
Topics:主题订阅相关信息
Subscribers:订阅者相关信息
Connections:连接信息
Network:集群连接信息
Send:模拟发送MQ信息
----------------------------------------------------------------------------
heloword实例----------------------------------------------------------------------------
1、生产者端
/**
* Hello world!
*
*/
public class Sender {
public static void main( String[] args ) throws Exception
{
//建立connectionFactory工厂对象,填入用户名、密码以及要连接的地址,均使用默认即可,默认端口为"tcp://localhost:61616"
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
//通过connectionFactory工厂对象创建connection连接,并且调用connection的start方法开启连接,connection默认是关闭的
Connection connection = connectionFactory.createConnection(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD);
connection.start();
//通过connection对象创建session会话(上下文环境对象),用于接收消息,参一表示是否启用事务,参二表示签收模式,一般我们设置自动签收。
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
//通过session创建destination对象,指的是一个客户端用来指定生产消息目标和消费消息的来源的对象。
//在p2p模式中,destination被称作Queue即队列,在pub/sub模式中,destination被称作Topic即主题。
Destination destination = session.createQueue("first");
//通过session对象创建消息的发送/接收对象(生产者/消费者)MessageProducer/MessageConsumer
MessageProducer producer = session.createProducer(null);
for (int i = 0; i < 50; i++) {
//使用JMS规范的TextMessage形式创建数据(通过session对象),并用MessageProducer的send方法发送对象。同理客户端使用receive方法进行接收数据。最后关闭connection连接。
TextMessage msg = session.createTextMessage("消息内容"+i);
//一参 目标地址
//二参 具体数据信息
//三参 传送数据模式
//四参 优先级
//五参 消息过期时间
producer.send(destination,msg);
}
if(connection !=null){
connection.close();
}
}
}
运行消息结果:
2、消费端
public class Receiver {
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection connection = connectionFactory.createConnection(ActiveMQConnectionFactory.DEFAULT_USER,ActiveMQConnectionFactory.DEFAULT_PASSWORD);
// Connection connection = connectionFactory.createConnection("fu","fu");
connection.start();
Session session = connection.createSession(Boolean.FALSE, Session.CLIENT_ACKNOWLEDGE);
Destination destination = session.createQueue("first");
// 通过session对象创建消息的发送/接收对象(生产者/消费者)MessageProducer/MessageConsumer
MessageConsumer consumer = session.createConsumer(destination);
while (true) {
// 使用JMS规范的TextMessage形式创建数据(通过session对象),并用MessageProducer的send方法发送对象。同理客户端使用receive方法进行接收数据。最后关闭connection连接。
TextMessage msg = (TextMessage) consumer.receive();
// 手工签收消息
msg.acknowledge();
System.out.println("消费数据:" + msg.getText());
}
}
}
消费端运行结果:
- 大小: 29.7 KB
- 大小: 92.1 KB
- 大小: 62 KB
- 大小: 10.1 KB
- 大小: 64.9 KB
分享到:
相关推荐
一份详细ActiveMQ的使用教程
java中使用消息中间件ActiveMQ的MQTT协议发布消息使用fusesource,fusesource提供三种方式实现发布消息的方式,分别是阻塞式(BlockingConnection)、回调式(CallbackConnection)和Future样式(FutureConnection)
实现了 mq 的两种模式(点对点、发布订阅)。
该demo主要用于activeMQ初学,队列消息监听和Topic消息监听
ActiveMQ的安装与使用ActiveMQ的安装与使用ActiveMQ的安装与使用
activeMq in action 使用activeMq开发JMS的简单讲述,activeMq in action 使用activeMq开发JMS的简单讲述
ActiveMQ_使用failover模式进行连接切换时,线程断开 ,ActiveMQ_使用failover模式进行连接切换时,线程断开
activemq消息测试工具
包括1、ActiveMQ java实例 2、ActiveMQ Spring结合实例 3、代码亲测,无问题。 4、资源分5分绝对值 注意:请先安装ActiveMQ 服务。
ActiveMQ 的安装与使用,ActiveMQ与spring整合,生产都、消费者、测试类等。
使用ActiveMQ由C++控制台程序发送消息,C#控制台程序接收消息。
适用于Java后台开发消息队列ActiveMQ使用者,包括服务器的搭建以及ActiveMQ的三种使用模式
此demo结合了网上纷乱的资源进行讲解。少量操作即可直接跑通,适合初学者进行学习。所需jar包在demo中也都具有。
spring整合使用activemq,代码主要是介绍如何使用spring整合使用activemq,可以直接拿过去使用
ActiveMQ使用Ajax实现多人聊天室。
ActiveMQ开发库文件(Release版),需配合ActiveMQ开发实例系列其他资源使用
使用activemq依赖库连接, 该项目为java工程,内有ssl证书生成方式链接,不清楚可私信
消息中间件之ActiveMQ 消息中间件已经成为互联网企业应用系统内部通信的核心手段,是目前企业内主流标配技术, 它具有解耦、异步、削峰、签收、事务、流量控制、最终一致性等一系列高性能架构所需功能。 当前使用...
压缩包中activemq文件夹存放的是消息服务器的发送方源代码,发送模式采用队列模式,框架使用的是springboot ,logsystem 是消息的接收方源代码,框架采用的是ssh。在使用这套模拟系统之前,需要自行下载一个activemq...