def _shedual_task(self):
        try:
            from rocketmq.client import PushConsumer
        except Exception as e:
            # print(traceback.format_exc())
            raise ImportError(f'rocketmq包 只支持linux和mac {e}')
        consumer = PushConsumer(f'g-{self._queue_name}')
        consumer.set_namesrv_addr(frame_config.ROCKETMQ_NAMESRV_ADDR)
        consumer.set_thread_count(1)
        consumer.set_message_batch_max_size(self._concurrent_num)

        self._publisher = RocketmqPublisher(self._queue_name)

        def callback(rocketmq_msg):
            # self.logger.debug(f'从rocketmq的 [{self._queue_name}] 主题的queue_id {rocketmq_msg.queue_id} 中 取出的消息是:{rocketmq_msg.body}')
            self._print_message_get_from_broker('rocketmq', rocketmq_msg.body)
            kw = {
                'body': json.loads(rocketmq_msg.body),
                'rocketmq_msg': rocketmq_msg
            }
            self._submit_task(kw)

        consumer.subscribe(self._queue_name, callback)
        consumer.start()

        while True:
            time.sleep(3600)
class RocketmqPushConsumer():
    consumer = None

    def __init__(self, conf):
        pid = uuid.uuid1().hex
        self.consumer = PushConsumer(pid)
        self.consumer.set_namesrv_addr(conf)

    # 创建长连接
    def start(self):
        self.consumer.start()

    #关闭链接
    def close(self):
        self.consumer.shutdown()

    def callback(self, msg):
        print('self.callback')
        print(msg.body)
        return msg

    # 获取消息
    def getMessage(self, topic, callback=None):
        if not callback:
            callback = self.callback
        self.consumer.subscribe(topic, callback)
        self.start()
Beispiel #3
0
 def start(self, topic, name_server, consumer):
     Consumer = PushConsumer(consumer)
     try:
         Consumer.set_namesrv_addr(name_server)
     except:
         traceback.print_exc()
         Consumer.set_name_server_address(name_server)
     Consumer.subscribe(topic, self.run)
     Consumer.start()
     print("run {} task success and into the loop, wait for the message!".
           format(name_server))
     while True:  # 这里是必须的,而且每个实例都必须有单独的while阻塞,不可以共用. TODO
         pass
def consume_msg_once():
    """
    消费方式PushConsumer(即时消费)(不可重复消费)
    """
    def callback(msg):
        print(msg)

    consumer = PushConsumer('PID-001')
    consumer.set_namesrv_addr(con_str)
    consumer.subscribe("test", callback)
    consumer.start()
    while True:
        time.sleep(30)
    consumer.shutdown()
Beispiel #5
0
    def _shedual_task(self):
        consumer = PushConsumer(f'g-{self._queue_name}')
        consumer.set_namesrv_addr(frame_config.ROCKETMQ_NAMESRV_ADDR)
        consumer.set_thread_count(1)
        consumer.set_message_batch_max_size(1)

        self._publisher = RocketmqPublisher(self._queue_name)

        def callback(rocketmq_msg):
            self.logger.debug(
                f'从rocketmq的 [{self._queue_name}] 主题的queue_id {rocketmq_msg.queue_id} 中 取出的消息是:{rocketmq_msg.body}'
            )
            kw = {
                'body': json.loads(rocketmq_msg.body),
                'rocketmq_msg': rocketmq_msg
            }
            self._submit_task(kw)

        consumer.subscribe(self._queue_name, callback)
        consumer.start()

        while True:
            time.sleep(3600)
Beispiel #6
0
def push_consumer():
    consumer = PushConsumer('testGroup')
    consumer.set_namesrv_addr('127.0.0.1:9876')
    yield consumer
    consumer.shutdown()