我可以在 MessageConsumer 上注册一个监听器,然后调用 receive() 方法吗? (JMS)

Can I register a listener on MessageConsumer and then call the receive() method? (JMS)

我想知道是否可以在 MessageConsumer 上注册一个侦听器,然后调用 receive() 方法。我知道这听起来不像是一个实际场景,但知道它会很有趣。 JMS 规范甚至允许这样的事情吗?

我知道 Session 是一个单线程上下文,它的侦听器对消息的分派将是顺序的,但我可以想象实际处理我注册的侦听器的线程将会这样做,而且它必须是JMS 提供者创建的一些线程。这意味着创建消息消费者的线程是独立的,并且可以同时侦听消息。我刚才说的对吗?在那种情况下会发生什么?

你对线程问题的分析正是我的思路所在。我认为您遇到了麻烦,因为 2 个线程与一个会话交互。

好的,我在 JMS 1.1 规范中进行了查找。当我阅读它时,有两个比特说明了这一点。

  1. 4.5 - "A client may either synchronously receive a consumer’s messages or have the provider asynchronously deliver them as they arrive." Either/or,不是两者。
  2. 4.4.6 - "It is erroneous for a client to use a thread of control to attempt to synchronously receive a message if there is already a client thread of control waiting to receive a message in the same session." 我认为消息侦听器是已经在同一会话上等待接收的另一个线程。

据我所知,JMS 规范中没有描述您的场景。所以留给JMS实现。例如,在 IBM MQ 的情况下,附加消息侦听器后的接收调用将抛出代码为 MQRC_HCONN_ASYNC_ACTIVE return 的异常。