The non-answer
Be careful with the approach of disabling serialisation for selected classes.
Your application might not need to be 'clustered' and not needing replicated sesssions or stateful entities, when run locally or in some limited development environment. However once deployed to test or production it could be clustered. Then it's going to be broken, if you have prevented serialization.
Therefore the best cause of action is to ensure 'serializability' of your classes, rather than force-preventing it.
Serialization OFF answer
If you insist on disabling serialization, removing <distributable/>
from your web.xml
should do the trick.
Serializing non-serializables answer
By trying to make some classes serializable sometimes you will find that types of certain class members are simply non-serializable and you cannot swap the type for something similar, which would serialize.
Then use the following trick, providing it is 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 field of FileWriter
type. We ensured the object -> bytes serialization by marking it transient
. However on the remote JVM when this class is being brought back from bytes, your FileWriter
field field will be null
. We avoid this problem by recreating it during deserialization (see readObject()
method).
This example works only because the File
field carries enough state for FileWriter
to be successfully recreated.
@Stateful
turns the class into a stateful EJB, which is then automatically distributed across the cluster to provide fault-tolerance. – Hamrnand@Stateless
or@Singleton
for this, but in that case you'd need to add a no-args constructor to your class. – Hamrnand