How to disable Wildfly 9.0.2 while trying to serialize specific classes in a clustered application

While setting up the cluster, I had a problem with WildFly / Infinispan trying to serialize several classes that perfectly recreate on each instance, although for some reason they seem to have decided to distribute them across the cluster.

Initially, I thought that the @Stateless annotation would have the effect that I would like, although this poses a problem due to the lack of proper constructors, so I do not believe that this is what we are looking for.

What is the correct way to disable this or overwrite a class based serialization method?

+7
java wildfly infinispan wildfly-9
source share
1 answer

Not answer

Be careful about disabling serialization for selected classes. You may not need to β€œcluster” the application and not use replicated messages or state objects if they run locally or in some limited development environment. However, after deployment for testing or production, it can be grouped. Then it will break if you prevent serialization.

Therefore, the best reason is to ensure the "serializability" of your classes, and not to prevent it.

Serialization Off answer

If you insist on disabling serialization, removing <distributable/> from your web.xml should do the trick.

Serializing Non-Serializable Responses

Having tried several serializable classes, you will find that the types of certain members of the class are simply not serializable, and you cannot change the type to something like that that will be serialized. Then use the following trick, if applicable:

 public class ClassForcedToBeSerializable implements Serializable { transient private FileWriter writer; //FileWriter is not serializable!!! private final File file; public ClassForcedToBeSerializable(File file) throws IOException { this.file = file; this.writer = new FileWriter(file); } //FLESH AND BONES HERE private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); if (writer == null) { this.writer = new FileWriter(file); } } } 

As you can see, the class includes a field of type FileWriter . We ensured serialization of the object -> bytes, by marking it transient . However, on the remote JVM, when this class returns from bytes, the field of the FileWriter field will be null . We avoid this problem by recreating it during deserialization (see the readObject() method).

This example only works because the File field contains sufficient state for FileWriter to successfully recreate.

+1
source share

All Articles