Spring 消息系统 JMS 开发实践

MQ 消息队列

应用程序之间的一种通信方式。

消息生产者 -> MQ -> 消息消费者

使用场景:将一些无需即时返回且耗时的操作提取出来,进行异步处理

JMS Java Message Service

Java 平台中关于面向消息中间件的 API,即技术规范。

RabbitMQ

RabbitMQ is the most widely deployed open source message broker.

RabbitMQ 在 MAC 上的安装与启动

通过 Homebrew 来安装:brew install rabbitmq
安装到目录:/usr/local/Cellar/rabbitmq/3.7.7

启动 RabbitMQ 服务sudo sbin/rabbitmq-server 默认端口 5672。
在单机上启动的时候可能会碰到问题,这里需要禁用几个插件:

.sbin/rabbitmq-plugins disable rabbitmq_stomp
.sbin/rabbitmq-plugins disable rabbitmq_mqtt
启动日志

对应两个日志文件:

  • /usr/local/var/log/rabbitmq/rabbit@localhost.log
  • /usr/local/var/log/rabbitmq/rabbit@localhost_upgrade.log

管理界面
在浏览器中输入:http://127.0.0.1:15672 默认用户名密码都为 guest

RabbitMQ 管理界面

ActiveMQ

Apache ActiveMQ ™ is the most popular and powerful open source messaging and Integration Patterns server.

ActiveMQ 是 JMS 的一种实现。

ActiveMQ 在 MAC 上的安装与启动

下载:http://activemq.apache.org/download.html

本地解压并移动到 /usr/local

tar zxvf apache-activemq-5.15.4-bin.tar.gz
sudo mv apache-activemq-5.15.4 /usr/local
cd /usr/local/apache-activemq-5.15.4
cd bin/macosx

启动 ActiveMQ 服务/activemq start 默认端口 61616。

启动 ActiveMQ 服务

管理界面
在浏览器中输入:http://127.0.0.1:8161/admin/ 默认用户名密码都为 admin

ActiveMQ 管理界面

  • Queues:显示 队列 方式的消息
  • Topics:显示 主题 方式的消息

ActiveMQ 消息有 3 种形式

  • JMS 公共
  • 点对点方式 point-to-point
    • 即上述所说的 队列 方式
    • Sender -> MQ -> Receiver
    • 没有时间上的依赖,Sender 无需知道 Receiver 是否在运行
  • 发布/订阅方式 publish/subscribe
    • 即上述所说的 主题 方式
    • 用于多接收端
    • 有时间上的依赖,一个 Subscriber 只能接受它创建以后的 Publisher 的消息

ActiveMQ 的使用

创建工厂类 ConnectionFactory:提供 ActiveMQ Server 的连接信息。

得到 ConnectionConnection connection = connectionFactory.createConnection();

得到 SessionSession session = connection.createSession();

发送消息:使用 MessageProducer

Destination destination = session.createQueue("mq.test");
MessageProducer producer = session.createProducer(destination);

MapMessage message = session.createMapMessage();
message.setString("name", "Tom");

producer.send(message);
session.commit();

接受消息:使用 MessageConsumer

Destination destination = session.createQueue("mq.test");
MessageConsumer consumer = session.createConsumer(destination);

consumer.setMessageListener(new MessageListener() {
  public void onMessage(Message msg) {
    msg.getString("name");
  }
});

Spring 集成 ActiveMQ

Spring 配置

<!-- ActiveMQ Server 的连接 -->
<bean id="connectionFactory" class="ActiveMQConnectionFactory">
  <property name="brokerURL" value="tcp://localhost:61616" />
</bean>

<!-- 消息目标,例如上述的 mq.test -->
<bean id="destination" class="ActiveMQQueue">
  <constructor-arg index="0" value="mq.test" />
</bean>

<bean id="jmsTemplate" class="JmsTemplate">
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="defaultDestination" ref="destination" />
  <property name="messageConverter">
    <bean class="SimpleMessageConverter" />
  </property>
</bean>

发送消息

jmsTemplate.send(message);

接受消息

while(true) {
  Map msg =  (Map) jmsTemplate.receiveAndConvert();
  msg.getString("name");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ActiveMQ 即时通讯服务 浅析http://www.cnblogs.com/hoojo/p/active_m...
    bboymonk阅读 1,513评论 0 11
  • 1、前言 之前我们通过两篇文章(架构设计:系统间通信(19)——MQ:消息协议(上)、架构设计:系统间通信(20)...
    境里婆娑阅读 1,913评论 0 4
  • 一、 消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合、异步消息、流量削锋等问题。实现高性能...
    步积阅读 57,077评论 10 138
  • 1 消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,...
    Bobby0322阅读 10,926评论 0 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,891评论 18 139