Concurrent Message Use

For simplicity, it is suggested that application programmers create a new message for every asynchronous send call. If concurrent message use is acceptable in an application, messages may be reused when sending asynchronously, but generally it is not recommended due to the complexity it may add.

Warning: During asynchronous sends, the application programmer should be very aware of concurrent message usage between the application and the thread handling completion listeners. The message passed to the completion listener is the same message passed to the MessageProducer send method, which means modification of that particular message is reflected in both the application thread and the thread invoking the completion listener.

For example, if a TextMessage is asynchronously sent with the text of foo, and then the same message object's text is subsequently set to bar, it is conceivable that when the completion listener is invoked the message will contain bar even though it contained foo at the time it was sent.