报错内容
异步回调需要时间,设置发送消息的超时时间关闭producer之前等待几秒
async_Producer.setSendMsgTimeout(15000);
countDownLatch.await(8, TimeUnit.SECONDS);
/**
* @Author: wty
* @ Data: 2023/4/6 16:07
*/
public class AsyncProducer {
public static void main(String[] args) throws MQClientException, InterruptedException {
//1.创建生产者,指定group
DefaultMQProducer async_Producer = new DefaultMQProducer("my-producer-group");
//2.指定nameserver的地址
async_Producer.setNamesrvAddr("127.0.0.1:9876");
//设置信息发送超时时间
async_Producer.setSendMsgTimeout(15000);
//启动
async_Producer.start();
//当异步消息发送失败了要重试几次
async_Producer.setRetryTimesWhenSendAsyncFailed(0);
int messgeCount = 100;
final CountDownLatch countDownLatch = new CountDownLatch(messgeCount);
for (int i = 0; i < messgeCount; i++) {
try {
final int index = i;
Message message = new Message("MyTopic1", "TagA", ("你好" + i).getBytes());
async_Producer.send(message, new SendCallback() {
public void onSuccess(SendResult sendResult) {
countDownLatch.countDown();
System.out.printf("%-10d ok %s %n",index,sendResult.getMsgId());
}
public void onException(Throwable e) {
countDownLatch.countDown();
System.out.printf("%-10d exception %s %n", index, e);
e.printStackTrace();
}
});
} catch (RemotingException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("==============");
//等待8秒
countDownLatch.await(8, TimeUnit.SECONDS);
async_Producer.shutdown();
}
}