from flask import Flask, request
from flask_redis import FlaskRedis
import time, requests, jsonify
from rocketmq.client import PushConsumer, ConsumeStatus
from celery import Celery
import all_mq
from concurrent.futures import ThreadPoolExecutor

#executor = ThreadPoolExecutor(2)

mq = all_mq.RocketMQ()
class FlaskApp(Flask):
    def __init__(self, *args, **kwargs):
        super(FlaskApp, self).__init__(*args, **kwargs)
        self._activate_background_job()

    def _activate_background_job(self):
        def run_job():
            mq.onMessage('alert')

        t1 = threading.Thread(target=run_job)
        t1.start()


app = FlaskApp(__name__)

import json, time
from rocketmq.client import PushConsumer, dll, ConsumeStatus
import traceback
import logging
class RocketMQ():
    def __init__(self):
        logging.basicConfig(level=logging.CRITICAL, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        self.logger = logging.getLogger(__name__)
        self.consumer = PushConsumer("itxxxconsumer")
        self.consumer.set_name_server_address("rxx-xxx.com:9876")
        self.topic_name = "itxx"
        #减少日志输出
        dll.SetPushConsumerLogLevel("rexxx.com:9876".encode('utf-8'), 1)
    def callback(self,msg):
        sdp_status_json = msg.body
        sdp_status_json1 = sdp_status_json.decode('utf-8')
        print(sdp_status_json1, msg.id)
        return ConsumeStatus.CONSUME_SUCCESS

    def onMessage(self, mq_tag):
        self.consumer.subscribe(self.topic_name, self.callback, mq_tag)##mq_tag为标签消费
        self.consumer.start()
        while True:
            time.sleep(200)
        self.consumer.shutdown()
    def my_func(test_body):
        print(test_body)
  • consumer.subscribe原始代码
    image.png

image.png


锅包肉
89 声望17 粉丝

这个人很懒,没有什么说的。


引用和评论

0 条评论