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

手机版网站推荐赣州本地网

手机版网站推荐,赣州本地网,怎样注册网站账号申请,毕业答辩企业网站开发的问题RabbitMQ手动签收消息 这里讲解SpringBoot使用RabbitMQ进行有回调的用法和消费者端手动签收消息的用法。 1、pom依赖 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsih…RabbitMQ手动签收消息 这里讲解SpringBoot使用RabbitMQ进行有回调的用法和消费者端手动签收消息的用法。 1、pom依赖 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.4/versionrelativePath//parentgroupIdcom.example.demo/groupIdartifactIdrabbitmq-demo/artifactIdversion0.0.1-SNAPSHOT/versionnamerabbitmq-demo/namedescriptionrabbitmq-demno/descriptionpropertiesjava.version8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2、配置文件 server:port: 9090 spring:application:name: rabbit-confirmrabbitmq:template:# 使用return-callback时必须设置mandatory为truemandatory: true# 消息发送到交换机确认机制,是否确认回调publisher-confirm-type: correlated# 消息发送到交换机确认机制是否返回回调publisher-returns: truelistener:simple:# 并发消费者初始化值concurrency: 5# 最大值max-concurrency: 10# 每个消费者每次监听时可拉取处理的消息数量prefetch: 20# 确认模式设置为手动签收acknowledge-mode: manualusername: zsx242030password: zsx242030virtual-host: /3、定义配置类 package com.example.demo.config;import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class ConfirmConfiguration {/*** 声明confirm.message队列*/Beanpublic Queue confirmQueue() {return new Queue(confirm.message);}/*** 声明一个名为exchange-2的交换机*/Beanpublic TopicExchange exchange2() {return new TopicExchange(exchange-2);}/*** 将confirm.message的队列绑定到exchange-2交换机*/Beanpublic Binding bindMessage1() {return BindingBuilder.bind(confirmQueue()).to(exchange2()).with(confirm.message);} }4、定义生产者 package com.example.demo.config;import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageBuilder; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Component;import javax.annotation.Resource; import java.sql.Timestamp; import java.time.LocalDateTime;Component Slf4j public class ConfirmProducer {Resourceprivate RabbitTemplate rabbitTemplate;/*** 如果消息没有到exchange,则confirm回调,ackfalse* 如果消息到达exchange,则confirm回调,acktrue* exchange到queue成功,则不回调return* exchange到queue失败,则回调return(需设置mandatorytrue,否则不回回调,消息就丢了)*/private final RabbitTemplate.ConfirmCallback confirmCallback (correlationData, ack, cause) - {if (!ack) {log.error(消息发送失败correlationData: {},cause: {}, correlationData, cause);} else {log.info(消息发送成功correlationData: {},ack: {}, correlationData, ack);}};private final RabbitTemplate.ReturnCallback returnCallback (message, replyCode, replyText, exchange, routeKey) -log.error(消息丢失: exchange: {},routeKey: {},replyCode: {},replyText: {}, exchange, routeKey, replyCode, replyText);/*** 发送消息** param message 消息内容*/public void send(String message) {// 构建回调返回的数据CorrelationData correlationData new CorrelationData();Timestamp time Timestamp.valueOf(LocalDateTime.now());correlationData.setId(time );Message message1 MessageBuilder.withBody(message.toString().getBytes()).setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)// 将CorrelationData的id 与 Message的correlationId绑定然后关系保存起来,然后人工处理.setCorrelationId(correlationData.getId()).build();rabbitTemplate.setConfirmCallback(confirmCallback);rabbitTemplate.setReturnCallback(returnCallback);rabbitTemplate.convertAndSend(exchange-2, confirm.message, message1, correlationData);} }5、定义消费者 package com.example.demo.config;import com.rabbitmq.client.Channel; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component;import java.io.IOException;Component Slf4j public class ConfirmConsumer {RabbitListener(bindings QueueBinding(value Queue(value confirm.message,durable true),exchange Exchange(value exchange-2,type topic),key confirm.message))public void receive(String message, Message message1, Channel channel) throws IOException {log.info(消费者收到消息{}, message);long deliverTag message1.getMessageProperties().getDeliveryTag();//第一个deliveryTag参数为每条信息带有的tag值第二个multiple参数为布尔类型//为true时会将小于等于此次tag的所有消息都确认掉如果为false则只确认当前tag的信息可根据实际情况进行选择。channel.basicAck(deliverTag, false);} }6、创建controller调用 package com.example.demo.controller;import com.example.demo.config.ConfirmProducer; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;RestController public class ConfirmController {Resourceprivate ConfirmProducer confirmProducer;GetMapping(/confirm-message)public void confirmMessage() {confirmProducer.send(hello confirm message);} }7、启动类 package com.example.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication public class RabbitDemoApplication {public static void main(String[] args) {SpringApplication.run(RabbitDemoApplication.class, args);}}8、测试 http://localhost:9090/confirm-message2022-07-05 18:20:43.043 INFO 4492 --- [nectionFactory1] com.example.demo.config.ConfirmProducer : 消息发送成功correlationData: CorrelationData [id2022-07-05 18:20:43.025],ack: true 2022-07-05 18:20:43.046 INFO 4492 --- [ntContainer#0-5] com.example.demo.config.ConfirmConsumer : 消费者收到消息hello confirm message
http://www.tj-hxxt.cn/news/219006.html

相关文章:

  • 自己怎么样做游戏网站html改变字体大小代码
  • 做商业广告有什么网站好推销的专业网站优化seo
  • 南充建网站的资料百度编辑器wordpress
  • 网站开发怎么收客户费广西网站建设公司哪家好
  • 图片链接生成网站深圳营销型网站建设推广服务
  • 北京上云网站建设公司中国视觉设计网
  • 哪个网站做课件能赚钱京网站制作公司
  • 重庆网站设计制造厂家做设计在哪个网站上找高清图片大全
  • 宣武上海网站建设河北响应式网站建设
  • 吉林省住房和城乡建设部网站外汇直播网站建设开发
  • 网站建设最新惠州做网络推广的公司
  • 电商网站开发视频教程wordpress 仿简书
  • 网站建设header百度怎么做网站广告
  • 怎么在网站上添加地图公司网站想维护服务器
  • 建站哪个网站比较好做网站的费用会计分录
  • 网站平台开发报价单成都武侯区建设局门户网站
  • 做网站哪里最便宜谁有好的网站推荐一个
  • 浙江省住房和城乡建设局网站首页长春市城建网站
  • 宜昌做网站要什么条件帮忙建设公司网站
  • sgs网站开发公司类似in a wordpress
  • 网站总体规划设计说明银行存款营销活动方案
  • 有专门做礼品的网站吗美妆网站建设项目计划书
  • 手机app开发 网站建设创新的天津网站建设
  • 网站开发中的抓包工具网站是什么时候出现的
  • 建站公司建的网站能改动吗室内设计风格
  • 自己做的网站别人查看温州市建设局网站
  • 阳江市住房和城乡规划建设局网站创新的营销型网站
  • 非官方网站建设网站策划书案例展示
  • 帝国cms 网站地图 自定义租赁模板建站 网站的名称归属
  • 软工毕设做网站wordpress宝塔安装