[컴] RabbitMQ temporary queue

queue / 임시큐 / 송수신 다른 큐/ queue /

RabbitMQ temporary queue

아래처럼 임시 queue 를 만들 수 있다.
result = channel.queue_declare(queue='', exclusive=True)

한명 접속 후 삭제

exclusive=True 주면, cosumer 의 connection 이 close 되면, queue 가 삭제된다.(참고:Queues — RabbitMQ)

example

"""
                                  to_b_rk     +-----------------+
                                              |                 |
+--------+                          +------>  |     to_b_q      |          +---------+
|        |                          |         |                 +--------> |         |
|        |                          |         +-----------------+          |         |
|        |       +----------+       |                                      |         |
|    A   +-----> | to_b     |       |                                      |   B     |
|        |       |          +-------+                                      |         |
|        |       +----------+                                              |         |
|        |       +----------+                 +------------------+         |         |
+----+---+       | to_a     |                 |                  |         |         |
     ^      +--> |          +---------------->+ <random_name>    +-----+   +---+-----+
     |      |    +----------+                 | temporary queue  |     |       |
     |      |                  <random_name>  +------------------+     |       |
     |      |                                                          |       |
     |      |                                                          |       |
     |      |                                                          |       |
     |      +------------------------------------------------------------------+
     |                                                                 |
     |                                                                 |
     +-----------------------------------------------------------------+


"""

parameters = pika.URLParameters('amqp://guest:guest@127.0.0.1:5672/%2F')
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

to_a = 'to_a'
to_b = 'to_b'
to_b_q = 'to_b_q'
to_b_rk = 'to_b_rk'

# to_b queue
channel.exchange_declare(exchange=to_b,
                         exchange_type='direct')
channel.queue_declare(queue=to_b_q)  # idempotent
channel.queue_bind(exchange=to_b,
                   queue=to_b_q,
                   routing_key=to_b_rk)

# to_a queue, random temporary queue
channel.exchange_declare(exchange='to_a',
                         exchange_type='direct')
result = channel.queue_declare(queue='', exclusive=True)
recv_queue = result.method.queue
channel.queue_bind(exchange='to_a',
                   queue=recv_queue,
                   routing_key=recv_queue)

# send message to to_b queue
data = {}
data['to_a_queue'] = recv_queue
message = json.dumps(data)
channel.basic_publish(exchange=to_b,
                      routing_key=to_b_rk,
                      body=message)


python 에서 간단한 메시지 전송을 하려면 ref. 1 을 참고하자.

See Also

  1. 쿠...sal: [컴] RabbitMQ 자료들

Reference

  1. RabbitMQ tutorial - "Hello world!" — RabbitMQ

댓글 없음:

댓글 쓰기