当前位置: 首页 > news >正文

濮阳做网站的公司电商关键词一般用哪些工具

濮阳做网站的公司,电商关键词一般用哪些工具,个人网站做cpa,淘宝客网站建设难度大吗SpringBoot集成消息处理框架 Spring framework提供了对JMS和AMQP消息框架的无缝集成,为Spring项目使用消息处理框架提供了极大的便利。 与Spring framework相比,Spring Boot更近了一步,通过auto-configuration机制实现了对jms及amqp主流框架…

SpringBoot集成消息处理框架

Spring framework提供了对JMS和AMQP消息框架的无缝集成,为Spring项目使用消息处理框架提供了极大的便利。

与Spring framework相比,Spring Boot更近了一步,通过auto-configuration机制实现了对jms及amqp主流框架如ActiveMQ、RabbitMQ以及Kafka的自动配置,应用层开发过程中无需自己配置,只要classpath下加入了相应的消息处理框架包,Spring Boot会自动完成加载,程序员直接就可以使用,简直不能再方便了。

JMS

The Java Message Service (JMS) API is a messaging standard that allows application components based on the Java Platform Enterprise Edition (Java EE) to create, send, receive, and read messages. It enables distributed communication that is loosely coupled, reliable, and asynchronous.

JMS是java message service的简称,是一个基于java的消息处理规范,允许基于JAVA EE平台的应用组件创建、发送、接收、读取消息。使得分布式通讯耦合度更低、更加可靠、异步处理。

JMS提供两种编程模式:

Point-to-Point—Messages are sent to a single consumer using a JMS queue.
Publish and Subscribe—Messages are broadcast to all registered listeners through JMS topics.

点对点通讯:消息通过队列发送给单一的消费者。
发布订阅模式:消息通过主题以广播的形式发送给所有的订阅者。

JMS规范规定了5种不同类型的消息体:
在这里插入图片描述

  1. StreamMessage:流式消息,顺序读取。
  2. MapMessage:键值对消息,可顺序读取,也可以通过键随机读取。
  3. TextMessage:文本消息,当初制定规范时候认为xml会成为最主流的消息载体,通过TextMessage可以发送xml格式的文本数据。
  4. ObjectMessage:对象消息,java对象序列化后发送。
  5. BytesMessage:字节消息。

JMS API对消息的发送、接收、存储等操作做了约定,每一个JMS消息框架的提供者(实现者)都必须遵守这些约定。

ActiveMQ

ActiveMQ是Apache旗下的、基于JMS的一款开源消息处理中间件,官网介绍:

Apache ActiveMQ® is the most popular open source, multi-protocol, Java-based message broker. It supports industry standard protocols so users get the benefits of client choices across a broad range of languages and platforms. Connect from clients written in JavaScript, C, C++, Python, .Net, and more. Integrate your multi-platform applications using the ubiquitous AMQP protocol. Exchange messages between your web applications using STOMP over websockets. Manage your IoT devices using MQTT. Support your existing JMS infrastructure and beyond. ActiveMQ offers the power and flexibility to support any messaging use-case.

最流行的开源、多协议、基于JAVA的消息处理中间件。支持工业级协议所以用户可以从多语言、跨平台的客户端选择中受益。等等…

目前ActiveMQ有两个主流版本:
在这里插入图片描述

There are currently two “flavors” of ActiveMQ available - the well-known “classic” broker and the “next generation” broker code-named Artemis. Once Artemis reaches a sufficient level of feature parity with the “Classic” code-base it will become the next major version of ActiveMQ. Initial migration documentation is available as well as a development roadmap for Artemis.

等到代表“下一代”的Artemis成熟之后,就会替代“classic”成为ActiveMQ的主版本。

ActiveMQ的下载安装

官网找一个合适的版本下载安装即可,非常简单。

安装后提供了一个管理端口:
在这里插入图片描述
可以通过管理端口做测试,管理端口是8161,而默认的MQ服务端口是61616。

SpringBoot项目自动配置ActiveMQ

首先初步了解一下SpringBoot对ActiveMQ的集成情况,轻车熟路的,检查一下auto-configuration:

在这里插入图片描述
找到这个JmsAutoConfiguration类:

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Message.class, JmsTemplate.class })
@ConditionalOnBean(ConnectionFactory.class)
@EnableConfigurationProperties(JmsProperties.class)
@Import(JmsAnnotationDrivenConfiguration.class)
public class JmsAutoConfiguration {

很明显,他就是SpringBoot的自动配置类,如果classpath下存在Message.class, JmsTemplate.class 类、以及Spring容器中存在ConnectionFactory Bean的话就会被启用。检查一下代码发现他会自动装配JmsTemplate、JmsMessagingTemplate等对象到Spring IoC容器中。

SpringBoot项目引入ActiveMQ

POM文件引入:

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency>

以上starter包含了spring-jms以及activemq的相关配置,所以通过以上对auto-configuration的分析,JmsTemplate以及JmsMessagingTemplate等相关组件会被SpringBoot自动配置好,后面我们就可以直接拿来使用了。

ps:JmsTemplate组件的自动配置过程源码也比较复杂,今天暂时不涉及。

到此,其实我们并没做什么具体的工作,但是ActiveMQ已经准备好了,我们项目中就可以直接使用JmsTemplate收发消息了。

消息发送

在application.yml文件中加入activeMQ的配置:

spring:activemq:broker-url: tcp://localhost:61616user: adminpassword: admin

消息发送类中通过自动装配引入JmsTemplate:

    @Autowiredprivate JmsTemplate jmsTemplate;

消息发送方法:

    public void sendMessage(String message){jmsTemplate.send("active.myqueue", new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {// 也可以创建对象 session.createObjectMessage()TextMessage textMessage = session.createTextMessage();textMessage.setText(message);return textMessage;}});}

Controller中加入对sendMessage方法的调用,以便测试:

    @GetMapping ("/sendmessage/{msg}")public String sendMessage(@PathVariable String msg){userService.sendMessage(msg);return "hello";}

消息接收

同样,消息接收方法中通过自动装配引入JmsTemplate。

消息接收方法:

    public String revieveMessage(){String message = (String)jmsTemplate.receiveAndConvert("active.myqueue");log.info("revieved message:"+message);return message;}

Controller方法中加入接收方法以便测试:

    @GetMapping ("/recievemsg")public String recieveMessage(){return userService.revieveMessage();}

这样,activeMQ的“点对点模式”收发消息的代码准备完毕,非常简单。

验证

首先启动activeMQ,启动之后在ActiveMQ的管理端监控队列(截图之前我已经测试过一次消息收发了,如果没测试过的话,队列是空的,不会存在active.myqueue这个队列):
在这里插入图片描述
启动我们刚才创建的测试应用,测试发送消息:
在这里插入图片描述
发送消息 1230230,收到反馈“hello”,说明消息发送成功。

我们从ActiveMQ管理端查看队列:
在这里插入图片描述
Number of pending Message 队列中尚未消费的消息数量为1,说明我们刚才的消息已经成功发送到队列中了。

消息接收测试:
在这里插入图片描述
成功接收到消息1230230。

再次从ActiveMQ管理端验证:
在这里插入图片描述
队列中的pending message数量变为0,入队数量和出队数量都为1,说明刚才的消息已经被成功消费。

此时,队列中尚未消费的数量为0的情况下,如果再次执行消息消费方法(recievemsg方法),消费方法会阻塞等待,直到再次调用消息发送方法发送一条新消息到队列、消费方法获取到新消息后结束阻塞等待。

监听器方式接收消息

发布订阅模式与点对点模式的区别主要是在消息接收端,Spring提供了接收消息的注解@JmsListener。

@JmsListener需要与@Component家族的注解结合使用,UserService中编写两个listener监听active.listenqueue::


@Service
@Slf4j
public class UserService {@Autowiredprivate JmsTemplate jmsTemplate;@JmsListener(destination = "active.listenqueue")public void revieveMsgListener(String content){log.info("revieveMsgListener:"+ content);}@JmsListener(destination = "active.listenqueue")public void revieveMsgListenerA(String content){log.info("revieveMsgListenerA"+content);}

UserService编写一个向该ActiveMQ发送消息的方法:

 public void sendMessage(String message){jmsTemplate.send("active.listenqueue", new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {// 也可以创建对象 session.createObjectMessage()TextMessage textMessage = session.createTextMessage();textMessage.setText(message);return textMessage;}});}

Controller中调用发送方法:

    @GetMapping ("/sendmessage/{msg}")public String sendMessage(@PathVariable String msg){userService.sendMessage(msg);return "hello";}

}

启动activeMQ,启动项目之后,查看activemq的admin端:
在这里插入图片描述

active.listenqueue以及2个监听器已经注册到ActiveMQ中,发送消息:
在这里插入图片描述

检查应用已经接收到了消息:
在这里插入图片描述
但是只有一个监听器接收到了消息,反复发送消息,后台log发现两个监听器轮番收到消息、但是一条消息不能被两个监听器同时接收到:
在这里插入图片描述

发布订阅模式

发布订阅模式下,消息发送给topic,订阅者仅订阅感兴趣的topic内的消息,消息消费完成后并不会从topic中消失,多个消费者可以从同一个topic内消费消息,所以,一条消息允许被多次消费。

默认情况下,SpringBoot集成ActiveMQ采用的是点对点队列模式,application.yml文件配置 spring:jms:pub-sub-domain参数为true开启topic模式:

spring:activemq:broker-url: tcp://localhost:61616user: adminpassword: adminjms:pub-sub-domain: true

启动activeMQ,启动项目,topic下看到项目中启动的两个topic:
在这里插入图片描述
调用sendmessage方法发送消息:
在这里插入图片描述
检查ActiveMQ状态,可以发现1条消息成功发送到队列中,被2个消费者分别消费了一次、消息共被消费了2次:
在这里插入图片描述
检查log:
在这里插入图片描述
两个监听方法都接收到了消息。

OK,let’s say it’s a day!

http://www.dt0577.cn/news/35051.html

相关文章:

  • p2p网站建设方案企业seo优化服务
  • 中国建设银行重庆网站首页镇海seo关键词优化费用
  • 网站建设合同用交印花税草莓永久地域网名入2022
  • 网络营销公司主要做些什么网站优化效果
  • 门户网站建设要求宁波网站建设公司
  • 做网站卖成人用品怎么样网站推广seo教程
  • 国内做网站如何创建网页链接
  • 中华门窗网怎么做网站黑锋网seo
  • 专门提供做ppt小素材的网站我要安装百度
  • 河南做外贸网站的公司郑州网络营销推广机构
  • 上海远丰电商网站建设公司怎么样seoul
  • 全flash网站怎么申请网址
  • 金华市住房和城乡建设局网站网络营销培训机构
  • 成都市住房和城乡建设局官方网站app推广方案
  • 个人如何制作app上海网络seo
  • 电子商务网站建设类论文网站网络营销公司
  • 京东网站架构微信营销的方法有哪些
  • 网站维护和制作怎么做会计分录百度网站大全首页
  • 关于做网站的了解点微信群发软件
  • 实时爬虫网站是怎么做的网络营销网课
  • 成都电商网站制作2023百度秒收录技术
  • 成都公司网站设计搜索排名查询
  • wordpress调用主站的文章什么网站可以发布广告
  • 展示型企业网站开发台州网站建设方案推广
  • markdown做网站编辑器今日头条收录入口
  • 手机微网站建设网站建设是干嘛的
  • 公司支付网站款做凭证电商网站平台搭建
  • 韩国设计app网站有哪些产品宣传推广方式有哪些
  • 做网站必须先买域名吗排名优化外包公司
  • 太仓网站制作书生潍坊住房公积金