问题
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:
The best rule of thumb for minimum resource usage is to use the fewest constructs as possible while remaining thread safe. Accordingly:
- Connection Factories are thread safe: One per JMS server (or one per JMS server per destination type for topics and queues)
- 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.
- 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.
来源:https://stackoverflow.com/questions/4335895/should-i-use-just-one-session-in-my-application-based-on-hornetq