rabbitmq_有选择发送接收_消息过滤
生产者_procedure_send_有选择的发消息
在茂名等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都网站制作、外贸网站建设 网站设计制作定制设计,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销,外贸网站建设,茂名网站建设费用合理。
importpika
importsys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
exchange_type='direct')
severity = sys.argv[1]iflen(sys.argv) >1else'info'
message =' '.join(sys.argv[2:])or'Hello World!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r"% (severity, message))
connection.close()
消费者_consumer_recive_有选择的接收消息
importpika
importsys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
exchange_type='direct')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
severities = sys.argv[1:]
if notseverities:
sys.stderr.write("Usage: %s [info] [warning] [error]"% sys.argv[0])
sys.exit(1)
print(severities)
forseverityinseverities:
channel.queue_bind(exchange='direct_logs',
queue=queue_name,
routing_key=severity)
print(' [*] Waiting for logs. To exit press CTRL+C')
defcallback(ch, method,properties, body):
print(" [x] %r:%r"% (method.routing_key, body))
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
生产者_procedure_send_更详细的的过滤发送
importpika
importsys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
exchange_type='topic')
routing_key = sys.argv[1]iflen(sys.argv) >1else'anonymous.info'
message =' '.join(sys.argv[2:])or'Hello World!'
channel.basic_publish(exchange='topic_logs',
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r"% (routing_key, message))
connection.close()
消费者_consumer_recive更详细的过滤接收
importpika
importsys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
exchange_type='topic')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
binding_keys = sys.argv[1:]
if notbinding_keys:
sys.stderr.write("Usage: %s [binding_key]..."% sys.argv[0])
sys.exit(1)
forbinding_keyinbinding_keys:
channel.queue_bind(exchange='topic_logs',
queue=queue_name,
routing_key=binding_key)
print(' [*] Waiting for logs. To exit press CTRL+C')
defcallback(ch, method,properties, body):
print(" [x] %r:%r"% (method.routing_key, body))
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
文章题目:rabbitmq_有选择发送接收_消息过滤
转载来源:http://azwzsj.com/article/jojppi.html