异步消息需要指定发送回调

113 阅读1分钟

异步消息需要指定发送回调,SendCallback,异步消息发送如:

rocketMQTemplate.asyncSend("message:sms", "我是短信消息", new SendCallback() { @Override public void onSuccess(SendResult sendResult) { System.out.println(sendResult); }

                @Override
                public void onException(Throwable e) {
                    e.printStackTrace();
                }
            }
    );

1 2 3 4 5 6 7 8 9 10 11 12 消费者端 通过 RocketMQListener 监听器来监听消息,@RocketMQMessageListener注解来指定消费者组以及topic和tags。

@Slf4j @Component @RocketMQMessageListener(topic = "message", selectorExpression="sms" //tags ,consumerGroup = "service-consumer" ,messageModel = MessageModel.CLUSTERING ) public class MessageConsumer implements RocketMQListener {

@Override
@Transactional
public void onMessage(MessageExt message) {
    String msg = new String(message.getBody(), CharsetUtil.UTF_8);
    log.info("消费者 {} ",msg);
}

}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 onMessage方法是自动ack消息,如果方法中出现异常,ack失败,消息将会重试消费。

事务消息 对事务的理解见上一篇《事务消息》

事务监听器 通过实现 RocketMQLocalTransactionListener 编写本地事务监听器

@Component ———————————————— 版权声明:本文为CSDN博主「墨家巨子@俏如来」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/u014494148/…