I have a series of objects that are sent over a network socket.
The code for reading the data is already implemented and kind of works, but my problem is that sometimes the socket timeout cuts the read (ObjectInputStream.readObject()
) of an object, corrupting the stream.
I use a timeout because this reading is performed within a JBoss resource adapter, so I don't want it to block indefinitely and potentially disable undeployment or graceful stopping of the EAR.
Is there a graceful (interruptible) way of handling reading object streams without using a separate thread and forcefully killing it?
P.S. I am aware that using object streams is a very bad decision, but that's just how the client applications are sending the data, and I can't change that.
I have something like this.
private volatile boolean closed = false;
private Closable/*e.g. ObjectOutputStream */ onClose = ...;
public void close() {
closed = true;
try {
if (onCLose != null) onClose.close();
} catch(IOException ignored) {
}
}
private Object readObject() {
try {
if (closed) throw new IllegalStateException();
return steam.readObject();
} finally {
if (closed) throw new IllegalStateException();
}
}