I need to create a single threaded watcher that monitors socket connections to see if they are open or were open before, and in case it is false, output a log line.
In the classes that opens the socket connection (3 different ones) I created a timer like the example bellow but that will create multiple threads which I do not want:
if (connectionToCheck()) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (!socket.isConnected()) {
LOG.info("connection - {} second timeout: { host={}, port={} }",DEFAULT_TIMEOUT ,host ,port );
}
}
}, DEFAULT_TIMEOUT*1000);
socket.connect(new InetSocketAddress(host, port), connect_timeout);
I am not very experienced on java so I was wandering if someone could give me some pointers on how to get started on this.
Thanks in advance.
Edit:
The idea is that multiple connections will be going at the same time and they will all need to be watched under by this new class. So the first connection would create the watcher object and subsequent ones would just add new connections to be monitored).