2

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.

ŁukaszBachman
  • 33,595
  • 11
  • 64
  • 74

1 Answers1

10

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.

Nicholas
  • 15,916
  • 4
  • 42
  • 66
  • side q, is it safe to create 2x producers from the same session and use them in different threads potentially at the same time ? – mP. Jan 03 '11 at 00:45
  • Hello mP; I would say not. I mean, it might work, depending on the JMS implementation and your implementation, but it is definitely contra-indicated. Instead, I would look at implementing some sort of server session pool where producers take a session from a pool, send a message and return the session to the pool. – Nicholas Jan 03 '11 at 15:01
  • How about creating new session for each transaction? does that make sense? – YaOg Oct 02 '14 at 07:38