Hi,
We have implemented a "KeepAlive service" that we use over TLS.
This service will send a message every 10 seconds to a server and if we get a response then we are ok and connection is considered ok.
This is started using:
KeepAlive keepAlive = new KeepAlive(this);
keepAlive.init();
future = scheduledTaskExecutor.newScheduledTask(keepAlive, 0, properties.getKeepAliveTime(),
TimeUnit.SECONDS);
Note: newScheduledTask() calls ScheduledFuture<?> future = executorService.scheduleAtFixedRate(runnable, delay, period, timeUnit);
However we also have a method where we "instantly" want to find out if we are connected to the server.
This is how it looks like:
public synchronized boolean isConnected() {
KeepAlive keepAlive = new KeepAlive(this);//create another keepAlive for same session.
keepAlive.init();
if (scheduledTaskExecutor!= null) {
ScheduledFuture<?> scheduledTaskOnce = scheduledTaskExecutor.newScheduledTaskOnce(keepAlive, 10,
TimeUnit.MILLISECONDS);
try {
Object object = scheduledTaskOnce.get();
if (object == null) {
logger.debug("Task has finished! {}", object);
}
} catch (InterruptedException | ExecutionException e) {
logger.debug("We could not finished check of connection", e);
}
return keepAlive.isAlive();
}
Note: newScheduledTaskOnce() calls ScheduledFuture<?> future = executorService.schedule(runnable, delay, timeUnit);
Q1: The KeepAlive hangs when doing it as a onetime task in the same executor. Any ideas why?
Q2: How will a onetime task in same scheduledExecutor be handled when we have a repeatedly ongoing?
br,
//mike
This service will send a message every 10 seconds to a server and if we get a response then we are ok and connection is considered ok.
future = scheduledTaskExecutor.newScheduledTask(keepAlive, 0, properties.getKeepAliveTime(),
TimeUnit.SECONDS);
Note: newScheduledTask() calls ScheduledFuture<?> future = executorService.scheduleAtFixedRate(runnable, delay, period, timeUnit);
However we also have a method where we "instantly" want to find out if we are connected to the server.
ScheduledFuture<?> scheduledTaskOnce = scheduledTaskExecutor.newScheduledTaskOnce(keepAlive, 10,
TimeUnit.MILLISECONDS);
Object object = scheduledTaskOnce.get();
Note: newScheduledTaskOnce() calls ScheduledFuture<?> future = executorService.schedule(runnable, delay, timeUnit);
Q1: The KeepAlive hangs when doing it as a onetime task in the same executor. Any ideas why?
Q2: How will a onetime task in same scheduledExecutor be handled when we have a repeatedly ongoing?
Q1: The KeepAlive hangs when doing it as a onetime task in the same executor. Any ideas why?
Q2: How will a onetime task in same scheduledExecutor be handled when we have a repeatedly ongoing?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 134:32:15 |
Calls: | 6,857 |
Calls today: | 3 |
Files: | 12,360 |
Messages: | 5,418,384 |