springboot整合RocketMQ自身踩坑记录
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RocketMQProducerController {
@Autowired
private DefaultMQProducer producer;
@RequestMapping("/sendMessage")
public String sendMessage() throws Exception {
Message message = new Message("TopicTest", "TagA", "OrderID001", "Hello world".getBytes());
SendResult sendResult = producer.send(message);
return "Send status: " + sendResult.getSendStatus() + ", msgId: " + sendResult.getMsgId();
}
}
这段代码展示了如何在Spring Boot应用中使用自动装配的DefaultMQProducer来发送一条消息到RocketMQ。在发送消息的方法上使用了@RequestMapping
注解,使得该方法可以通过HTTP请求被调用。
评论已关闭