I forgot to add that your experiment probably didn't show changes because I believe by default the ActiveMQConnection has the flag "copyMessageOnSend" set to true. So the message would have been copied anyways into a new object which means the whole copy by reference wording is even less relevant and more wrong since a copy of the message is still sent even with the VM transport by default.
See: https://activemq.apache.org/connection-configuration-uri https://github.com/apache/activemq/blob/activemq-5.16.1/activemq-client/src/main/java/org/apache/activemq/ActiveMQSession.java#L1955 On Thu, Apr 1, 2021 at 8:28 AM Christopher Shannon < christopher.l.shan...@gmail.com> wrote: > Yes, Java is pass by value but the value is a copy of the reference and > not an independent copy of the message (unlike C or C++). The documentation > wording should be fixed as it is wrong as you pointed out but I assume > whoever wrote that was just referring to the fact that when the message is > sent by VM transport the same reference is used so you need to be careful > as changes to the original message after sending would also affect the > message that was already sent since they are the same object still. The > messages are just stored in a data structure in the JVM (I think a > LinkedBlockingQueue). So this is just (poorly) trying to convey the fact > that the messages are not independent copies of the message like they > would be if sent over TCP or another protocol. > > On Thu, Apr 1, 2021 at 7:53 AM Cunningham <headde...@gmail.com> wrote: > >> Java's built-in evaluation strategy [1] is 100% pass-by-value [2]. >> >> A question regarding that fact arose for me when I read this note in the >> ActiveMQ documentation: "One thing to note with the VM transport is that >> messages are passed by reference" [3]. That contradicts the well-known >> fact >> about Java. >> >> Another question arose about this: "disable the automatic serialization of >> ObjectMessage payloads so that the objects are passed by value in 4.x by >> setting the objectMessageSerializationDefered flag to true" [4]. >> >> The documentation leads you to expect that setting >> objectMessageSerializationDefered to false somehow transforms Java's >> built-in pass-by-value evaluation strategy to instead do >> pass-by-reference. >> >> ActiveMQ's documentation is incomplete and not 100% crystal clear. Since >> Java being pass-by-value is a given, the ActiveMQ documentation would be >> more complete and less confusing if its contradictory use of >> pass-by-reference and pass-by-value were explained in more detail. >> >> To try to understand what context ActiveMQ uses >> pass-by-reference/pass-by-value, I've looked at some unit tests here [5] >> and here [6]. Neither of those unit tests exercise what's claimed in the >> documentation I quoted. I didn't see anything that looked like >> pass-by-reference in the VMTransport code either [7]. >> >> So I did some experimenting. I instantiated a new ActiveMQObjectMessage in >> a method that implements one of several consumers in the receiving end of >> a >> VM transport. In that one mutating consumer's method, I then assigned the >> locally instantiated new message to the original reference received from >> the destination that several consumers subscribed to. >> >> If ActiveMQ truly does pass-by-reference in the standard sense of the >> term, >> then I would expect the assignment of the newly instantiated message >> created in the one mutating consumer's method would be seen by all the >> other consumers. I would expect the producer that sent the original >> message >> to also be able to observe — after sending the original message — that its >> original reference would eventually refer to the new message instantiated >> and assigned in the mutating consumer. >> >> Neither of those expectations were borne out in my experiment. Which is >> not >> a surprise, since Java doesn't do pass-by-refrence. >> >> Therefore my questions are: >> >> 1. In what sense does the ActiveMQ documentation mean "passed by >> reference"? >> 2. Where can I find existing unit tests that exercise and verify >> ActiveMQ's >> expected pass-by-reference/pass-by-value behavior? >> 3. Is that ActiveMQ documentation even relevant for ActiveMQ 5+? Or only >> for 3x and 4x? >> >> >> ---- >> >> >> [1] https://bit.ly/WikiPbV >> [2] https://bit.ly/JavaDudePbV >> [3] https://bit.ly/VMTransPbR >> [4] https://bit.ly/DisObjSerPbV >> [5] https://bit.ly/ObjSerTest >> [6] https://bit.ly/ObjMsgNotSer >> [7] https://bit.ly/VMTrans >> >