随州做网站,合肥网站建设方案优化,装修公司排名哪家好的,平面设计公司一般做什么前言 
今天我们使用Java和RabbitMQ实现消息队列的延迟功能。 
前期准备#xff0c;需要安装好docker、docker-compose的运行环境。 
需要安装RabbitMQ的可以看下面这篇文章。 
如何使用PHP和RabbitMQ实现消息队列#xff1f;-CSDN博客 
今天讲的是依赖RabbitMQ的延迟插件实现…前言 
今天我们使用Java和RabbitMQ实现消息队列的延迟功能。 
前期准备需要安装好docker、docker-compose的运行环境。 
需要安装RabbitMQ的可以看下面这篇文章。 
如何使用PHP和RabbitMQ实现消息队列-CSDN博客 
今天讲的是依赖RabbitMQ的延迟插件实现消息队列的延迟功能。 
如何安装RabbitMQ的延迟插件并且启用可以看下面的这篇文章。 
如何使用PHP和RabbitMQ实现延迟队列方式一_php调rabbit 设置延时-CSDN博客 
一、编写代码 
1、使用springboot框架快速搭建一个项目。 
2、在 pom.xml 中添加 Spring Boot AMQP 的依赖内容如下。 
dependency  groupIdorg.springframework.boot/groupId  artifactIdspring-boot-starter-amqp/artifactId  
/dependency 
3、在 application.yml 中配置 RabbitMQ 的连接信息内容如下。 
spring:rabbitmq:host: localhostport: 5672username: guestpassword: guest 
4、在配置类中定义交换机、队列和绑定内容如下。 
package com.ayzen.hello;import java.util.HashMap;
import java.util.Map;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class RabbitMqConfig {public static final String DELAYED_EXCHANGE  delayed_exchange;public static final String DELAYED_QUEUE  delayed_queue;public static final String ROUTING_KEY  delayed_key;BeanCustomExchange delayedExchange() {MapString, Object args  new HashMap();args.put(x-delayed-type, direct);return new CustomExchange(DELAYED_EXCHANGE, x-delayed-message, true, false, args);}BeanQueue delayedQueue() {return new Queue(DELAYED_QUEUE, true);}BeanBinding binding(Queue delayedQueue, CustomExchange delayedExchange) {return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(ROUTING_KEY).noargs();}
}5、创建一个生产者发送一个带有延迟属性的消息内容如下。 
package com.ayzen.hello;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/test)
public class TestController {final Logger logger  LoggerFactory.getLogger(getClass());AutowiredRabbitTemplate rabbitTemplate;GetMapping(/send)public ResponseEntityObject send() {this.sendDelayMessage(sendDelayMessage, 5000);return ResponseEntity.ok(ResponseDto.success(ok));}private void sendDelayMessage(String message, long ttlInMilliseconds) {MessageProperties messageProperties  new MessageProperties();messageProperties.setHeader(x-delay, ttlInMilliseconds);Message msg  MessageBuilder.withBody(message.getBytes()).andProperties(messageProperties).build();rabbitTemplate.convertAndSend(delayed_exchange, delayed_key, msg);logger.info(send message to rabbitmq.);}
}6、创建一个消息者监听接收队列中的消息内容如下。 
package com.ayzen.hello;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;Component
public class TestService {final Logger logger  LoggerFactory.getLogger(getClass());RabbitListener(queues  delayed_queue)public void process(String message) {logger.info(process message from rabbitmq,message{}, message);}
}7、至此测试项目代码已完成下一步将进行验证。 
二、测试验证 
1、启动服务。 
2、调用生产者执行如下代码。 
curl http://127.0.0.1:8080/test/send 
3、查看日志正常情况会返回如下内容。 如上图所示在2024-04-07T22:32:47.48908:00接收到生产者的请求然后在2024-04-07T22:32:52.58808:00执行消费动作延迟5秒。 
4、至此使用Java和RabbitMQ实现延迟队列的功能已验证完毕。 
总结 
用Java和RabbitMQ实现消息队列的延迟功能其实依靠的是RabbitMQ的一个延迟插件主要有以下几个步骤。 
1、安装RabbitMQ延迟插件。 
2、编写Java测试项目。 
3、进行测试验证。 
上面的代码只是做个简单的示例如果运用到实际的项目当中需要做进一步的优化。 
最后因本人能力有限有什么不对的地方望各位大佬指出好让我改进多多包含谢谢大家。 文章转载自: http://www.morning.qdxkn.cn.gov.cn.qdxkn.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.mgnrc.cn.gov.cn.mgnrc.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.ntffl.cn.gov.cn.ntffl.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.nydgg.cn.gov.cn.nydgg.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.yqgny.cn.gov.cn.yqgny.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.jybj.cn.gov.cn.jybj.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.qdscb.cn.gov.cn.qdscb.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.frzdt.cn.gov.cn.frzdt.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.wnnfh.cn.gov.cn.wnnfh.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn