[컴] RabbitMQ 를 이용해서 message 보내고 받기

메시지 송신 수신 / 큐 / 메시지 보내는 법 래빗mq 사용법 엠큐 /


RabbitMQ 를 이용해서 message 보내고 받기

def createTestReceiveQueue():
    parameters = pika.URLParameters(
        "amqp://guest:guest@127.0.0.1/%2F"
    )
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    result = channel.queue_declare(queue='my_q')
    channel.exchange_declare(exchange='my_ex',
                            exchange_type='direct')
    channel.queue_bind(queue='my_q',
                        exchange='my_ex',
                        routing_key='my_rk')


def send():
    parameters = pika.URLParameters(
        "amqp://guest:guest@127.0.0.1/%2F"
    )
    connection = pika.BlockingConnection(parameters)
    ch = connection.channel()
    ch.queue_declare(queue="my_q", durable=False)
    ch.basic_publish(
        exchange="my_ex",
        routing_key="my_rk",
        body=json.dumps(
            {
                "testid": "id-test",
            }
        ),
        properties=pika.BasicProperties(
            content_type="application/json", content_encoding="utf-8"
        ),
    )

def consume():
    parameters = pika.URLParameters(
        "amqp://guest:guest@127.0.0.1/%2F"
    )
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    result = channel.queue_declare(queue='my_q')
    res, prop, msg = channel.basic_get(queue=self.receiveQueueName)
    if res is None:
        # no message from queue
        return {}
    channel.basic_ack()


See Also

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

Reference

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



댓글 없음:

댓글 쓰기