Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn an Object in a thread safe class
    primarykey
    data
    text
    <p>I am having a problem that appears to indicate Java objects aren't created on the heap I expected. The short version is that I have a JMS onMessage() method that is calling another method to instantiate the proper object (routingCommand) based on the JSon payload of the TextMessage.</p> <pre><code> public void onMessage(Message message) throws RuntimeException { IRoutingCommand routingCommand = null; routingCommand = instantiateRoutingCommand(message); ... process(routingCommand); } private IRoutingCommand instantiateRoutingCommand(Message message) { IRoutingCommand routingCommand = null; ... lots of code to build the correct type of RoutingCommand routingCommand = new IeRoutingCommand(ieNotification); return routingCommand; } </code></pre> <p>The problem is that during my load test, on Extremely Rare occasions, the same JMS message is being "process()ed" multiple times. When I set maxConcurrentConsumers=1 this does not happen.</p> <p>I found this gem of wisdom on the Internet that I never knew but assuming it is correct it would explain my problem:</p> <p><em>Local references to objects are a bit different. The reference itself is not shared. The object referenced however, is not stored in each threads's local stack. All objects are stored in the shared heap.</em></p> <p>The instantiateRoutingCommand method is the around 50 lines long - that's why I broke it out to begin with. I COULD examine the JMS message enough to figure out what type of object it represents and instantiate it in the onMessage() method and convert instantiateRoutingCommand to just use setters on the passed object. That would satisfy the above stipulation. But even that is not entirely trivial and would clutter up the onMessage() method.</p> <p>I didn't try making the object volatile or making anything synchronized because if the above is true then I don't see any of that helping. So what's the best way to handle passing objects in a multi-threaded object?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload