0

I am new to the world of Message Queues and I am currently evaluating RabbitMQ, ActiveMQ and Kafka. I see that in RabbitMQ, the Producer will create a Connection to the RabbitMQ server and the thread holding the Connection will remain active until the connection is closed. This leads me to believe that there MUST be a separate thread which delivers information to the RMQ Producer thread which will simply publish the message to the queue and keep looping until connection to the RMQ Server is closed? Is this assumption correct? Any thoughts/inputs would be appreciated.

Thanks!

P.S: This isn't the behaviour with Kafka. [ Apache Kafka: Java Producer reusability ]

Community
  • 1
  • 1
z00lander
  • 65
  • 4
  • A Publisher can simply open related connections/channels and then post the message to queue i.e RabbitMQ broker and then can safely close the connections/channels and exit. Message will remain in broker till it get consumed. For this to happen the Publisher should post *persistent* messages and queue should be *durable*. – sameerkn Jul 20 '16 at 08:38
  • Thanks @sameerkn. Essentially what I want is, Producer/Publisher should write the message and exit as soon as possible. A few seconds later, the next message comes in. Now I need to go through the overhead of setting up the Connection again right? I would like to avoid that additional overhead of repeatedly creating the Connection for every message that I want to publish. – z00lander Jul 20 '16 at 08:41
  • Then, you will need a separate thread which will simply keep on accepting and posting the messages to RabbitMQ broker. This thread will do all the needful of keeping the connections/channels open until the Publisher/Application/MainThread whose concern is just posting the message exists. – sameerkn Jul 20 '16 at 09:10

1 Answers1

2

in general, you should have a single RMQ connection per application instance. that connection can be opened as soon as your application starts.

having a connection does not yet give you the ability to publish or consume messages, though.

to do that, you need to create a channel.

the general best practice is one channel per thread in your application. need to publish a messages from this thread? create a channel for the thread. done with publishing it and not doing any other RMQ work on this channel? close the channel.

unlike connections, channels are cheap and easy to create. they work over the existing RMQ connection, and they take very little resources to create.

you can create thousands of channels in a single connection (though you might want to limit that number for performance reasons)

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219