{"record":{"id":"bd54990070477c8b","repo":"apache/rocketmq","slug":"the-producer-service-state-not-ok-servicestate","errorCode":null,"errorMessage":"The producer service state not OK, {serviceState}","messagePattern":"The producer service state not OK, (.+?)","errorType":"exception","errorClass":"MQClientException","httpStatus":null,"severity":"error","filePath":"client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java","lineNumber":485,"sourceCode":"    public boolean isUnitMode() {\n        return this.defaultMQProducer.isUnitMode();\n    }\n\n    public void createTopic(String key, String newTopic, int queueNum) throws MQClientException {\n        createTopic(key, newTopic, queueNum, 0);\n    }\n\n    public void createTopic(String key, String newTopic, int queueNum, int topicSysFlag) throws MQClientException {\n        this.makeSureStateOK();\n        Validators.checkTopic(newTopic);\n        Validators.isSystemTopic(newTopic);\n\n        this.mQClientFactory.getMQAdminImpl().createTopic(key, newTopic, queueNum, topicSysFlag, null);\n    }\n\n    private void makeSureStateOK() throws MQClientException {\n        if (this.serviceState != ServiceState.RUNNING) {\n            throw new MQClientException(\"The producer service state not OK, \"\n                + this.serviceState\n                + FAQUrl.suggestTodo(FAQUrl.CLIENT_SERVICE_NOT_OK),\n                null);\n        }\n    }\n\n    public List<MessageQueue> fetchPublishMessageQueues(String topic) throws MQClientException {\n        this.makeSureStateOK();\n        return this.mQClientFactory.getMQAdminImpl().fetchPublishMessageQueues(topic);\n    }\n\n    public long searchOffset(MessageQueue mq, long timestamp) throws MQClientException {\n        this.makeSureStateOK();\n        return this.mQClientFactory.getMQAdminImpl().searchOffset(mq, timestamp);\n    }\n\n    public long maxOffset(MessageQueue mq) throws MQClientException {\n        this.makeSureStateOK();","sourceCodeStart":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/apache/rocketmq/blob/293f5885719fc4aa3619446a1900f58ccfcfdd29/client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java#L467-L503","documentation":"MQClientException thrown by DefaultMQProducerImpl.makeSureStateOK() from the head of nearly every public producer API (send, sendOneway, createTopic, fetchPublishMessageQueues, searchOffset, invokeMessageQueueSelector, ...). It fires whenever serviceState != RUNNING - producer not started, start failed, or already shut down. It is the generic 'you forgot start() (or already shut down)' guard for all send-side operations.","triggerScenarios":"triggerScenarios","commonSituations":"commonSituations","solutions":["Call producer.start() once before any send and verify it succeeded (no exception)","Gate background senders on application lifecycle (stop worker threads before producer.shutdown(); e.g. @PreDestroy ordering, SmartLifecycle) with a volatile 'closed' flag checked before send","If state is START_FAILED, fix the underlying start error (see its exception) and create a fresh producer instance","Wrap send paths with an isRunning()/state check or use a small guard method to fail with a clearer message than the generic one"],"exampleFix":"// before\nDefaultMQProducer p = new DefaultMQProducer(\"g\");\np.send(msg); // throws: not started\n// after\nDefaultMQProducer p = new DefaultMQProducer(\"g\");\np.start();\nif (!closed) { p.send(msg); }","handlingStrategy":"try-catch","validationCode":" private volatile boolean closed = false;\n@PreDestroy void close() { closed = true; producer.shutdown(); }\npublic void sendSafe(Message m) throws MQClientException {\n    if (closed) throw new IllegalStateException(\"producer shut down\");\n    producer.send(m);\n}","typeGuard":null,"tryCatchPattern":"try {\n    producer.send(msg);\n} catch (MQClientException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"service state not OK\")) {\n        // producer not started or already shut down - lifecycle bug, not a broker issue\n        throw new IllegalStateException(\"Producer lifecycle misuse: \" + e.getMessage(), e);\n    }\n    throw e;\n}","preventionTips":["Call start() in @PostConstruct and shutdown() in @PreDestroy of the same singleton bean","Stop worker threads before producer.shutdown() (SmartLifecycle phase ordering)","Add a running-state guard in send wrappers for a clearer failure than the generic message","Never send from threads that can outlive the producer without an explicit closed flag"],"tags":["rocketmq","producer","lifecycle","send"],"backgroundTag":null,"analyzedSha":"293f5885719fc4aa3619446a1900f58ccfcfdd29","analyzedAt":"2026-08-14T11:50:13.822Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}