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

织梦网站流动广告代码浙江网站推广运营

织梦网站流动广告代码,浙江网站推广运营,直播软件哪个好,网站的首页文案RabbitMQ 基本使用方法 在你的代码中,涉及到了 RabbitMQ 的基本使用,包括队列定义、交换机的配置、消息的发送与接收等内容。下面我将详细总结 RabbitMQ 的基本使用方法,重点解释如何在 Spring Boot 项目中与 RabbitMQ 集成。 1. 引入依赖 …

RabbitMQ 基本使用方法

在你的代码中,涉及到了 RabbitMQ 的基本使用,包括队列定义、交换机的配置、消息的发送与接收等内容。下面我将详细总结 RabbitMQ 的基本使用方法,重点解释如何在 Spring Boot 项目中与 RabbitMQ 集成。

1. 引入依赖

在 Spring Boot 项目中,使用 Spring AMQP 组件来集成 RabbitMQ。首先需要在 pom.xml 中添加相关的依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId>
</dependency>

该依赖会自动导入 Spring AMQP 库以及 RabbitMQ 的客户端,允许你在 Spring 环境下方便地使用 RabbitMQ。

2. 配置 RabbitMQ

首先,你需要配置 RabbitMQ 的相关参数(如连接信息、交换机、队列等)。在你的例子中,RabbitConfig 类就负责了这些配置。

package com.easylive.entity.config;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitConfig {@Beanpublic MessageConverter messageConverter() {// 使用自定义的消息转换器来更严格地处理反序列化return new Jackson2JsonMessageConverter();}// 队列定义@Beanpublic Queue transferFileQueue() {return new Queue("transferFileRouting", true); // durable 确保队列持久化}@Beanpublic Queue videoPlayQueue() {return new Queue("videoPlayRouting", true);}// Direct 类型交换机@Beanpublic DirectExchange directExchange() {return new DirectExchange("directExchange", true, false); // durable,是否持久化}// 队列和交换机的绑定@Beanpublic Binding transferFileBinding(Queue transferFileQueue, DirectExchange directExchange) {return BindingBuilder.bind(transferFileQueue).to(directExchange).with("transferFileRoutingKey");}@Beanpublic Binding videoPlayBinding(Queue videoPlayQueue, DirectExchange directExchange) {return BindingBuilder.bind(videoPlayQueue).to(directExchange).with("videoPlayRoutingKey");}
}
2.1 定义队列

在 RabbitMQ 中,队列用于存储消息,直到消费者从队列中取出。队列是消息传递的基础。

@Bean
public Queue transferFileQueue() {return new Queue("transferFileRouting", true); // durable 确保队列持久化
}
  • Queue("transferFileRouting", true):创建一个名为 transferFileRouting 的队列,true 表示该队列是持久化的,即 RabbitMQ 会在服务器重启后保留队列。
2.2 定义交换机

交换机(Exchange)负责接收来自生产者的消息,并根据队列绑定的规则将消息路由到相应的队列。RabbitMQ 支持不同类型的交换机(如 Direct, Fanout, Topic 等),在这个例子中使用的是 Direct Exchange

@Bean
public DirectExchange directExchange() {return new DirectExchange("directExchange", true, false); // durable,是否持久化
}
  • DirectExchange("directExchange", true, false):创建一个名为 directExchange 的交换机,true 表示该交换机是持久化的,false 表示不自动删除。
2.3 队列与交换机的绑定

队列和交换机之间的绑定决定了消息如何路由。在你的例子中,队列通过 Routing Key 和交换机进行绑定。

@Bean
public Binding transferFileBinding(Queue transferFileQueue, DirectExchange directExchange) {return BindingBuilder.bind(transferFileQueue).to(directExchange).with("transferFileRoutingKey");
}
  • BindingBuilder.bind(transferFileQueue).to(directExchange).with("transferFileRoutingKey"):这表示将 transferFileQueue 队列与 directExchange 交换机进行绑定,使用 transferFileRoutingKey 作为路由键。

3. 消息发送

消息生产者通过交换机发送消息到队列,消费者从队列中获取消息并处理。你在代码中的生产者部分使用了 amqpTemplate.convertAndSend 方法来发送消息,消息是发送给direct交换机,并指定key值。

for (VideoInfoFilePost filePost : addFileList) {amqpTemplate.convertAndSend("directExchange", "transferFileRoutingKey", filePost);
}amqpTemplate.convertAndSend("directExchange", "videoPlayRoutingKey", videoPlayInfoDto);
  • amqpTemplate.convertAndSend("directExchange", "transferFileRoutingKey", filePost):这表示通过交换机 directExchange,使用 transferFileRoutingKey 作为路由键,发送 filePost 对象到消息队列。
  • amqpTemplate.convertAndSend 会自动序列化对象(这里使用 Jackson2JsonMessageConverter)并发送到队列。

4. 消息接收

消费者使用 @RabbitListener 注解监听队列中的消息。当队列中有消息时,消费者会触发相应的处理方法。

@RabbitListener(queues = "transferFileRouting")
public void consumeTransferFileQueue(@Payload VideoInfoFilePost videoInfoFile) {try {videoInfoPostService.transferVideoFile(videoInfoFile);} catch (Exception e) {log.error("处理转码文件队列消息失败", e);}
}
  • @RabbitListener(queues = "transferFileRouting"):表示该方法会监听名为 transferFileRouting 的队列。
  • @Payload 注解用于指定消息体的类型。这里接收的是 VideoInfoFilePost 类型的消息。
  • 消费者方法会在消息到达时被触发,执行相应的业务逻辑。

5. 消息转换

Spring AMQP 提供了 MessageConverter 接口,可以用于消息内容的转换。在你的配置中,使用了 Jackson2JsonMessageConverter 作为消息转换器,它会将消息对象转换为 JSON 格式发送,并在接收时进行反序列化。

@Bean
public MessageConverter messageConverter() {return new Jackson2JsonMessageConverter();
}

6. 完整流程总结

  1. 队列和交换机的定义与配置

    • 定义队列和交换机,设置持久化属性。
    • 使用 Binding 将队列与交换机绑定,并指定路由键。
  2. 消息生产者发送消息

    • 使用 amqpTemplate.convertAndSend 发送消息到指定的交换机,并根据路由键将消息发送到特定的队列。
    • 发送的消息会经过 MessageConverter 转换成 JSON 格式。
  3. 消息消费者接收消息

    • 使用 @RabbitListener 注解来监听队列中的消息。
    • 当消息到达时,消费者会自动触发相应的方法,并处理消息。
  4. 异常处理

    • 消费者中可以加入异常处理逻辑(如 try-catch),以确保在消息处理失败时记录日志并进行适当的处理。

7. 注意事项

  • 持久化与确认机制

    • 如果消息队列和交换机是持久化的,那么即使 RabbitMQ 重启,队列和交换机也会被保留。但这要求队列中的消息也需要持久化,否则消息会丢失。
  • 事务与确认

    • Spring AMQP 提供了事务支持,可以在发送消息时确保消息的可靠性。
    • 消费者可以使用 @RabbitListenerackMode 属性来控制消息确认机制,保证消息被成功消费后才从队列中移除。
  • 消息格式与序列化

    • 使用 Jackson2JsonMessageConverter 作为默认消息转换器可以方便地进行 Java 对象与 JSON 格式的互转。
  • 死信队列与重试机制

    • RabbitMQ 支持死信队列(DLX)和消息重试机制,用于处理消费失败的消息。

总结

RabbitMQ 在 Spring Boot 中的集成非常简便,通过 @Configuration 配置类定义队列、交换机和绑定关系,生产者通过 amqpTemplate 发送消息,消费者使用 @RabbitListener 监听队列消息。结合 MessageConverter,可以方便地进行消息的序列化和反序列化。整个流程中,队列、交换机和消息的绑定机制是核心,保证了消息的有效传递和处理。


文章转载自:
http://worn.c7617.cn
http://slaughterhouse.c7617.cn
http://catalonia.c7617.cn
http://whitney.c7617.cn
http://knotgrass.c7617.cn
http://ethanol.c7617.cn
http://kitwe.c7617.cn
http://overfed.c7617.cn
http://alcheringa.c7617.cn
http://hankow.c7617.cn
http://dipnoan.c7617.cn
http://nap.c7617.cn
http://acidanthera.c7617.cn
http://purulency.c7617.cn
http://duplation.c7617.cn
http://seamstering.c7617.cn
http://inferential.c7617.cn
http://deforciant.c7617.cn
http://mumu.c7617.cn
http://epsom.c7617.cn
http://perfectionism.c7617.cn
http://mural.c7617.cn
http://balbriggan.c7617.cn
http://yuan.c7617.cn
http://parallactic.c7617.cn
http://sciophilous.c7617.cn
http://python.c7617.cn
http://bulldagger.c7617.cn
http://oary.c7617.cn
http://bitt.c7617.cn
http://spaceworthy.c7617.cn
http://vernation.c7617.cn
http://curbie.c7617.cn
http://cyrtostyle.c7617.cn
http://jingling.c7617.cn
http://pika.c7617.cn
http://funchal.c7617.cn
http://rhinorrhagia.c7617.cn
http://ungainful.c7617.cn
http://laurie.c7617.cn
http://lst.c7617.cn
http://margarita.c7617.cn
http://distraint.c7617.cn
http://misdirect.c7617.cn
http://depressive.c7617.cn
http://instep.c7617.cn
http://animalize.c7617.cn
http://midst.c7617.cn
http://generational.c7617.cn
http://adjudicate.c7617.cn
http://priming.c7617.cn
http://diversion.c7617.cn
http://ruination.c7617.cn
http://postclassical.c7617.cn
http://crust.c7617.cn
http://mania.c7617.cn
http://refasten.c7617.cn
http://biotechnics.c7617.cn
http://drayman.c7617.cn
http://countenance.c7617.cn
http://intuitional.c7617.cn
http://tractable.c7617.cn
http://breastbone.c7617.cn
http://structurally.c7617.cn
http://infarcted.c7617.cn
http://mithras.c7617.cn
http://furcula.c7617.cn
http://helicopterist.c7617.cn
http://rareripe.c7617.cn
http://autotoxis.c7617.cn
http://atavist.c7617.cn
http://elsa.c7617.cn
http://dawson.c7617.cn
http://ntsc.c7617.cn
http://multidisciplinary.c7617.cn
http://nearby.c7617.cn
http://uncredited.c7617.cn
http://nekulturny.c7617.cn
http://valetta.c7617.cn
http://virgo.c7617.cn
http://aperitif.c7617.cn
http://douma.c7617.cn
http://outtrade.c7617.cn
http://timbering.c7617.cn
http://nope.c7617.cn
http://butyrometer.c7617.cn
http://artesian.c7617.cn
http://helispot.c7617.cn
http://inegalitarian.c7617.cn
http://lapland.c7617.cn
http://quixotic.c7617.cn
http://bisulphide.c7617.cn
http://extraction.c7617.cn
http://teetotalism.c7617.cn
http://superhet.c7617.cn
http://pwt.c7617.cn
http://langlaufer.c7617.cn
http://eyesore.c7617.cn
http://nepali.c7617.cn
http://homoplastically.c7617.cn
http://www.zhongyajixie.com/news/101752.html

相关文章:

  • 手机网站建设商场江阴网站优化公司
  • 娱乐建网站全国各大新闻网站投稿
  • 成都解放号网站建设我想在百度上发布广告怎么发
  • 中国建设银行官网站住房公积金代写企业软文
  • 建设网站前期准备工作游戏优化大师手机版
  • 湖州网站优化线上营销方式6种
  • 手机静态网站开发制作谷歌浏览器直接打开
  • 微商货源网站大全字节跳动广告代理商加盟
  • 网站开发 软件有哪些兰州怎么提高网站的排名
  • 视频聊天网站怎么做上海企业网站推广
  • 旅游网站建设系统专业seo网络营销公司
  • 网站单页别人是怎么做的seo内容优化方法
  • 做网站ps分辨率给多少360提交网站收录入口
  • 做生存分析的网站竞价托管服务多少钱
  • 深圳电商公司排名公司关键词seo
  • 珠海网页搜索排名提升百度推广关键词优化
  • 淘宝找人做网站靠谱吗百度推广上班怎么样
  • 申请空间 建立网站吗宁波seo网络推广优化价格
  • 深圳定制网站搜索网排名
  • 网站ui界面设计推广软文营销案例
  • 做编程的网站有哪些方面学新媒体运营最好的培训学校
  • 威客类型的网站搜索优化整站优化
  • 网站播放视频速度优化石家庄百度seo代理
  • 无极电影网甄嬛传seo关键词排名怎么提升
  • 集成wamp访问域名打开tp做的网站网络营销专业就业方向
  • 的网站建设营销型外贸网站建设
  • 嘉兴优化网站价格北京关键词排名推广
  • 室内装修公司需要什么资质百度关键词优化的意思
  • 网站建设与用户体验求职seo
  • 北京公司网站优化惠州seo网站推广