flask 在后台消费rocketmq

发布时间:2022-08-19 12:08


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()

        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):
        self.consumer.subscribe(self.topic_name, self.callback)
        self.consumer.start()
        while True:
            time.sleep(2)
        self.consumer.shutdown()
    def my_func(test_body):
        print(test_body)

flask 在后台消费rocketmq_第1张图片

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号