spring rocketmq struts2 生产者示例代码
时间: 2023-11-11 22:00:44 浏览: 123
以下是一个简单的 Spring Boot + RocketMQ 生产者示例代码:
```java
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class RocketMQProducer {
@Value("${rocketmq.producer.group}")
private String producerGroup;
@Value("${rocketmq.namesrvAddr}")
private String namesrvAddr;
public void sendMessage(String topic, String tags, String message) throws Exception {
DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
producer.setNamesrvAddr(namesrvAddr);
producer.start();
Message msg = new Message(topic, tags, message.getBytes());
producer.send(msg);
producer.shutdown();
}
}
```
其中,`rocketmq.producer.group` 和 `rocketmq.namesrvAddr` 是在 `application.properties` 中配置的。
阅读全文