What is the best approach to create a long lived publisher in rabbitmq? The goal is to use the same producer for multiple messages in different classes. I want to avoid creating a new connection and new channel for each message.
Asked
Active
Viewed 243 times
0
-
Are you using a library for RabbitMQ, such a Spring? These libraries have these capabilities built in. – Boris the Spider May 10 '15 at 21:41
-
I have to use the RabbitMQ java client. – Ni12N May 10 '15 at 21:42
-
Are you using Spring too? – Nicolas Labrot May 11 '15 at 06:50
2 Answers
0
I want to avoid creating a new connection and new channel for each message.
Did you try just not doing that, and keeping the old connection and corresponding channel alive? You can create a helper/singleton class for that. As for the closure of both channel and connection, you'd probably need to override finalize
for that. See this topic for more info on destructors

Community
- 1
- 1

CptBartender
- 1,235
- 8
- 22
0
I suggest using IOC to handle this for you. Setup the IConnection as a singleton (see this post for help with that https://stackoverflow.com/a/30032001/1173800) and then each time you instantiate your IProducer it will handle re-using the connection for you.