class RmqProductor(): # 生产者实例 producer = None ''' 传入配置文件,链接namesrv conf:namesrv配置文件 group_id:消费组id orderly:是否是顺序消息 timeout:超时 compress_level:消息压缩水平 number max_message_size:消息大小限制 ''' def __init__(self,conf,group_id,orderly=False, timeout=None, compress_level=None, max_message_size=None): self.producer = Producer(group_id,orderly, timeout, compress_level) self.producer.set_namesrv_addr(conf) self.producer.start() # 发送普通消息 def send_message_sync(self,topic,body,keys='',tags='',property=None,delay_time_level=0): msg = RmqMessage.get_msg_instance(topic,body,keys,tags) ret = self.producer.send_sync(msg) print('send message status: ' + str(ret.status) + ' msgId: ' + ret.msg_id) return ret def send_orderly_with_sharding_key(self,topic,body,order_id,keys='',tags='',property=None,delay_time_level=0): msg = RmqMessage.get_msg_instance(topic, body, keys, tags, property, delay_time_level) ret = self.producer.send_orderly_with_sharding_key(msg, order_id) print('send message status: ' + str(ret.status) + ' msgId: ' + ret.msg_id) print('send sync order message done') return ret def shutdown(self): self.producer.shutdown()
def send_orderly_with_sharding_key(count): producer = Producer(gid, True) producer.set_name_server_address(name_srv) producer.start() for n in range(count): msg = create_message() ret = producer.send_orderly_with_sharding_key(msg, 'orderId') print ('send message status: ' + str(ret.status) + ' msgId: ' + ret.msg_id) print ('send sync order message done') producer.shutdown()