技術メモ

神奈川在住のITエンジニアの備忘録。おもにプログラミングやネットワーク技術について、学んだことを自分の中で整理するためにゆるゆると書いています。ちゃんと検証できていない部分もあるのでご参考程度となりますが、誰かのお役に立てれば幸いです。

MessageListenerクラスのonMessageメソッド

JavaのJMS(Java Message Service)の受信側でコールされるMessageListener#onMessage()は、並列して走ることがあるのか調べてみた。

まず、JavaDocを読んでみる。

・・・the onMessage method is not called with the next message until the session has completed the last call.

MessageListener (Java(TM) EE 7 Specification APIs)

これによると、並列して走らないように読める。以下のページにも並列して走らないという記載がある。

MessageListener#onMessage() is executed as part of session thread, so you will not receive next message until return from onMessage().

java - JMS: Can we get multiple messages from queue in OnMessage() withtout commit or rollback - Stack Overflow

 

JMSの仕様として、MessageListener#onMessage()はシーケンシャルになるので、この処理の中では排他は考えなくても大丈夫だと思われる。