Serialization Problem when using HashMap

I’m using JRuby in a Rails application, which is supported by a Java
backend. One of the Java Interface functions requires a hashmap be
passed
from the JRuby to the Ruby code, and I’m running into an unexpected
RuntimeException in this case.

The Hashmap construction is very straightforward:

def java_map
hash_map = ::Java::JavaUtil::HashMap.new
hash_map[:original_document_uri] = “”.to_s
end

This construction causes no errors in JRuby, and I can nicely access the
map
as expected.

The trouble comes when I attempt to pass this map into Java:

NativeException (java.lang.RuntimeException:
org.jcodings.specific.ASCIIEncoding):
org/apache/activemq/command/ActiveMQObjectMessage.java:111:in
storeContent' org/apache/activemq/command/ActiveMQObjectMessage.java:162:insetObject’
org/apache/activemq/ActiveMQSession.java:380:in createObjectMessage' org/logicblaze/lingo/jms/marshall/DefaultMarshaller.java:60:increateRequestMessage’
org/logicblaze/lingo/jms/JmsClientInterceptor.java:131:in invoke' net/zerofootprint/jms/JmsProxyFactoryBean.java:58:ininvoke’
org/springframework/aop/framework/ReflectiveMethodInvocation.java:172:in
proceed' org/springframework/aop/framework/JdkDynamicAopProxy.java:202:ininvoke’
$Proxy15:-1:in …(I’ve removed my application specific details here,
as
they seem unimportant)

That is the stacktrace that I get on my rails log, and in my java log I
get:

java.lang.RuntimeException: org.jcodings.specific.ASCIIEncoding
… (similar trace to above)
Caused by: java.io.NotSerializableException:
org.jcodings.specific.ASCIIEncoding
at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at java.util.HashMap.writeObject(HashMap.java:1000)
… (far too long for inclusion)

As you can see, I am using ActiveMQ as my broker, and have the java
running
in a Tomcat instance, while the JRuby is running inside Trinidad.

I’m not sure if the HashMap here is a red herring, but it seems that
whatever I put into the hashmap causes this exception.

Finally, I tried using an empty hashmap, and that seemed to work ok.

I hope someone can lend me a bright Idea.

Hi Ian,
something You put into the hashmap is using
org.jcodings.specific.ASCIIEncoding
so while serializing your map You get:
Caused by: java.io.NotSerializableException:
org.jcodings.specific.ASCIIEncoding
and that’s why empty map works.

Best greetings,
Paweł Wielgus.

2011/1/25 Ian B. [email protected]:

Pawel,

Thanks for getting back. That was my belief, but I couldn’t understand
why

hash_map[:original_document_uri] = "".to_s

would be causing that error. It turns out that it’s the symbol on the
lhs
which causes the problem.

Thanks,
Ian

2011/1/25 Paweł Wielgus [email protected]