0

I have a question relating ActiveMQ and producers.

Should I create a producer for every sending of a message? Or use the same one all the time? Is there a performance impact by creating a producer for every sending?

Also the connection gets down after some period of inactivity, but I don't know if it's related to this, any advices?

Martin Spa
  • 1,494
  • 1
  • 24
  • 44

1 Answers1

0

Yes, there is a small performance impact in creating a producer, especially if the broker is located on another machine (the clients needs to talk to broker to create a producer).

In the rest of this answer I assume you use Java/JMS to talk with AMQ.

If you have a very trivial program, you could of course "re use" your producers, create them with a "NULL" destination, and set the destination when sending.

What you could do to make it easy is to use the PooledConnectionFactory which pools connections, sessions and producers. I think that wrapper class will help you.

Actually, you could use the PooledConnectionFactory like this (psuedocode):

 cf = new PooledConnectionFactory(myOriginalConnectionFactory)
 sendMessage(cf)
 sendMessage(cf)
 sendMessage(cf)

 SendMessage(connectionFactory)
   conn = connectionFactory.CreateConnection
   sess = conn.CreateSession
   prod = sess.createProducer
   msg = sess.createMessage
   prod.send(msg)
   prod.close
   sess.close
   conn.close

This means you don't have to worry about closed/open sessions, connections etc. This is the way the widely used JmsTemplate from Spring Framework works (and of course works a lot better with pooled/cached resources).

Also look at this page for performance tips and tricks.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84