48 lines
1.7 KiB
Java
48 lines
1.7 KiB
Java
package com.ruoyi.testrocketmq.producer;
|
|
|
|
import com.ruoyi.testrocketmq.config.ProducerConfig;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
public class AsyncProducer {
|
|
|
|
@Autowired
|
|
private ProducerConfig producerConfig;
|
|
|
|
/**
|
|
* 发送异步的消息
|
|
* @param topic 主题
|
|
* @param tag 标签
|
|
* @param key 自定义的key,根据业务来定
|
|
* @param value 消息的内容
|
|
* @return org.apache.rocketmq.client.producer.SendResult
|
|
*/
|
|
// public SendResult sendAsyncProducerMessage(String topic, String tag, String key, String value) throws UnsupportedEncodingException {
|
|
//
|
|
// try {
|
|
// DefaultMQProducer defaultMQProducer = producerConfig.producer;
|
|
// //Create a message instance, specifying topic, tag and message body.
|
|
// Message msg = new Message(topic, tag, key,value.getBytes(RemotingHelper.DEFAULT_CHARSET));
|
|
// defaultMQProducer.send(msg, new SendCallback() {
|
|
// // 异步回调的处理
|
|
// @Override
|
|
// public void onSuccess(SendResult sendResult) {
|
|
// System.out.printf("%-10d 异步发送消息成功 %s %n", msg, sendResult.getMsgId());
|
|
// }
|
|
//
|
|
// @Override
|
|
// public void onException(Throwable e) {
|
|
// System.out.printf("%-10d 异步发送消息失败 %s %n", msg, e);
|
|
// e.printStackTrace();
|
|
// }
|
|
// });
|
|
// } catch (MQClientException e) {
|
|
// e.printStackTrace();
|
|
// } catch (RemotingException e) {
|
|
// e.printStackTrace();
|
|
// } catch (InterruptedException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
// return null;
|
|
// }
|
|
}
|