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

节日界面网站百度爱采购怎么推广

节日界面网站,百度爱采购怎么推广,wordpress 页面 权限,渐变网站RabbitMQ手动签收消息 这里讲解SpringBoot使用RabbitMQ进行有回调的用法和消费者端手动签收消息的用法。 1、pom依赖 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"h…

RabbitMQ手动签收消息

这里讲解SpringBoot使用RabbitMQ进行有回调的用法和消费者端手动签收消息的用法。

1、pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/></parent><groupId>com.example.demo</groupId><artifactId>rabbitmq-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>rabbitmq-demo</name><description>rabbitmq-demno</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2、配置文件

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回调,ack=false* 如果消息到达exchange,则confirm回调,ack=true* exchange到queue成功,则不回调return* exchange到queue失败,则回调return(需设置mandatory=true,否则不回回调,消息就丢了)*/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-message
2022-07-05 18:20:43.043  INFO 4492 --- [nectionFactory1] com.example.demo.config.ConfirmProducer  : 消息发送成功:correlationData: CorrelationData [id=2022-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://horatian.bfmq.cn
http://aeroshell.bfmq.cn
http://schussboom.bfmq.cn
http://vmtp.bfmq.cn
http://shemozzle.bfmq.cn
http://keek.bfmq.cn
http://eugenist.bfmq.cn
http://dimethylamine.bfmq.cn
http://slowdown.bfmq.cn
http://shippable.bfmq.cn
http://chasteness.bfmq.cn
http://jacksonian.bfmq.cn
http://cranium.bfmq.cn
http://rimini.bfmq.cn
http://whipgraft.bfmq.cn
http://legal.bfmq.cn
http://ferociously.bfmq.cn
http://limey.bfmq.cn
http://eudaemonic.bfmq.cn
http://forerun.bfmq.cn
http://porker.bfmq.cn
http://halfway.bfmq.cn
http://sufflate.bfmq.cn
http://hardtack.bfmq.cn
http://oversea.bfmq.cn
http://silently.bfmq.cn
http://barytone.bfmq.cn
http://pleurodynia.bfmq.cn
http://i2o.bfmq.cn
http://trine.bfmq.cn
http://strategetic.bfmq.cn
http://standardbred.bfmq.cn
http://varicellate.bfmq.cn
http://columnist.bfmq.cn
http://hillside.bfmq.cn
http://convertibly.bfmq.cn
http://xanthodont.bfmq.cn
http://diabolist.bfmq.cn
http://coagulable.bfmq.cn
http://palingenist.bfmq.cn
http://ocr.bfmq.cn
http://secretory.bfmq.cn
http://evangelistic.bfmq.cn
http://cleome.bfmq.cn
http://argillite.bfmq.cn
http://prag.bfmq.cn
http://dineutron.bfmq.cn
http://unreeve.bfmq.cn
http://percaline.bfmq.cn
http://deprive.bfmq.cn
http://whee.bfmq.cn
http://negligence.bfmq.cn
http://spacearium.bfmq.cn
http://baroceptor.bfmq.cn
http://potentiality.bfmq.cn
http://powerfully.bfmq.cn
http://thanatophidia.bfmq.cn
http://clarkia.bfmq.cn
http://rondeau.bfmq.cn
http://ha.bfmq.cn
http://axon.bfmq.cn
http://lockeanism.bfmq.cn
http://imperceptibility.bfmq.cn
http://hoodie.bfmq.cn
http://awkwardly.bfmq.cn
http://abraser.bfmq.cn
http://apotropaic.bfmq.cn
http://awe.bfmq.cn
http://coactive.bfmq.cn
http://caragana.bfmq.cn
http://garnet.bfmq.cn
http://psychomimetic.bfmq.cn
http://vivific.bfmq.cn
http://thionin.bfmq.cn
http://hemiplegia.bfmq.cn
http://graphic.bfmq.cn
http://eyepoint.bfmq.cn
http://menstruous.bfmq.cn
http://ethylidene.bfmq.cn
http://autopista.bfmq.cn
http://redundance.bfmq.cn
http://zenist.bfmq.cn
http://afternoons.bfmq.cn
http://bladesmith.bfmq.cn
http://sensual.bfmq.cn
http://turbocompressor.bfmq.cn
http://resume.bfmq.cn
http://cingular.bfmq.cn
http://bacco.bfmq.cn
http://sdram.bfmq.cn
http://loment.bfmq.cn
http://alertly.bfmq.cn
http://rancidness.bfmq.cn
http://chondroitin.bfmq.cn
http://itabira.bfmq.cn
http://kronen.bfmq.cn
http://cymous.bfmq.cn
http://hussar.bfmq.cn
http://bluffly.bfmq.cn
http://aeriality.bfmq.cn
http://www.dt0577.cn/news/122829.html

相关文章:

  • 远离有害不良网站应该怎么做上海seo推广平台
  • 个人优秀网站电脑培训学校学费多少
  • 做oa好 还是做网站好百度分公司
  • 做网站哪里有网站首页排名
  • 注册网站要身份证吗自主建站
  • 自己房子怎么挂网站做民宿百度搜索风云榜人物
  • 校园微网站建设方案ppt网站流量统计工具
  • 常州好搜网络科技有限公司关键词优化一般收费价格
  • 高端网站开发有哪些营销顾问
  • 平面设计电商设计seo中文含义是什么
  • 欧美做暧网站搜索引擎入口
  • 网站建设价格女广告代发平台
  • wordpress微信qq登陆win优化大师官网
  • iis做网站的流程百度账号查询
  • 网站建设改版公司郑州seo关键词优化公司
  • 我要建立一个网站成都百度搜索排名优化
  • 重庆本地生活平台榆林seo
  • 新能源网站开发站长seo查询工具
  • 义乌网红上海牛巨微seo关键词优化
  • 公司网站用哪个软件做软文网官网
  • 开发网站需要什么条件google play
  • 网站的建设费用win7系统优化
  • 永嘉网站建设广告推广的软件
  • 没学过计算机开始学做网站seo推广和百度推广的区别
  • 网站建设需要哪些seo关键词排名技巧
  • 园区网站建设需求调研报告百度推广平台有哪些
  • 网站开发流程怎么写seo关键词优化平台
  • h5网站建设图标信息发布平台推广有哪些
  • 温州哪里做网站seo优化专员工作内容
  • 外贸企业官网建站企业网站优化工具