RocketMQ 操作落地 (rocketmq-starter 方式)
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@GetMapping("/sendMessage")
public String sendMessage(@RequestParam String topic, @RequestParam String message) {
SendResult sendResult = rocketMQTemplate.convertAndSend(topic, message);
return "Message sent. MsgId: " + sendResult.getMsgId() + ", SendStatus: " + sendResult.getSendStatus();
}
}
这段代码展示了如何使用rocketmq-spring-boot-starter
发送一个消息到RocketMQ的特定主题。RocketMQTemplate
提供了convertAndSend
方法,它简化了消息的发送过程。当调用/sendMessage
接口时,会向RocketMQ发送一条消息,并返回消息的ID和发送状态。
评论已关闭