rocketmq 发送异步信息 org.apache.rocketmq.client.exception.MQClientException: The producer service state

测试rocketmq异步发送消息时,报

org.apache.rocketmq.client.exception.MQClientException: The producer service state not OK, SHUTDOWN_ALREADY
See http://rocketmq.apache.org/docs/faq/ for further details.
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.makeSureStateOK(DefaultMQProducerImpl.java:412)
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:521)
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.access$300(DefaultMQProducerImpl.java:89)
	at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl$3.run(DefaultMQProducerImpl.java:489)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

代码:

public class AsyncProducer {
    public static void main(String[] args) throws Exception {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("group_async_test_1");
        // 设置NameServer的地址
        producer.setNamesrvAddr("192.168.43.15:9876;192.168.43.16:9876");
        // 启动Producer实例
        producer.start();
       // producer.setRetryTimesWhenSendAsyncFailed(0);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            // 创建消息,并指定Topic,Tag和消息体
            Message msg = new Message("topic_async_test_a",
                    "TagA",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            // SendCallback接收异步返回结果的回调
            producer.send(msg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.printf("%-10d OK %s %n", index,
                            sendResult.getMsgId());
                }
                @Override
                public void onException(Throwable e) {
                    System.out.printf("%-10d Exception %s %n", index, e);
                    e.printStackTrace();
                }
            });
        }
        // 如果不再发送消息,关闭Producer实例。
        producer.shutdown();
    }
}

原因

代码中当消息发送完就立马 调用producer.shutdown();了,应该在前面加上

Thread.sleep(10000);
引用提供了一种在RocketMQ中手动添加topic的方法,可以通过执行命令`sh mqadmin updateTopic -n XXX:9876 -b XXX:10911 -t ESOrderListTopic`来实现。然后,可以启动broker来测试发送和接收消息,命令为`nohup sh bin/mqbroker -n localhost:9876 & tail -f bin/nohup.out`。在发送和接收消息之前,需要告诉客户端name servers的地址。可以使用环境变量NAMESRV_ADDR来设置name servers的地址,命令为`export NAMESRV_ADDR=localhost:9876`。接下来,可以使用命令`sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer`来发送消息,使用命令`sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer`来接收消息。如果遇到了`org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to <172.17.128.11:30909> failed`这个问题,可能是连接到指定的主机和端口失败导致的。您可以检查主机和端口是否正确,并确保RocketMQ服务正在运行。另外,也可以尝试使用`sh bin/mqshutdown broker`命令来关闭broker,然后再重新启动。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [rocketMQ 踩坑解决No route info of this topic和rocketmq:connect to 172.17.42.1:10911 failed](https://blog.csdn.net/weixin_44607885/article/details/103137280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [linux安装RocketMQ](https://blog.csdn.net/huangbaokang/article/details/102832746)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值