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

ssr网站开发红河网站建设

ssr网站开发,红河网站建设,盐城网站建设与网页制作,深圳网站建设 案例文章目录 一、RabbitMq 下载安装二、开发步骤:1.MAVEN 配置2. RabbitMqConfig 配置3. RabbitMqUtil 工具类4. DailyDelaySendConsumer 消费者监听5. 测试延迟发送 一、RabbitMq 下载安装 官网:https://www.rabbitmq.com/docs 二、开发步骤:…

文章目录

  • 一、RabbitMq 下载安装
  • 二、开发步骤:
    • 1.MAVEN 配置
    • 2. RabbitMqConfig 配置
    • 3. RabbitMqUtil 工具类
    • 4. DailyDelaySendConsumer 消费者监听
    • 5. 测试延迟发送

一、RabbitMq 下载安装

官网:https://www.rabbitmq.com/docs

二、开发步骤:

1.MAVEN 配置

   		<!--RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId><version>2.7.7</version></dependency>

2. RabbitMqConfig 配置

package com.lq.common.config;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;@Configuration
public class RabbitMqConfig {/**延迟交换机名称*/public static final String  DELAY_EXCHANGE="DelayExchange";/**延迟队列名称*/public static final String  DELAY_QUEUE="DelayQueue";public static final String ROUTING_KEY="delay";@Beanpublic CustomExchange customExchange(){Map<String, Object> map = new HashMap<>();//设置交换机支持延迟消息推送map.put("x-delayed-type","direct");return new CustomExchange(DELAY_EXCHANGE,"x-delayed-message",true,false,map);}@Beanpublic Queue delayQueue(){return new Queue(DELAY_QUEUE,true);}@Beanpublic Binding DelayBinding(){return BindingBuilder.bind(delayQueue()).to(customExchange()).with(ROUTING_KEY).noargs();}}

3. RabbitMqUtil 工具类

package com.lq.common.util;import com.lq.common.config.RabbitMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.ReturnedMessage;
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.Service;import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Service
@Slf4j
public class RabbitMqUtil {@Autowiredprivate RabbitTemplate rabbitTemplate;private DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");@PostConstructpublic void init(){/*** 消息发送到交换机成功回调函数*/rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {if (ack){log.info("消息投递到交换机成功");}else {log.error("消息投递到交换机失败,原因->{}",cause);}}});/**交换机投递到队列失败回调函数**/rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {@Overridepublic void returnedMessage(ReturnedMessage returned) {log.error("投递到队列失败,错误原因->{}",returned);}});}/*** @Description 发送延迟消息* @param content 延迟内容* @param delayTime 延迟时间 ,单位ms;  例如 5000 代表 5 秒* @Author hqd* @Date 2024-10-21*/public Boolean sendDelayMessage(String content,Integer delayTime){log.info("消息发送时间->{}",LocalDateTime.now().format(formatterDateTime));rabbitTemplate.convertAndSend(RabbitMqConfig.DELAY_EXCHANGE, RabbitMqConfig.ROUTING_KEY, content, new MessagePostProcessor() {@Overridepublic Message postProcessMessage(Message message) throws AmqpException {log.info("延迟时间->{}",delayTime);//这个底层就是setHeader("x-delay",i);是一样的 设置延时时间message.getMessageProperties().setDelay(delayTime);//单位毫秒return message;}});return true;}}

4. DailyDelaySendConsumer 消费者监听

package com.lq.daily.mq.consumer;import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.lq.common.config.RabbitMqConfig;
import com.lq.daily.dto.DailyDelaySendDTO;
import com.lq.daily.service.ILqDailyService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;/*** @Description 日报延迟发送消费者* @Author hqd* @Date 2024-10-21 16:04*/
@Slf4j
@Component
public class DailyDelaySendConsumer {@Autowiredprivate ILqDailyService lqDailyService;private DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");@RabbitListener(queues = RabbitMqConfig.DELAY_QUEUE)public void dailyDelaySendListener(String content, Channel channel, Message message) throws IOException, InterruptedException{log.info("消息接收时间->{}", LocalDateTime.now().format(formatterDateTime));log.info("接收消息内容是->{}",content);log.info("{}",message.getMessageProperties().getDeliveryTag());channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//处理日报发送业务逻辑if (StrUtil.isNotBlank(content)&& content.startsWith("{")){DailyDelaySendDTO dto = JSONObject.parseObject(content, DailyDelaySendDTO.class);if (ObjectUtil.isNotEmpty(dto)){lqDailyService.updateDailyDelaySend(dto.getDailyCode(), LocalDateTime.parse(dto.getDelaySendTime(),DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));}}}
}

5. 测试延迟发送

   @PassToken@GetMapping("/testDelayMq")@ApiOperation("测试Mq 延迟消息发送")public void testDelayMq(){DailyDelaySendDTO dto = new DailyDelaySendDTO();dto.setDailyCode("DC2024101015135400001");dto.setDelaySendTime("2024-10-22 10:58");LocalDateTime sendTime = LocalDateTime.parse(dto.getDelaySendTime()+":00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));long between = ChronoUnit.MILLIS.between(LocalDateTime.now(), sendTime);rabbitMqUtil.sendDelayMessage(JSON.toJSONString(dto),new Long(between).intValue());}

在这里插入图片描述


文章转载自:
http://enrapt.xqwq.cn
http://cryptopine.xqwq.cn
http://congenital.xqwq.cn
http://gasthof.xqwq.cn
http://underbidder.xqwq.cn
http://afterlight.xqwq.cn
http://hepatotomy.xqwq.cn
http://abound.xqwq.cn
http://thunderstroke.xqwq.cn
http://hydrometry.xqwq.cn
http://consignable.xqwq.cn
http://crowfoot.xqwq.cn
http://untried.xqwq.cn
http://hasty.xqwq.cn
http://cinnamon.xqwq.cn
http://decimal.xqwq.cn
http://tabernacular.xqwq.cn
http://downer.xqwq.cn
http://suburbanise.xqwq.cn
http://daunt.xqwq.cn
http://babiroussa.xqwq.cn
http://appealable.xqwq.cn
http://investable.xqwq.cn
http://brimful.xqwq.cn
http://acclimation.xqwq.cn
http://conversazione.xqwq.cn
http://notoungulate.xqwq.cn
http://chromic.xqwq.cn
http://loopworm.xqwq.cn
http://micropaleontology.xqwq.cn
http://imperceptibility.xqwq.cn
http://underthrust.xqwq.cn
http://farness.xqwq.cn
http://trikerion.xqwq.cn
http://radiotoxic.xqwq.cn
http://afferently.xqwq.cn
http://macau.xqwq.cn
http://therophyte.xqwq.cn
http://grime.xqwq.cn
http://upwafted.xqwq.cn
http://zonerefine.xqwq.cn
http://antiarrhythmic.xqwq.cn
http://clinicopathologic.xqwq.cn
http://unitholder.xqwq.cn
http://barony.xqwq.cn
http://removability.xqwq.cn
http://multilevel.xqwq.cn
http://guck.xqwq.cn
http://virtuously.xqwq.cn
http://rhapsodise.xqwq.cn
http://biddable.xqwq.cn
http://mammillary.xqwq.cn
http://hempy.xqwq.cn
http://quebecois.xqwq.cn
http://jeremiad.xqwq.cn
http://kasher.xqwq.cn
http://strenuosity.xqwq.cn
http://bagwoman.xqwq.cn
http://dustless.xqwq.cn
http://hippology.xqwq.cn
http://manfully.xqwq.cn
http://bedrabble.xqwq.cn
http://zoo.xqwq.cn
http://conchiferous.xqwq.cn
http://decalog.xqwq.cn
http://fetor.xqwq.cn
http://gymnastic.xqwq.cn
http://stt.xqwq.cn
http://nonsoap.xqwq.cn
http://foreside.xqwq.cn
http://dopplerite.xqwq.cn
http://anaphylactoid.xqwq.cn
http://turnverein.xqwq.cn
http://curial.xqwq.cn
http://babiche.xqwq.cn
http://kumbaloi.xqwq.cn
http://audit.xqwq.cn
http://inspan.xqwq.cn
http://surmountable.xqwq.cn
http://dumbstruck.xqwq.cn
http://discountable.xqwq.cn
http://scca.xqwq.cn
http://medalist.xqwq.cn
http://abash.xqwq.cn
http://rudeness.xqwq.cn
http://previable.xqwq.cn
http://stu.xqwq.cn
http://barranco.xqwq.cn
http://glim.xqwq.cn
http://hemosiderosis.xqwq.cn
http://resuscitate.xqwq.cn
http://expanse.xqwq.cn
http://palpable.xqwq.cn
http://stapes.xqwq.cn
http://lensless.xqwq.cn
http://constitutive.xqwq.cn
http://leukovirus.xqwq.cn
http://kinescope.xqwq.cn
http://christology.xqwq.cn
http://phytane.xqwq.cn
http://www.hrbkazy.com/news/58492.html

相关文章:

  • 福田的网站建设公司免费的个人网站怎么做
  • 学科网站建设方案网络推广网上营销
  • 山东省两学一做网站杭州网站建设 seo
  • root.txt文件放到您网站的根目录下市场营销四大分析方法
  • 新手学网页设计的网站seo的全称是什么
  • 网站怎么做链接跳转域名注册 万网
  • 网购网站有哪些seo外链招聘
  • 教育培训机构怎么建设网站近几天的新闻摘抄
  • 淘宝代运营是什么意思百度关键词优化多少钱一年
  • 网站推荐你了解我意思吧如何做关键词优化
  • 公司网站建设行业怎么样优化的含义
  • iis网站没有属性谷歌推广教程
  • dedecms生成网站地图太原seo
  • 郑州做网站外包的公司有哪些网站域名查询工具
  • 个人做美食视频网站狼雨的seo教程
  • 网站域名注册信息查询北京seo排名优化网站
  • 我要在58上面做网站12月30日疫情最新消息
  • 东莞seo网站优化方式网络推广优化
  • 做网站还需要买服务器么家电企业网站推广方案
  • 四川省的建设厅注册中心网站济南优化网页
  • 上海网站制作公司联系方式网络营销的未来发展趋势
  • 网站开发的安全性原则常州网站建设优化
  • wordpress 小说多站网站排名推广推荐
  • 专业 网站设计公司价格深圳做网站的
  • 做asp动态网站制作流程网站快速上排名方法
  • 大连做网站价钱2022知名品牌营销案例100例
  • 西安做网站公司玖佰网络企业网站推广方案设计毕业设计
  • 科技公司起名大全免费windows优化软件
  • wordpress极简免费主题徐州seo代理计费
  • unity可以做网站吗百度竞价广告