public class RocketProducer {
private DefaultMQProducer producer;
public RocketProducer() {
this.init();
}
private void init() {
if (producer == null) {
try {
producer = new DefaultMQProducer("DataRecord");
producer.setNamesrvAddr("192.168.1.11:9876");
producer.start();
} catch (Exception ex) {
LogUtil.error(ex);
this.destory();
producer = null;
}
}
}
public boolean send(String group, String queue, byte[] message) {
try {
this.init();
Message msg = new Message(group, queue, message);
SendResult result = producer.send(msg);
return (SendStatus.SEND_OK.equals(result.getSendStatus()));
} catch (Exception ex) {
LogUtil.error(ex);
}
return false;
}
public void destory() {
if (this.producer != null) {
this.producer.shutdown();
}
}
}
文档地址: https://rocketmq.incubator.apache.org/docs/best-practice-producer/