de.mklinger.qetcher.client.impl.lookup.ScheduledNodesLookup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qetcher-client-bundle Show documentation
Show all versions of qetcher-client-bundle Show documentation
Qetcher Java client, OSGi bundle, minimal dependencies
package de.mklinger.qetcher.client.impl.lookup;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.mklinger.micro.annotations.VisibleForTesting;
import de.mklinger.qetcher.client.QetcherClient;
import de.mklinger.qetcher.client.common.concurrent.Delay;
/**
* @author Marc Klinger - mklinger[at]mklinger[dot]de
*/
public class ScheduledNodesLookup implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(ScheduledNodesLookup.class);
private final QetcherClient client;
private final NodesHolder nodesHolder;
private final AtomicBoolean closed;
public ScheduledNodesLookup(final QetcherClient client, final NodesHolder nodesHolder) {
this.client = client;
this.nodesHolder = nodesHolder;
this.closed = new AtomicBoolean();
}
public void startDelayed() {
execute(5, 300, TimeUnit.SECONDS);
}
@VisibleForTesting
protected void execute(final long initialDelay, final long delay, final TimeUnit unit) {
Delay.delayedExecutor(initialDelay, unit).execute(() -> execute(delay, unit));
}
private void execute(final long nextDelay, final TimeUnit unit) {
if (closed.get()) {
return;
}
LOG.debug("Doing nodes lookup...");
client.getAvailableNodes()
.thenAccept(nodesHolder::setNodes)
.whenComplete((unused, error) -> {
if (closed.get()) {
return;
}
if (error != null) {
LOG.warn("Error on nodes lookup: {}", error.getMessage());
if (LOG.isDebugEnabled()) {
LOG.debug("Error on nodes lookup stack trace:", error);
}
} else {
LOG.debug("Nodes lookup successful");
}
execute(nextDelay, nextDelay, unit);
});
}
@Override
public void close() {
closed.set(true);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy