Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
 def custom_init(self):
     group_id = f'g-{self._queue_name}'
     if group_id not in self.__class__.group_id__rocketmq_producer:  # 同一个进程中创建多个同组消费者会报错。
         producer = Producer(group_id)
         producer.set_namesrv_addr(frame_config.ROCKETMQ_NAMESRV_ADDR)
         producer.start()
         self.__class__.group_id__rocketmq_producer[group_id] = producer
     else:
         producer = self.__class__.group_id__rocketmq_producer[group_id]
     self._producer = producer
class RocketProducer():
    produce = None
    message = None

    # 初始化配置文件
    def __init__(self, conf):
        pid = uuid.uuid1().hex
        self.produce = Producer("pid")
        self.produce.set_namesrv_addr(conf)

    # 开始链接
    def start(self):
        return self.produce.start()

    # 发送同步消息
    def sendSync(self, topic, msg, group='', keys='', tags=''):
        if not topic or not msg:
            print('消息topic或msg不能为空')
            return
        msg = bytes(json.dumps(msg), encoding="utf8")
        if not self.message:
            self.getMessageInstance(topic)
        if keys:
            self.setkeys(keys)
        if tags:
            self.setTags(tags)
        self.message.set_body(msg)
        print('sending')
        response = self.produce.send_sync(self.message)
        return response

    # 发送延时消息 level 为延时等级,总共12级 最大2h
    def sendDelay(self, topic, msg, group='', keys='', tags='', level=None):
        pass

    # 获取消息队列实例
    def getMessageInstance(self, topic):
        self.message = Message(topic)

    # 设置消息体
    def setkeys(self, keys):
        if keys:
            return self.message.set_keys(keys)

    # 设置标签
    def setTags(self, tags):
        if tags:
            return self.message.set_tags(tags)

    # 关闭链接
    def close(self):
        self.produce.shutdown()
Ejemplo n.º 4
0
    def custom_init(self):
        try:
            from rocketmq.client import Producer
        except Exception as e:
            # print(traceback.format_exc())
            raise ImportError(f'rocketmq包 只支持linux和mac {str(e)}')

        group_id = f'g-{self._queue_name}'
        if group_id not in self.__class__.group_id__rocketmq_producer:  # 同一个进程中创建多个同组消费者会报错。
            producer = Producer(group_id)
            producer.set_namesrv_addr(frame_config.ROCKETMQ_NAMESRV_ADDR)
            producer.start()
            self.__class__.group_id__rocketmq_producer[group_id] = producer
        else:
            producer = self.__class__.group_id__rocketmq_producer[group_id]
        self._producer = producer
Ejemplo n.º 5
0
def produce_msg():
    """
    推送消息
    """
    producer = Producer('PID-001')  # 随便
    producer.set_namesrv_addr(con_str)  # ip和端口
    # 推送消息的时候,如果消息所占字节太长,需要手动设置size,代码中设置的是1M。
    # producer = Producer('PID-001', max_message_size=1024 * 1024)
    producer.start()
    msg.set_keys('2020-12-15')
    msg.set_tags('explain')
    msg.set_body('{"key":"value"}')
    """
    1、同步发送
    Producer 向 broker 发送消息,阻塞当前线程等待 broker 响应 发送结果。
    2、异步发送
    Producer 首先构建一个向 broker 发送消息的任务,把该任务提交给线程池,等执行完该任务时,回调用户自定义的回调函数,执行处理结果。
    3、Oneway 发送
    Oneway 方式只负责发送请求,不等待应答,Producer 只负责把请求发出去,而不处理响应结果。
    """
    ret = producer.send_sync(msg)
    print(ret.status, ret.msg_id, ret.offset)
    producer.shutdown()
Ejemplo n.º 6
0
from rocketmq.client import Producer, Message
import json

namesrv = 'localhost:9876'

producer = Producer('PID-test')
producer.set_namesrv_addr(namesrv)
producer.start()

msg_body = {"id": "001", "name": "test_mq", "message": "abcdefg"}
ss = json.dumps(msg_body).encode('utf-8')

msg = Message('topic_name')
# msg.set_keys('xxxxxx')
# msg.set_tags('xxxxxx')
msg.set_body(ss)  # message body

retmq = producer.send_sync(msg)
print(retmq.status, retmq.msg_id, retmq.offset)
producer.shutdown()
Ejemplo n.º 7
0
def producer():
    prod = Producer('testGroup')
    prod.set_namesrv_addr('127.0.0.1:9876')
    prod.start()
    yield prod
    prod.shutdown()
Ejemplo n.º 8
0
#生产消息producer
from rocketmq.client import Producer, Message
import json

producer = Producer('PID-test')
producer.set_namesrv_addr('120.131.3.191:4324')  #rocketmq队列接口地址(服务器ip:port)
producer.start()

msg_body = {"id": "001", "name": "test_mq", "message": "abcdefg"}
ss = json.dumps(msg_body).encode('utf-8')

msg = Message('topic_name')  #topic名称
msg.set_keys('xxxxxx')
msg.set_tags('xxxxxx')
msg.set_body(ss)  #message body

retmq = producer.send_sync(msg)
print(retmq.status, retmq.msg_id, retmq.offset)
producer.shutdown()

# 消费消息consumer:
# 使用PullConsumer
from rocketmq.client import PullConsumer
consumer = PullConsumer('CID_test')
consumer.set_namesrv_addr('xxx.xxx.xxx.xxx:xxxxx')
consumer.start()

for msg in consumer.pull('topic_name'):
    print(msg.id, msg.body)
consumer.shutdown()