python 的rabbitmq 实例

import pika
def rabbitmq_conn():
    credentials = pika.PlainCredentials(mq_config["user"], mq_config["passwd"])
    parameters = pika.ConnectionParameters(mq_config["host"],mq_config["port"],'/',credentials)
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    return channel

#######生产者   
body = 'body'
    channel.basic_publish(exchange='',
                          routing_key='hello',
                          body=body)
    print " [x] Sent %s" %body
    connection.close()
#########消费者
def deal_msg_callback(ch, method, properties, body):
    print body
    ch.basic_ack(delivery_tag = method.delivery_tag)
channel=rabbitmq_conn()
channel.basic_consume(deal_msg_callback,queue='123456')
channel.start_consuming()




#####################################################################
一个rabbitmq的类
import  kafka
from pika import  connection,BlockingConnection,ConnectionParameters,credentials,BasicProperties
#单例类
class _RmqMgrSingleton(type):
    def __init__(self, name, bases, dict):
        super(_RmqMgrSingleton, self).__init__(name, bases, dict)
        self._instance = {}
    def __call__(self, host,port,user,passwd):
        if not self._instance.has_key((host,port,user,passwd)):
            self._instance[(host,port,user,passwd)] = super(_RmqMgrSingleton, self).__call__(host,port,user,passwd)
        return self._instance[(host,port,user,passwd )]
class RmqMgr(object):
    "rabbit mq 队列的一个抽象"
    __metaclass__ = _RmqMgrSingleton
    def __init__(self,host,port,user,passwd):
        self.host=host
        self.port=port
        self.user,self.passwd=user,passwd
    def _get_rmq_channel(self ,host,port,user,passwd,withconn=False):
        "获取连接到rabbitmq的通道对象"
        conn=BlockingConnection(ConnectionParameters(host=host,port=port,credentials=credentials.PlainCredentials(username=user,password=passwd)))
        if withconn==False:
            return  conn.channel()
        else:
            return conn.channel(),conn
    def send_message(self ,exchange,routeing_key,body):
        "发送消息到队列"
        if type(body)==unicode:
            body=body.encode("utf-8")
        assert type(body)==str
        try:
            self.__channel.basic_publish(exchange=exchange,routing_key=routeing_key,body=body,properties=BasicProperties(delivery_mode=2))
        except:
            self.__channel,self.__conn=self._get_rmq_channel(self.host,self.port,self.user,self.passwd,withconn=True)
            self.__channel.basic_publish(exchange=exchange,routing_key=routeing_key,body=body,properties=BasicProperties(delivery_mode=2))
        return True
    def __del__(self):
        self.__conn.close()
        
        
import requests
def get_rabbitmq_info():
    url = "http://"+RMQ["user"]+":"+RMQ["passwd"]+"@"+RMQ["host"]+":15672/api/queues/"
    for i in requests.get(url).json():
        if i["name"]=="v3baninfo_precheck":
            if int(i["messages"]) > 50000:
                return True
            else:
                return False


转载于:https://my.oschina.net/u/1458120/blog/639295

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值