2
votes

In application based on HornetQ engine I intend to create multiple Producers and Consumers. I have learned, that I should reuse resources as much as possible thanks to this page.

Does that mean, that for my application I should crate one and exactly one ConnectionFactory, one Connection, one Session and then (using this Session object) creating as many Producers/Consumers as I want?

That shouldn't be hard, but I'm not sure if this is the proper approach.

1

1 Answers

10
votes

The best rule of thumb for minimum resource usage is to use the fewest constructs as possible while remaining thread safe. Accordingly:

  1. Connection Factories are thread safe: One per JMS server (or one per JMS server per destination type for topics and queues)
  2. Connections are thread safe: Depending on the application architecture, you may be able to use one connection, but I would not bend over backwards to do this.
  3. Sessions and all constructs below the session are NOT thread safe: You will need one session per concurrent thread (or per transaction if you think about it that way).

Based on that, hopefully you can strike a balance between an elegant architecture and low resource utilization.