@Slf4j
public class SyncProducer {
public static void sendMessage(String topic, String message) {
try {
ClientServiceProvider provider = ClientServiceProvider.loadService();
ClientConfiguration configuration = ClientConfiguration.newBuilder()
.setEndpoints("ip:9181")
.build();
Producer producer = provider.newProducerBuilder()
.setClientConfiguration(configuration)
.setTopics(topic)
.build();
String tag = "messageTag";
Message msg = provider.newMessageBuilder()
.setTopic(topic)
.setTag(tag)
.setBody(message.getBytes(StandardCharsets.UTF_8))
.build();
SendReceipt send = producer.send(msg);
log.info("Send message successfully, messageId={}", send.getMessageId());
producer.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage());
}
}
public static void main(String[] args) {
SyncProducer.sendMessage("TestTopic", "TestMessage");
}
}
报错:
11:26:52.842 [main] ERROR com.xmzn.common.mq.producer.SyncProducer -- Expected the service ProducerImpl-0 [FAILED] to be RUNNING, but the service has FAILED
java.lang.IllegalStateException: Expected the service ProducerImpl-0 [FAILED] to be RUNNING, but the service has FAILED
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractService.checkCurrentState(AbstractService.java:381)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractService.awaitRunning(AbstractService.java:305)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractIdleService.awaitRunning(AbstractIdleService.java:165)
at org.apache.rocketmq.client.java.impl.producer.ProducerBuilderImpl.build(ProducerBuilderImpl.java:93)
at com.xmzn.common.mq.producer.SyncProducer.sendMessage(SyncProducer.java:29)
at com.xmzn.common.mq.producer.SyncProducer.main(SyncProducer.java:46)
Caused by: java.util.concurrent.CancellationException: Task was cancelled.
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractFuture.cancellationExceptionWithCause(AbstractFuture.java:1543)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:586)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:567)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:91)
at org.apache.rocketmq.client.java.impl.ClientImpl.startUp(ClientImpl.java:188)
at org.apache.rocketmq.client.java.impl.producer.ProducerImpl.startUp(ProducerImpl.java:114)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.AbstractIdleService$DelegateService$1.run(AbstractIdleService.java:62)
at org.apache.rocketmq.shaded.com.google.common.util.concurrent.Callables.lambda$threadRenaming$3(Callables.java:103)
at java.base/java.lang.Thread.run(Thread.java:1583)
Exception in thread "main" java.lang.RuntimeException: Expected the service ProducerImpl-0 [FAILED] to be RUNNING, but the service has FAILED
at com.xmzn.common.mq.producer.SyncProducer.sendMessage(SyncProducer.java:41)
at com.xmzn.common.mq.producer.SyncProducer.main(SyncProducer.java:46)
怎么解决?
如果你已经部署了RocketMQ 5.1.4并且无法发送消息,这里有一些可能的原因和解决方案:
以上是一些常见的原因和解决方法,但具体问题可能需要结合实际场景分析。希望这些信息对你有所帮助。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
涵盖 RocketMQ、Kafka、RabbitMQ、MQTT、轻量消息队列(原MNS) 的消息队列产品体系,全系产品 Serverless 化。RocketMQ 一站式学习:https://rocketmq.io/