Package: pyrocketmq
1. Introduce1.1 Why another python RocketMQ API ?Currently the existing apis are too outdated. For example:The official python API, which you can install viapip installrocketmq-client-python, is for RocketMQ 2.0 and below, and does not support PullConsumer.An unofficial python API written by messense, which you can install viapip install rocketmq, wraps the same cpp dynamic libraries as the official API, does not support RocketMQ 4.0+ either.The official cpp APInow has v5.0-rc1 version, but does not provide pre-compiled binaries, which means you have to compile the project your self. If you’re not familiar bazel tool and cpp class export, it may be a little difficult.1.2 What’s the features of this API ?It’s developed and tested on RocketMQ 4.0+, so it supports many recent features. For example, custom queue selector when producing messages, custom message selector by tag/sql when consuming messages, etc.It exposes the java API as it is as possible, viaJPype. You can directly reference the java doc in most cases.The get method of any java class are wrapped as property of the coresponding python class. For example, callingmsg.getTopic()in java is equivalent to callingmsg.topicin python.2. InstallationDownload RocketMQ binary release, for example:https://archive.apache.org/dist/rocketmq/4.3.2/rocketmq-all-4.3.2-bin-release.zip.Extract it to somewhere as you like, for example: /path/to/rocketmq-all-4.3.2-bin-releaseInstall pyrocketmq via pip:pip install pyrocketmqMake sure the jvm is startedBEFOREyou import and use pyrocketmq, for example:importjpypeimportjpype.importsjpype.startJVM(classpath=['/path/to/rocketmq-all-4.3.2-bin-release/lib/*',])frompyrocketmqimport*# do somethingjpype.shutdownJVM()3. QuickStart3.1 Producerimportjsonfrompyrocketmq.common.messageimportMessagefrompyrocketmq.client.producerimportProducer,SendStatuspr=Producer('test_producer')pr.setNamesrvAddr('localhost:9876')pr.start()body=json.dumps({'name':'Alice','age':1}).encode('utf-8')msg=Message(topic='test_topic',body=body,tags='girl')# send, tcp-like, return sendStatussr=pr.send(msg)assert(sr.sendStatus==SendStatus.SEND_OK)pr.shutdown()3.2 PullConsumerimportjsonfrompyrocketmq.client.consumer.consumerimportPullConsumer,PullStatuscs=PullConsumer('test_pull_consumer')cs.setNamesrvAddr('localhost:9876')topic='test_topic'cs.start()# pull messages from each queuemqs=cs.fetchSubscribeMessageQueues(topic)formqinmqs:ofs=cs.minOffset(mq)pr=cs.pull(mq,subExpression='girl',offset=ofs,maxNums=1)ifpr.pullStatus==PullStatus.FOUND:# iterate msg in pull resultformsginpr:print(json.loads(msg.body))cs.shutdown()3.3 PushConsumerimportjsonimporttimefromtypingimportListfrompyrocketmq.client.consumer.listenerimportConsumeConcurrentlyContext,ConsumeConcurrentlyStatus,MessageListenerConcurrentlyfrompyrocketmq.client.consumer.consumerimportMessageSelector,PushConsumerfrompyrocketmq.common.commonimportConsumeFromWherefrompyrocketmq.common.messageimportMessageExt# subclass MessageListenerConcurrently to write your own consume actionclassMyMessageListenerConcurrently(MessageListenerConcurrently):def_consumeMessage(self,msgs:List[MessageExt],context:ConsumeConcurrentlyContext)->ConsumeConcurrentlyStatus:print('Concurrently',context.ackIndex)formsginmsgs:print(json.loads(msg.body))returnConsumeConcurrentlyStatus.CONSUME_SUCCESScs=PushConsumer('test_push_consumer')cs.setNamesrvAddr('localhost:9876')selector=MessageSelector.byTag('girl')ml=MyMessageListenerConcurrently()cs.registerMessageListener(ml)cs.subscribe('test_topic',selector)cs.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET)cs.start()time.sleep(5)cs.shutdown()