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

网站建设公司活动排名优化seo

网站建设公司活动,排名优化seo,济南网站建设认可搜点网络,高清网站seo这篇文章,主要接收消息队列RabbitMQ七种模式之Publisher Confirms发布确认模式。 目录 一、消息队列 1.1、发布确认模式 1.2、案例代码 (1)引入依赖 (2)编写生产者【消息确认--单条确认】 (3&#xf…

这篇文章,主要接收消息队列RabbitMQ七种模式之Publisher Confirms发布确认模式。

目录

一、消息队列

1.1、发布确认模式

1.2、案例代码

(1)引入依赖

(2)编写生产者【消息确认--单条确认】

(3)编写生产者【消息确认--批量确认】

(4)编写生产者【消息确认--异步确认】


一、消息队列

1.1、发布确认模式

RabbitMQ消息队列中,生产者发送消息给RabbitMQ的时候,可能会出现发送失败的情况,如果不进行处理,此时这一条消息就将丢失。如何确保生产者一定能够将消息发送到RabbitMQ里面呢???

RabbitMQ提出了一种发布确认模式,这种模式大致思想是:生产者发送消息给RabbitMQ时候,如果RabbitMQ正确接收到消息后,需要发给一个ACK标识给生产者,生产者接收到ACK标记后,就可以确认这一条消息发送成功啦。如果生产者没有接收到ACK标识,则可以重复发送这一条消息给RabbitMQ,这就可以确保消息不丢失。

发布确认模式有三种实现,分别是:逐条确认机制、批量确认机制、异步确认机制。

1.2、案例代码

(1)引入依赖

<!-- 引入 RabbitMQ 依赖 -->
<dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.16.0</version>
</dependency>

(2)编写生产者【消息确认--单条确认】

  • 生产者发送消息的时候,需要调用【confirmSelect()】方法开启消息确认机制。
  • 生产者将消息发送完成之后,需要调用【waitForConfirms()】方法,阻塞等待RabbitMQ消息队列返回ACK标识。这个方法返回一个boolean类型,true表示RabbitMQ接收消息成功,false表示接收失败。
  • 【waitForConfirms()】方法还可以指定一个超时时间,如果在这个超时时间里面RabbitMQ还没有返回ACK标识,那么该方法将抛出一个InterruptedException中断异常。
package com.rabbitmq.demo.confirm;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;/*** @version 1.0.0* @Date: 2023/2/25 16:23* @Copyright (C) ZhuYouBin* @Description: 消息生产者*/
public class Producer {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory = new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost("127.0.0.1"); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection = null; // 连接Channel channel = null; // 通道try {connection = factory.newConnection();// 4、获取通道channel = connection.createChannel();// TODO 开启消息确认机制channel.confirmSelect();// 5、声明 Exchange,如果不存在,则会创建String exchangeName = "exchange_direct_2023";channel.exchangeDeclare(exchangeName, "direct");// 6、发送消息for (int i = 0; i < 10; i++) {// 路由键唯一标识String routingKey = "error";if (i % 3 == 0) {routingKey = "info";} else if (i % 3 == 1) {routingKey = "warn";}String message = "这是发布确认模式,发送的第【" + (i+1) + "】条【" + routingKey + "】消息数据";channel.basicPublish(exchangeName, routingKey, null, message.getBytes());// 等待RabbitMQ返回ACK标识boolean wait = channel.waitForConfirms();System.out.println("RabbitMQ是否接收成功: " + wait);if (!wait) {// 消息发送失败,则可以重新发送channel.basicPublish(exchangeName, routingKey, null, message.getBytes());}}} catch (Exception e) {e.printStackTrace();} finally {if (null != channel) {try {channel.close();} catch (Exception e) {}}if (null != connection) {try {connection.close();} catch (Exception e) {}}}}
}

(3)编写生产者【消息确认--批量确认】

  • 前一种方式,是一条消息就调用一次【waitForConfirms()】方法,阻塞等待RabbitMQ的ACK确认标识。
  • 但是这种方式是非常耗时的,当需要发送的消息非常多的时候,会严重影响系统性能,所以为了解决这个问题,提出了批量确认的方法。
  • 批量确认调用【waitForConfirmsOrDie()】方法,此时会等待一批消息的ACK确认标识,如果这一批消息中存在一个消息没有被RabbitMQ成功接收,此时该方法将抛出一个【IOException】异常。
  • 所以,可以通过捕获IOException异常来判断消息是否发送成功。
  • 这种方式的缺点:当一批消息出现失败的情况时候,我们没办法知道是哪一条消息失败了,只能够重新将这一批消息重新发送。
package com.rabbitmq.demo.confirm;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.io.IOException;/*** @version 1.0.0* @Date: 2023/2/25 16:23* @Copyright (C) ZhuYouBin* @Description: 消息生产者*/
public class ProducerBatch {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory = new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost("127.0.0.1"); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection = null; // 连接Channel channel = null; // 通道try {connection = factory.newConnection();// 4、获取通道channel = connection.createChannel();// TODO 开启消息确认机制channel.confirmSelect();// 5、声明 Exchange,如果不存在,则会创建String exchangeName = "exchange_direct_2023";channel.exchangeDeclare(exchangeName, "direct");// 6、发送消息int batchSize = 3;int count = 0;for (int i = 0; i < 10; i++) {// 路由键唯一标识String routingKey = "error";if (i % 3 == 0) {routingKey = "info";} else if (i % 3 == 1) {routingKey = "warn";}String message = "这是发布确认模式,发送的第【" + (i+1) + "】条【" + routingKey + "】消息数据";channel.basicPublish(exchangeName, routingKey, null, message.getBytes());// 批量确认if (count == batchSize) {// 等待RabbitMQ返回ACK标识channel.waitForConfirmsOrDie();count = 0;}count++;}} catch (IOException e) {System.out.println("消息发送失败啦");} catch (Exception e) {e.printStackTrace();} finally {if (null != channel) {try {channel.close();} catch (Exception e) {}}if (null != connection) {try {connection.close();} catch (Exception e) {}}}}
}

(4)编写生产者【消息确认--异步确认】

  • 异步确认在消息发送之后,调用【addConfirmListener()】方法,该方法介绍两个参数,第一个参数是成功接收到ACK标识的回调方法,第二个参数是失败接收到NACK标识的回调方法。
  • 注意:一定要先调用【addConfirmListener()】监听方法,然后再发送消息,如果两者顺序反了,则监听方法不生效。
package com.rabbitmq.demo.confirm;import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmCallback;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;import java.io.IOException;/*** @version 1.0.0* @Date: 2023/2/25 16:23* @Copyright (C) ZhuYouBin* @Description: 消息生产者*/
public class ProducerAsync {public static void main(String[] args) {// 1、创建连接工厂ConnectionFactory factory = new ConnectionFactory();// 2、设置连接的 RabbitMQ 服务地址factory.setHost("127.0.0.1"); // 默认就是本机factory.setPort(5672); // 默认就是 5672 端口// 3、获取连接Connection connection = null; // 连接Channel channel = null; // 通道try {connection = factory.newConnection();// 4、获取通道channel = connection.createChannel();// TODO 开启消息确认机制channel.confirmSelect();// 5、声明 Exchange,如果不存在,则会创建String exchangeName = "exchange_confirm_2023";channel.exchangeDeclare(exchangeName, "direct");// TODO 一定要先调用监听接口,在发送消息channel.addConfirmListener(new ConfirmCallback() {@Overridepublic void handle(long deliveryTag, boolean multiple) throws IOException {System.out.println("RabbitMQ接收成功啦.....消息的标识deliveryTag=" + deliveryTag + ",批量发送多条消息multiple=" + multiple);}}, new ConfirmCallback() {@Overridepublic void handle(long deliveryTag, boolean multiple) throws IOException {System.out.println("RabbitMQ接收失败啦.....");}});for (int i = 0; i < 10; i++) {// 6、发送消息String message = "这是发布确认模式,发送的消息数据";channel.basicPublish(exchangeName, "queue_confirm_2023", null, message.getBytes());}} catch (IOException e) {System.out.println("消息发送失败啦");} catch (Exception e) {e.printStackTrace();} finally {if (null != channel) {try {channel.close();} catch (Exception e) {}}if (null != connection) {try {connection.close();} catch (Exception e) {}}}}
}

到此,RabbitMQ消息队列中的发布确认模式就介绍完啦。

综上,这篇文章结束了,主要接收消息队列RabbitMQ七种模式之Publisher Confirms发布确认模式。


文章转载自:
http://splitter.rywn.cn
http://ressentiment.rywn.cn
http://autochanger.rywn.cn
http://razzberry.rywn.cn
http://originality.rywn.cn
http://demarcate.rywn.cn
http://recalescence.rywn.cn
http://objectivize.rywn.cn
http://gabun.rywn.cn
http://inorganization.rywn.cn
http://hideously.rywn.cn
http://fissile.rywn.cn
http://avaricious.rywn.cn
http://comply.rywn.cn
http://externality.rywn.cn
http://lawbook.rywn.cn
http://waywardness.rywn.cn
http://impercipience.rywn.cn
http://gript.rywn.cn
http://specially.rywn.cn
http://outset.rywn.cn
http://lossmaker.rywn.cn
http://photoset.rywn.cn
http://permeation.rywn.cn
http://dingle.rywn.cn
http://terrorist.rywn.cn
http://moralization.rywn.cn
http://undispersed.rywn.cn
http://trangam.rywn.cn
http://surinamer.rywn.cn
http://purr.rywn.cn
http://inferable.rywn.cn
http://spermatoblast.rywn.cn
http://coronary.rywn.cn
http://momentary.rywn.cn
http://recorder.rywn.cn
http://landmine.rywn.cn
http://forestry.rywn.cn
http://triangular.rywn.cn
http://expandable.rywn.cn
http://cacophonous.rywn.cn
http://revivification.rywn.cn
http://unauthoritative.rywn.cn
http://multicide.rywn.cn
http://dermonecrotic.rywn.cn
http://shinbone.rywn.cn
http://leucovorin.rywn.cn
http://goblinry.rywn.cn
http://ogrish.rywn.cn
http://linguodental.rywn.cn
http://improperly.rywn.cn
http://slushy.rywn.cn
http://bushtailed.rywn.cn
http://desegregate.rywn.cn
http://nonetheless.rywn.cn
http://laciness.rywn.cn
http://iridocapsulitis.rywn.cn
http://clever.rywn.cn
http://whosit.rywn.cn
http://term.rywn.cn
http://latania.rywn.cn
http://finder.rywn.cn
http://blewits.rywn.cn
http://jellify.rywn.cn
http://zhujiang.rywn.cn
http://realizable.rywn.cn
http://jacamar.rywn.cn
http://zooecology.rywn.cn
http://wertherian.rywn.cn
http://footmark.rywn.cn
http://manganic.rywn.cn
http://idealist.rywn.cn
http://marrowy.rywn.cn
http://celticist.rywn.cn
http://granadilla.rywn.cn
http://trotskyite.rywn.cn
http://sousse.rywn.cn
http://bedtick.rywn.cn
http://rulebook.rywn.cn
http://dicing.rywn.cn
http://idiolect.rywn.cn
http://crummie.rywn.cn
http://supersystem.rywn.cn
http://innutritious.rywn.cn
http://hypothenar.rywn.cn
http://unminished.rywn.cn
http://pediatry.rywn.cn
http://lymphangitis.rywn.cn
http://recommencement.rywn.cn
http://zamouse.rywn.cn
http://wineskin.rywn.cn
http://munitionment.rywn.cn
http://magnesuim.rywn.cn
http://gluconate.rywn.cn
http://panax.rywn.cn
http://drave.rywn.cn
http://clothe.rywn.cn
http://nereus.rywn.cn
http://clothesbag.rywn.cn
http://scorification.rywn.cn
http://www.15wanjia.com/news/70219.html

相关文章:

  • 类似freenom的免费域名网站制作网站平台
  • 360网站建设公司公司网站建站要多少钱
  • 给私人企业做网站推广手机黄页怎么找
  • 做与食品安全有关的网站什么网站可以免费推广
  • 在荔浦找事情做投简历那个网站seo与网络推广的区别和联系
  • 做网站用什么面板好软广告经典案例
  • 苏州做网站比较好的公司百度导航2023年最新版
  • 北京市两学一做网站广告优化
  • 设计师招聘网成都最好的seo外包
  • 百度文库怎么做网站排名关键词优化和seo
  • 网站是用什么技术做的济南seo快速霸屏
  • 怎么用css做网站网站设计开发网站
  • 电商网站设计规划书太原seo关键词排名优化
  • 一级域名做网站的好处注册推广
  • 网站怎么做动态背景图片有人看片吗免费观看视频
  • wordpress今天更新文章数深圳seo关键词优化外包公司
  • 徐州网站优化百度seo公司兴田德润
  • 方庄网站建设公司网页怎么做
  • 中山市饮食网站建设seo排名策略
  • 北京网站建设招聘2023新一轮病毒叫什么名字
  • 郑州网站制作电话新产品的推广销售方法
  • 视频付费点播网站怎么做吉林网站推广公司
  • 嘉兴网站建设公司营销软文范例
  • 宿州公司做网站百度健康
  • 广州市城市建设档案馆网站企业网站快速排名
  • 如何评判网站建设岗位b站推广入口
  • 政府网站建设 会议纪要高清的网站制作
  • 配件查询网站制作蚁坊软件舆情监测系统
  • 网站建设 首选百川互动怎么自己注册网站
  • 罗湖中心区做网站哪家竞价托管专业