Skip to content

How to use rockemq in multi-threaded scenarios properly and handle exceptions such as Namer Server Cluster and Broker Cluster restart #99

@tom0392

Description

@tom0392

Description of problem

We send messages from the client to the MQ server at a certain interval by using multiple threads following the steps below:

Producer
from rocketmq.client import Producer, Message

producer = Producer('PID-XXX')
producer.set_name_server_address('127.0.0.1:9876')
producer.start()

msg = Message('YOUR-TOPIC')
msg.set_keys('XXX')
msg.set_tags('XXX')
msg.set_body('XXXX')
ret = producer.send_sync(msg)
print(ret.status, ret.msg_id, ret.offset)
producer.shutdown()

When a restart of the MQ server including Namer Server Cluster and Broker Cluster causes the service to stop for a short time, the MQ client will report an error at first when sending a MQ message:

Throw an exception in the program as:
File "/usr/lib/python2.7/site-packages/neutron_lbaas/drivers/bcslb/monitor/eslb_monitor.py", line 80, in push_message_to_rocketMQ_handler
ret = producer.send_sync(msg)
File "/usr/lib64/python2.7/site-packages/rocketmq/client.py", line 210, in send_sync
ffi_check(dll.SendMessageSync(self._handle, msg, ctypes.pointer(c_result)))
File "/usr/lib64/python2.7/site-packages/rocketmq/exceptions.py", line 44, in ffi_check
raise exc_cls(msg)
ProducerSendSyncFailed: No route info of this topic: dawn-bcmq-performance-topic,error:-1,in file rocketmq-client-cpp/src/producer/DefaultMQProducerImpl.cpp line:418

We think the reason for this error is that the Name server cannot be connected normally, which leads to no routing information. However, when the MQ server returns to normal after restart, the client will still keep reporting errors:

ProducerStartFailed: The producer group[] has been created before, specify another name please.,error:-1,in file rocketmq-client-cpp/src/producer/DefaultMQProducerImpl.cpp line:79

Problem analysis

When the MQ Server is stopped, the first thread has been in an abnormal state and cannot go to the following shutdown step normally:
producer.shutdown()
Then, the program starts the second thread to send messages, and calling producer.start() results in an error:

The producer group[ ] has been created before, specify another name please.

As long as the first thread that has the problem at the beginning cannot shutdown producer normally, all subsequent new threads will cause the same error as the second thread when run to producer.start(). Even when the MQ server restarts and returns to normal, the client will continue to report that same error.

suggestion

  1. First suggestion

When the producer sends a message successfully, the program will return SendStatus.OK=0 to the ret variable. So we think when the producer fails to send a message, the program should return an error code to the ret accordingly and not to throw an exception to prevent the thread from running the rest of code, especially the following step:
producer.shutdown()

  1. Second suggestion

Is there recommended usage steps of producer for rocketmq's multi-thread usage scenarios?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions