William Brogden wrote:
lets say i have 5 threads currently executing across this
I don't see how you can have 5 Threads using the same socket "at the same time" - how would you know which one gets the response?
Why not put all of your socket communication in a single object which is shared by the other processes with a typical locking mechanism.
That way only one Thread will see the loss of the connection and be responsible for re-establishing the connection.
Bill
Sorry for not explaining this better and the potential thread necro, but I wanted to update on this incase others are having problems and possibly to do a sanity check.
Here are the basicis of what i have, and what i have done that seems to be working.
I have an interface called Session, its definition is
I have the following objects the implement this interface
SessionImpl that has the actual socket and all of the logic to read and write commands to that socket
CollapsedSession that has a collection of Session's that it executes the commands across.
The implementation of executeCommand for the CollapsedSession is listed in the original post. It is here that i had my issues. Every thread that needed to execute a command would do so on the CollapsedSession. I was locking properly if the command could just be executed, however if something happened and I had to renegotiate which system was the active one, it was renegotiating once per thread that had access to the collapsed session. Causing me to get locked out by the server at the remote end and failing all other commands.
What i have done now is added a reentrant lock to the CollapsedSession, On each call to the executeCommand, it will try to acquire the lock. If it gets the lock then it will execute the command, should this fail, it will try to renegotiate the connection and send the command again as long as there are more retries left.
This is working and here is the modified code
I'm working on cleaning it up and
testing it further however, i have been able to simulate a dropped connection and it worked as expected, (only one attempt at renegotiation).