有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用来自不同线程的JMS会话

从javadoc for Session中可以看出:

A Session object is a single-threaded context for producing and consuming messages.

因此,我理解您不应该同时使用来自两个不同线程的会话对象。我不清楚的是,您是否可以使用会话对象(或子对象,如队列)来自与其创建的线程不同的线程

在我正在处理的情况下,我正在考虑将会话对象放入一个可用会话池中,任何线程都可以从该池中借用、使用并在完成后返回该池

这是洁食吗

(顺便说一句,如果这对答案有任何影响,请使用ActiveMQ。)


共 (1) 个答案

  1. # 1 楼答案

    我认为the JMS 1.1 spec中第4.4节的脚注说明了一些问题:

    There are no restrictions on the number of threads that can use a Session object or those it creates. The restriction is that the resources of a Session should not be used concurrently by multiple threads. It is up to the user to insure that this concurrency restriction is met. The simplest way to do this is to use one thread. In the case of asynchronous delivery, use one thread for setup in stopped mode and then start asynchronous delivery. In more complex cases the user must provide explicit synchronization.

    根据我对规范的阅读,只要您正确管理并发性,您想做什么就可以了