RocketMq 顺序消费、分区消息、延迟发送消息、Topic、tag分类 实战 (延迟发送消息) (四)

延迟发送消息生产者配置

如下所示:@Bean注解向Spring容器注入一个名字叫delayOrderProducerBean、类型为OrderProducerBean 的对象(下文需要用到)

bash 复制代码
@Configuration
public class DelayProducerClient {

    @Autowired
    private RocketMqDelayProperties rocketMqDelayProperties;

    @Lazy
    @Bean(name = "delayOrderProducerBean",initMethod = "start", destroyMethod = "shutdown")
    public OrderProducerBean buildDelayOrderProducer() {
        OrderProducerBean orderProducerBean = new OrderProducerBean();
        orderProducerBean.setProperties(rocketMqDelayProperties.getProducerProperties());
        return orderProducerBean;
    }
}
bash 复制代码
  	@Autowired
    private RocketMqDelayProperties rocketMqDelayProperties;
	
	@Qualifier("delayOrderProducerBean")
    @Autowired
    private OrderProducerBean delayOrderProducerBean;
	
    public void sendDelayMQMessage(DelayBaseBo delayBaseBo) {

        String jsonBody = JsonLUtils.toJSon(delayBaseBo.getDelayMqSub());
        log.info("shardingKey = {},tag = {},jsonBody = {}", delayBaseBo.getShardingKey(), delayBaseBo.getTag(), jsonBody);

        // 发送消息,只要不抛异常就是成功
        try {
            Message msg = new Message(
                    // Message所属的Topic
                    rocketMqDelayProperties.getTopic(),
                    // Message Tag 可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在MQ服务器过滤
                    delayBaseBo.getTag(),
                    // msg key  一般为业务主键
                    delayBaseBo.getShardingKey(),
                    // Message Body 可以是任何二进制形式的数据, MQ不做任何干预
                    // 需要Producer与Consumer协商好一致的序列化和反序列化方式
                    jsonBody.getBytes()
            );
            msg.setStartDeliverTime(LocalDateLUtils.getEpochMilli(delayBaseBo.getDelayTime()));
            SendResult sendResult = delayOrderProducerBean.send(msg, delayBaseBo.getShardingKey());
            assert sendResult != null;
            log.info("event消息发送成功,topic = {},key = {},messageId = {}", sendResult.getTopic(), delayBaseBo.getShardingKey(), sendResult.getMessageId());
        } catch (ONSClientException e) {
            //日志记录错误信息与入参信息
            log.error("event消息发送失败,shardingKey = {} ,jsonBody = {} , 异常信息:", delayBaseBo.getShardingKey(), jsonBody, e);
        }
    }

可以看到,延迟发送消息和实时发送消息的方法差不多,只不过多了一个msg.setStartDeliverTime()参数。

相关推荐
开发者联盟league1 天前
在windows上安装和运行rocketmq
windows·rocketmq
冷小鱼1 天前
消息队列(MQ)技术全景科普:从选型到AI+未来
人工智能·kafka·rabbitmq·rocketmq·mq·pulsar
筠·2 天前
Docker Compose 部署 RocketMQ
docker·rocketmq·java-rocketmq
Rcnhtin6 天前
RocketMQ
java·linux·rocketmq
qq_297574677 天前
RocketMQ 系列文章(高级篇第 1 篇):高可用集群部署与运维监控实战指南
运维·rocketmq·java-rocketmq
成为大佬先秃头8 天前
解决RocketMQ-Dashboard开启登录认证后不生效
rocketmq
卷毛的技术笔记8 天前
从“拆东墙补西墙”到“最终一致”:分布式事务在Spring Boot/Cloud中的破局之道
java·spring boot·分布式·后端·spring cloud·面试·rocketmq
qq_297574679 天前
RocketMQ 系列文章(进阶篇第 4 篇):死信队列与延迟消息实战指南
rocketmq
KAI丶9 天前
【RocketMQ】dashboard消息展示重复
rocketmq
Wmenghu9 天前
Ubuntu 安装 RocketMQ 5.x + Dashboard 完整教程
linux·ubuntu·rocketmq