Queues

2024-08-26 11:04
文章标签 queues

本文主要是介绍Queues,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

RabbitMQ 是一个消息中间件: 它接收、存储并转发消息数据。本教程将带你通过一系列步骤来设置和使用 RabbitMQ。

环境准备

1. 安装 RabbitMQ

- [下载](https://www.rabbitmq.com/download.html)并安装RabbitMQ服务器。

- 启动RabbitMQ服务器。

- 通过浏览器访问 `http://localhost:15672/` 来查看管理界面。

2. 安装客户端库

- 对于Python, 安装 pika 库:

```bash

pip install pika

```

Hello World 示例

发送者 (Sender)

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',

routing_key='hello',

body='Hello World!')

print(" [x] Sent 'Hello World!'")

connection.close()

```

接收者 (Receiver)

```python

import pika

def callback(ch, method, properties, body):

print(" [x] Received %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_consume(queue='hello',

on_message_callback=callback,

auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.start_consuming()

```

工作队列 (Work Queues)

创建工作队列

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = 'A message to the task queue!'

channel.basic_publish(exchange='',

routing_key='task_queue',

body=message,

properties=pika.BasicProperties(

delivery_mode=2, # make message persistent

))

print(" [x] Sent %r" % message)

connection.close()

```

消费者

```python

import pika

import time

def callback(ch, method, properties, body):

print(" [x] Received %r" % body.decode())

time.sleep(body.count(b'.'))

print(" [x] Done")

ch.basic_ack(delivery_tag=method.delivery_tag)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

channel.basic_qos(prefetch_count=1)

channel.basic_consume(queue='task_queue', on_message_callback=callback)

channel.start_consuming()

```

发布/订阅 (Fanout)

发布者

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

message = "Info: Hello, this is a fanout message!"

channel.basic_publish(exchange='logs', routing_key='', body=message)

print(" [x] Sent %r" % message)

connection.close()

```

订阅者 1

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

```

订阅者 2

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

```

路由 (Routing)

发布者

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

severity = 'info'

message = f'Info: Hello from {severity} routing!'

channel.basic_publish(exchange='direct_logs', routing_key=severity, body=message)

print(" [x] Sent %r:%r" % (severity, message))

connection.close()

```

消费者 1 (error)

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

severities = ['error']

for severity in severities:

channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

```

消费者 2 (warning)

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

severities = ['warning']

for severity in severities:

channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

```

主题交换 (Topics)

发布者

```python

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

routing_key = 'kern.critical'

message = 'A critical kernel error'

channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message)

print(" [x] Sent %r:%r" % (routing_key, message))

connection.close()

```

消费者 1 (kern.*)

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

binding_keys = ['kern.*']

for binding_key in binding_keys:

channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

```

消费者 2 (*.*)

```python

import pika

def callback(ch, method, properties, body):

print(" [x] %r" % body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

channel = connection.channel()

channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

result = channel.queue_declare(queue='', exclusive=True)

queue_name = result.method.queue

binding_keys = ['*.*']

for binding_key in binding_keys:

channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key=binding_key)

print(' [*] Waiting for logs. To exit press CTRL+C')

channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

这篇关于Queues的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1108378

相关文章

Zookeeper 进阶之——Zookeeper编程示例(如何使用Zookeeper实现屏障Barriers和队列Queues)...

[quote]原文:http://www.cnblogs.com/haippy/archive/2012/07/26/2609769.html [/quote] [b]引言[/b] 本文将告诉你如何使用 Zookeeper 实现两种常用的分布式数据结构,屏障(barriers) 和队列(queues),我们为此还分别实现了两个类:Barrier and Queue. 本文中的例子假设你已

SpringBoot的RabbitMQ消息队列: 三、第二模式Work queues

上一节的两个工程,一个负责发送,一个负责接收,也就是一一对于的关系。      只要消息发出了,接收者就处理;当接收效率较低时,就会出现接收者处理不过来,我们就可能会处理不过来,于是我们就可能多配置接受者。这个模式就是"Work queues",它的结构如下     多个接收者,它们会出现什么情况呢?是否像大锅饭,有的人撑死,有的人饿死。这个通过例子验证。 一、再建一个接

[LeetCode] Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the top element.empty() – Return whether th

leetcode Implement Stack using Queues

Implement Stack using Queues 题目:https://leetcode.com/problems/implement-stack-using-queues/ Implement Stack using Queues  解题思路:使用两个队列实现栈,始终保持一个队列为空。 class MyStack {Queue<Integer> queue1=new LinkedL

JMS(Java Messaging Service)—— Queues、Topics

1.点对点模式 点对点的模式主要建立在一个队列上面,当连接一个列队的时候,发送端不需要知道接收端是否正在接收,可以直接向ActiveMQ发送消息,发送的消息,将会先进入队列中,如果有接收端在监听,则会发向接收端,如果没有接收端接收,则会保存在activemq服务器,直到接收端接收消息,点对点的消息模式可以有多个发送端,多个接收端,但是一条消息,只会被一个接收端给接收到,哪个接收端先连上Activ

LeetCode--Implement Stack using Queues

Problem: Implement the following operations of a stack using queues. - push(x) – Push element x onto stack. - pop() – Removes the element on top of the stack. - top() – Get the top element. - em

leetcode-225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whet

Dissecting Message Queues(不同消息中间件Throughput和latency

转载自:http://bravenewgeek.com/dissecting-message-queues/ 有一些结果图片无法显示,可以去原文查看. Continuing my series on message queues, I spent this weekend dissecting various libraries for performing distributed mes

RabbitMQ 的队列 queues 名称为空的解决方法

今天碰到很奇怪的问题,  rabbitmq的队列名称居然为空了,  队列中积累了两百多万的数据, 快把mq搞蹦了,  没有队列名称就删除不了,也清空不了消息, 在服务器节点上:  rabbitmqctl  list_queues   查询队列也超时 这个时候就想起来: 只能把这个队列所在的virtual host 虚拟空间干掉, 不然没办法删除了。 步骤: 1 备份 该队列的  virtua

iOS并发编程中Operation与Dispatch Queues实践

导读: 本文为读《Concurrency Programming Guide》笔记第二篇,在上篇分享了OS X和iOS应用开发中实现任务异步执行的技术以及应注意的事项之后,作者付宇轩(@DevTalking)对Operation对象的设置与执行,以及Dispatch Queues的创建与管理进行了实践总结。 系列阅读 iOS开发中设计并发任务技术与注意事项iOS并发编程中Operatio