I solved the problem by synchronizing every access to the map and by making a
deep copy of the map whenever it is revealed to the outside. Our real
problem was that the access to the map was already synchronized but it was
not copied deeply enough at a getter so it was possible that other classes
access sub-maps of the map while it was serialized by ActiveMQ.

Code which caused the problems:
public Map<String, Map<String, String> getMap()
{
  // here a new map is produced to ensure a new object is created and the
member-variable mymap can not be accessed directly from the outside
  // however the sub-maps of mymap still remain the same object and
therefore can be directly changed from the outside which should not be the
case
  return new HashMap(mymap);
}

Code that solved the problem (not the real code, just some idea):
public Map<String, Map<String, String> getMap()
{
  HashMap newmap = new HashMap();
  for(Entry<Map<String, String>> e : mymap.entrySet())
  {
    newmap.put(e.getKey(), new HashMap(e.getValue());
  }
  return newmap;
}

Maybe this helps you a bit. I think the reason why it is not reproducable
and only happens rarely is that it is a typical multithreading-problem and
only occurs if the map is accessed exactly while it is getting serialized by
ActiveMQ.

greetings,
Martin



JIRA j...@apache.org wrote:
> 
> 
>     [
> https://issues.apache.org/activemq/browse/AMQ-2083?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=51532#action_51532
> ] 
> 
> Ruben Martin commented on AMQ-2083:
> -----------------------------------
> 
> We are facing the same problem. In our case, the first object is a custom
> object with some primitive fields and a map (HashMap). This map contains
> other deeply nested maps. We tried making the first map a synchronizedMap,
> but it did not work. And anyway both writer and reader are wraped on
> transactions (spring) and synchronized methods.
> The strange thing is that we cannot reproduce the problem in a consistent
> way in our tests, it is somehow random (maybe related to the system load?)
> 
> Martin, could you please be a bit more explicit about the way you solved
> the problem with synchronization? Any hint will be much appreciated.
> 

-- 
View this message in context: 
http://www.nabble.com/-jira--Created%3A-%28AMQ-2083%29-java.io.OptionalDataException-when-getting-a-deeply-nested-HashMap-from-an-ObjectMessage-tp21580409p23424597.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Reply via email to