Reputation: 1
For a specific requirement I have to share simple Java cache object between several tomcat instances in different machines. To achieve this, I tried with serializing the cache object and kept it in shared directory. This cache object will be referred[deserialized/readobject] by all the instances before proceeding with certain actions and updated/overwrite[serialize/writeobject] by all the instances after the actions being processed. Internally I will create a flag file[empty one] before serialize/deserialize and it will be deleted after serialization/deserialization. So if the flag file is available then cache object being used by an instance, so other instance should wait to process.
It works when the load is minimal and but when load is higher I started getting . I believe it could be because of deadlock. Is there a way to make sure simultaneous process of single file doesn't cause this issue. I can't go with Java synchronization as it is specific to instance. Your help is much appreciated.
java.io.StreamCorruptedException: invalid stream header: 00000000
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
Upvotes: 0
Views: 992
Reputation: 533442
Sharing data via files can be tricky to get right. You might look at using Chronicle Map which is designed for sharing persisted data between processes, but without creating and deleting file in a thread safe manner.
Upvotes: 1