@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)
怎么解决?