开发者

rabbitmq multiple consumer and multiple publisher

开发者 https://www.devze.com 2023-01-28 05:06 出处:网络
Want to know the behavior of rabbitmq multiple publisher and consumer. Does rabbitmq server gives one message to any one of the consumer at a time and other consumers are ideal at that time?

Want to know the behavior of rabbitmq multiple publisher and consumer. Does rabbitmq server gives one message to any one of the consumer at a time and other consumers are ideal at that time?

OR

Consumers pick any unattended message from queue, so that at a time, more than one consumers are cons开发者_开发技巧uming the message from queue?

Basically I am designing a database queue and do not want more than one inserts at a time.


A message from the queue will be delivered to one consumer only. Ie: once the message makes its way to the queue - it won't be copied (broadcasted) to multiple consumers.

If you want to do broadcast - you have to use multiple queues.

See this tutorial for more details: http://www.rabbitmq.com/tutorial-two-python.html


yes , RabitMQ supports multiple publisher and consumer.

  1. Multiple Publisher

    For publishing a messsge to rabbitmqyou need to declare a factory and do a connection to the rabbitmq server. then decare a chennel to rabbitmq

    ConnectionFactory FACTORY = new ConnectionFactory
    

    FACTORY.setUsername ("guest")

    FACTORY.setPassword ("guest")

    FACTORY.setVirtualHost ("\")

    FACTORY.setPort (5572)

    FACTORY.setHost ("localhost")

    Connection connection=FACTORY.newConnection Channel channel = connection.createChannel

the basic key to route a message is a routing key

channel.basicPublish(EXCHANGE_NAME, "Queue1", MessageProperties.PERSISTENT_TEXT_PLAIN, "msg1".getBytes)

channel.basicPublish(EXCHANGE_NAME, "Queue2", MessageProperties.PERSISTENT_TEXT_PLAIN, "msg2".getBytes)

these two messages will be published to a seperate queue as per the routing key as mention queue1 and queue2

2.Multiple Consumer

for multiple consumer we declare a queue and bind to a particular routing key the the message to that routing key will be publishe to respected queue.

  channel.exchangeDeclare(EXCHANGE_NAME, "direct", durable)
  channel.queueDeclare("q1", durable, false, false, null)
  channel queueBind ("q1", EXCHANGE_NAME,"queue1")// routing key = "queue1"
  val q1Consumer = new QueueingConsumer(channel)
  channel basicConsume ("q1", false, q1Consumer)

like this u can consume messages from first queue and same goes for second queue but specify the routing key as "queue2"

  channel.exchangeDeclare(EXCHANGE_NAME, "direct", durable)
      channel.queueDeclare("q2", durable, false, false, null)
      channel queueBind ("q2", EXCHANGE_NAME,"queue2") // routing key = "queue2"
      val q2Consumer = new QueueingConsumer(channel)
      channel basicConsume ("q2", false, q2Consumer)
0

精彩评论

暂无评论...
验证码 换一张
取 消