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

企业进行网站建设的方式湖南网站seo地址

企业进行网站建设的方式,湖南网站seo地址,清河做网站多少钱,宁波seo营销推广rabbitMQ在发送消息时,会出现交换机不存在(交换机名字写错等消息),这种情况如何会退给生产者重新处理?【交换机层】 生产者发送消息时,消息未送达到指定的队列,如何消息回退? 核心&…

rabbitMQ在发送消息时,会出现交换机不存在(交换机名字写错等消息),这种情况如何会退给生产者重新处理?【交换机层】
生产者发送消息时,消息未送达到指定的队列,如何消息回退?

核心:对类RabbitTemplate.ConfirmCallback 和RabbitTemplate.ReturnCallback的重写。

RabbitTemplate.ConfirmCallback:交换机在收到消息或者没收到消息时会被触发
RabbitTemplate.ReturnCallback:消息进入交换机,不能达到指定目的地时被出发。

开启交换机确认
开启消息不可达回退

配置文件不开启 这两项

spring:rabbitmq:
#    交换机进行确认消息publisher-confirm-type: correlated
#   交换机不可以路由消息时 消息回退publisher-returns: true
配置类声明
package com.esint.configs;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 发布确认**/
@Configuration
public class ConfirmConfig {//交换机public static final String CONFIRM_EXCHANGE = "confirm.exchange";//队列public static final String CONFIRM_QUEUE = "confirm.queue";//routing-keypublic static final String CONFIRM_ROUTING_KEY = "key1";//声明 交换机@Bean("confirmExchange")public DirectExchange confirmExchange(){return new DirectExchange(CONFIRM_EXCHANGE);}//声明 队列@Bean("confrimQueue")public Queue confrimQueue(){return QueueBuilder.durable(CONFIRM_QUEUE).build();}//绑定@Beanpublic Binding queueBindingExchange(@Qualifier("confrimQueue") Queue confrimQueue,@Qualifier("confirmExchange") DirectExchange confirmExchange){return  BindingBuilder.bind(confrimQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);}
}

消费者:

package com.esint.controller;import com.esint.configs.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {@Autowiredprivate RabbitTemplate rabbitTemplate;//发消息@GetMapping("/sendMessage/{message}")public void sendMessage(@PathVariable String message){//普通发送模式 无是否发送成功回调CorrelationData correlationData = new CorrelationData("101");rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE,ConfirmConfig.CONFIRM_ROUTING_KEY+"123",message);log.info("发送消息为:{}",message);}}

消费者:

package com.esint.consumer;import com.esint.configs.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Slf4j
@Component
public class Consumer {@RabbitListener(queues = ConfirmConfig.CONFIRM_QUEUE)public void receiveConfrimMessage(Message message){log.info("接收到的消息为:" + new String(message.getBody()));}
}
核心修改的重写的类:
package com.esint.consumer;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Slf4j
@Component
public class MyCallBack implements RabbitTemplate.ConfirmCallback ,RabbitTemplate.ReturnCallback{/***  注入:本类为实现了RabbitTemplate的内部类,所以在RabbitTemplate发送消息的时候不会调用到我们自己的实现,所以需要把这个类在注入到RabbitTemplate中。*/@Autowiredprivate RabbitTemplate rabbitTemplate;@PostConstructpublic void init(){rabbitTemplate.setConfirmCallback(this);rabbitTemplate.setReturnCallback(this);}/*** RabbitTemplate.ConfirmCallback  是在【生产者】发送【交换机】 交换机的感知回应调去方法** 交换机确认回调方法* 1.交换机接收消息成功*   参数1  correlationData保存了回调消息ID和相关信息*   参数2  交换机收到消息 true*   参数3  失败原因 为 null* 2.交换机接受消息失败*   参数1  correlationData保存了回调消息ID和相关信息*   参数2  交换机收到消息 false*   参数3  失败原因* @param correlationData  来源于生产者 所以在发消息时 需要带有这个属性* @param ack* @param cause*/@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {String id = correlationData != null ? correlationData.getId() : "";if(ack){log.info("交换机确认收到 ID:{}" ,id);}else {log.info("交换机未收到ID:{}的消息,原因:{}",id,cause);//这里实现发送交换机失败的存储逻辑}}/*** 回退消息* 在消息传递过程不可达目标地时 返还给生产者  只有消息不可达,才会执行这个方法** @param message* @param replyCode* @param replyText* @param exchange* @param routingKey*/@Overridepublic void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {log.error("消息{} 被交换机{} 退回,原因:{} 路由:{}",new String(message.getBody()),exchange,replyText,routingKey);//这里实现发送消息不到达的逻辑 发送消息无法被逻辑 默认就会被交换机丢掉 这里重写后 可以在这里处理存储}
}

故意发送一个错误路由时:
在这里插入图片描述
消息能发出 交换机有确认 消息可以被回退

http://www.cotm.com.cn/news/521.html

相关文章:

  • 怎么在网站上做宣传网络优化大师手机版
  • 小鸟云WordPress数据库连接站长工具seo综合查询权重
  • 校园二手物品交易网站怎么做百度收录提交网站后多久收录
  • 客户在我这做的网站被罚靠谱的推广平台有哪些
  • 网站建设主要包括哪两个方面微博seo排名优化
  • 做店铺首页的网站渠道网络
  • 网站结构分析怎么写网上教育培训机构
  • 大连建设工程信息网怎么没有了重庆可靠的关键词优化研发
  • 做浏览单的网站工业设计公司
  • 百度网站是百度公司做的吗seo优化师
  • 访问一个网站的全过程企业网站怎么注册
  • 怎么做商城网站网络优化是做啥的
  • 安徽合肥做网站的公司有哪些营销推广运营
  • web网站开发书籍德阳seo优化
  • 网站设计遇到难题短视频精准获客
  • 网站建设技术实现难点唐山公司做网站
  • 用dw制作个介绍家乡网站沧州百度推广总代理
  • 网站开发合同手游推广代理平台有哪些
  • 关于网站平台建设调研的函北京网络优化
  • 页面设计要求伊春seo
  • 工商局网站如何做网登谷歌官方seo入门指南
  • 辽宁省建设厅网站广州抖音推广公司
  • 凡科轻站小程序收费吗福州百度关键词优化
  • 商业网站建设软件微信平台推广方法
  • 做网站推广的一般都是什么公司小程序制作费用一览表
  • web是什么意思动漫seo网络推广公司排名
  • 做seo要明白网站内网站seo公司
  • 有没有建网站的app网站推广的公司
  • asp.net网站开发上武汉seo搜索优化
  • 洛阳网站建设建站系统百度电话号码查询