一、简介

1.什么是消息中间体

消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型,它可以在分布式环境下扩展进程间的通信。对于消息中间件,常见的角色有Producer(生产者)、Consumer(消费者)

2.什么是JMS

JMS(Java Messaging Service)是Java平台上有关面向消息中间件的技术规范,它便于消息系统中的Java应用程序进行消息交换,并且通过提供标准的产生、发送、接收消息的接口简化企业应用的开发。

JMS 定义了五种不同的消息正文格式,以及调用的消息类型,允许你发送并接收以一些不同形式的数据,提供现有消息格式的一些级别的兼容性。

TextMessage--一个字符串对象
​
MapMessage--一套名称-值对
​
ObjectMessage--一个序列化的 Java 对象
​
BytesMessage--一个字节的数据流
​
StreamMessage -- Java 原始值的数据流

3.JMS消息传递类型

对于消息的传递有两种类型:

一种是点对点的,即一个生产者和一个消费者对应

一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收

4.ActiveMQ下载与安装

(1)下载

官方网站下载:http://activemq.apache.org/download.html

(2)安装启动(Linux)

1.将apache-activemq-版本号-bin.tar.gz上传至服务器

2.解压此文件

tar  zxvf  apache-activemq-5.12.0-bin.tar.gz

3.为apache-activemq目录赋权

chmod 777 apache-activemq-5.12.0

4.启动

bin目录下运行:
​
./activemq start

二、Spring整合JMS

1.点对点模式

(1)消息生产者

①创建工程springjms_product

②导入pom依赖

    
<properties><spring.version>4.2.4.RELEASE</spring.version></properties>
​<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId><version>5.13.4</version></dependency></dependencies>

③创建spring配置文件

applicationContext-jms-producer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd">
​
​<context:component-scan base-package="com.yfy"></context:component-scan>
​
​<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.25.132:61616"/></bean>
​<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory"/></bean>
​<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --><property name="connectionFactory" ref="connectionFactory"/></bean><!--这个是队列目的地,点对点的  文本信息--><bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="queue_text"/></bean>
​
​
</beans>

④创建生产者类

QueueProduct.java

@Component
public class QueueProduct {
​@Autowiredprivate JmsTemplate jmsTemplate;
​@Autowiredprivate Destination queueTextDestination;
​public void sentTextMessag(final String message) {jmsTemplate.send(queueTextDestination, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage(message);}});}
}

⑤创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-jms-producer.xml")
public class Test {
​@Autowiredprivate QueueProduct queueProduct;
​@org.junit.Testpublic void testSendQueue() {queueProduct.sentTextMessag("hello queue");}
}

(2)消息消费者

①创建工程springjms_consumer

②导入pom依赖,和消息生产者一样

③创建spring配置文件

applicationContext-jms-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  <property name="brokerURL" value="tcp://192.168.25.132:61616"/></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  </bean>  <!--这个是队列目的地,点对点的  文本信息-->  <bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  <constructor-arg value="queue_text"/>  </bean>
​<!--这个是订阅模式  文本信息--><bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="topic_text"/></bean><!-- 我的监听类 --><bean id="myMessageListener" class="com.yfy.MyMessageListener"></bean><!-- 消息监听容器:队列 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueTextDestination" /><property name="messageListener" ref="myMessageListener" /></bean>
​
</beans>

④创建监听类

MyMessageListener.java

public class MyMessageListener implements MessageListener {
​@Overridepublic void onMessage(Message message) {TextMessage testMessage=(TextMessage)message;try {String text = testMessage.getText();System.out.println(text);} catch (JMSException e) {e.printStackTrace();}}
}

⑤创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-jms-consumer.xml")
public class Test {
​@org.junit.Testpublic void testReceive(){try {System.in.read();} catch (IOException e) {e.printStackTrace();}}
}

2.发布/订阅模式

(1)消息生产者

在product配置文件中加入

 <!--这个是订阅模式  文本信息--><bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="topic_text"/></bean>

TopicProduct.java

@Component
public class TopicProduct {
​@Autowiredprivate JmsTemplate jmsTemplate;
​@Autowiredprivate Destination topicTextDestination;
​public void sendMessage(String message) {jmsTemplate.send(topicTextDestination, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage(message);}});}
}

创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-jms-producer.xml")
public class Test {
​
​@Autowiredprivate TopicProduct topicProduct;
​@org.junit.Testpublic void TestSendTopic() {topicProduct.sendMessage("hello topic");}
}

(2)消息消费者

在consumer配置文件中加入

 <!-- 我的监听类 --><bean id="myMessage2Listener" class="com.yfy.MyMessage2Listener"></bean><!-- 消息监听容器:订阅模式 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="topicTextDestination" /><property name="messageListener" ref="myMessageListener" /></bean>

创建监听类

MyMessage2Listener.java

public class MyMessage2Listener implements MessageListener {
​@Overridepublic void onMessage(Message message) {TextMessage textMessage=(TextMessage)message;try {String text = textMessage.getText();System.out.println(text);} catch (JMSException e) {e.printStackTrace();}}
}

测试:同时运行三个消费者工程,在运行生产者工程,查看消费者工程的控制台输出

消息中间体activeMQ相关推荐

  1. java 消息机制 ActiveMQ入门实例

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt180 1.下载ActiveMQ  去官方网站下载:http://active ...

  2. CentOS源码安装消息队列ActiveMQ

    消息队列ActiveMQ介绍 JMS全称:Java Message Service中文:Java消息服务.JMS是java的一套API标准,最初的目的是为了是应用程序能够访问现有的MOM系统(MOM是 ...

  3. java jms消息队列_JMS消息队列ActiveMQ(发布/订阅模式)

    消费者1(Consumer)--订阅(subcribe)-->主题(Topic)package com.java1234.activemq2; import javax.jms.Connecti ...

  4. 消息队列 - ActiveMQ

    消息队列 - ActiveMQ 一.入门概述 1.在什么场景下使用消息中间件,为什么使用 2. 消息队列是什么 3. 去哪下 二.ActiveMQ的安装和控制台 1.Linux安装 2.Apache ...

  5. java分布式面试题之消息队列ActiveMQ部分

    java分布式面试题之消息队列ActiveMQ部分 java分布式面试题之消息队列ActiveMQ部分 1.如何使用ActiveMQ解决分布式事务? 在互联网应用中,基本都会有用户注册的功能.在注册的 ...

  6. 高可用、可扩展、稳定和安全的消息队列ActiveMQ特点分析

    ActiveMQ是Apache软件基金下的一个开源软件.是目前能力强劲.应用广泛的开源消息总线之一.它为企业消息传递提供高可用.出色性能.可扩展.稳定和安全保障.ActiveMQ的目标是在尽可能多的平 ...

  7. 深入浅出 消息队列 ActiveMQ(转)

    一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...

  8. 分布式消息通信ActiveMQ原理-持久化策略-笔记

    2019独角兽企业重金招聘Python工程师标准>>> 消息的持久化策略分析 消息持久性对于可靠消息传递来说是一种比较好的方法, 即时发送者和接受者不是同时在线或者消息中心在发送者发 ...

  9. 消息队列-ActiveMQ

    1 业务需求描述 举例描述: 再警情通报的业务时通过发送消息界面可以选择 警情联络,和船情通报两种消息 发送方式可分为 一对一发送:部门对部门.个人对个人 一对多发送:部门对多部门.个人对多人 2 功 ...

最新文章

  1. rocketmq-flink
  2. jni和java之间字符串的转换
  3. python结束if else_python | if else || where true 流程控制
  4. json中的值类型及输出对象的所有名称和对应的值
  5. PXE+Kickstart实现无人值守批量安装Linux
  6. 微软出品,文科生也能学得懂的Python免费入门视频
  7. 2019 - OO第一单元作业总结
  8. WordPress 安装主题时 提示 “无法创建目录”
  9. 源码编辑器怎么编出游戏_编辑游戏
  10. 计算机网络第七版 部分详细答案
  11. 跨网页的新手引导_用户体验之如何设计一个完美的新手引导流程?(附带案例)...
  12. 西门子200smart与8台v90伺服驱动器Profinet通讯,控制8台伺服电机
  13. 由旋转矩阵反算旋转角度
  14. 计算机信函 教案模板,一年级信息技术课教案模板三篇
  15. 微信授权 昵称显示微信用户、无头像
  16. 50句英语成语:字面和实际意思大不同
  17. 测试工程师需掌握的技能
  18. Python | 图片转文字
  19. ktor启动报错:Module function cannot be found for the fully qualified name 'ApplicationKt.module'
  20. css3中var函数

热门文章

  1. [mmu/cache]-cache在linux和optee中的应用-InProgress
  2. SQL注入是什么?如何防止?
  3. 【dfs】P1036 选数
  4. 2020-12-12(c++多维数组的反编译观察)
  5. 利用该0 day漏洞的攻击活动情况
  6. 【CTF大赛】陇剑杯-机密内存-解题过程分析
  7. vbs复制自己到tmp目录
  8. 设计模式C++实现(11)——装饰模式
  9. 7、Java Swing JTextArea:文本域组件。 JScrollPane:滚动窗口
  10. 1.16 Java的异常跟踪栈