Skip to content

Commit

Permalink
Close #13
Browse files Browse the repository at this point in the history
  • Loading branch information
jachinte committed Oct 21, 2019
1 parent ee653e7 commit c8b2441
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import javax.xml.bind.JAXBException;
import org.apache.commons.configuration2.CompositeConfiguration;
Expand All @@ -18,6 +19,8 @@
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A run-time monitor to collect data from a target API.
Expand All @@ -28,6 +31,12 @@
@SuppressWarnings("checkstyle:ClassDataAbstractionCoupling")
public final class HistorianMonitor {

/**
* The logger.
*/
private static final Logger LOGGER =
LoggerFactory.getLogger(HistorianMonitor.class);

/**
* A list of consumers for reporting run-time changes.
*/
Expand All @@ -43,6 +52,11 @@ public final class HistorianMonitor {
*/
private final Scheduler scheduler;

/**
* Whether this monitor is currently collecting data.
*/
private final AtomicBoolean running;

/**
* Default constructor.
* @throws ConfigurationException If there is an error building the
Expand All @@ -52,6 +66,7 @@ public HistorianMonitor() throws ConfigurationException {
this.consumers = new ArrayList<>(1);
this.config = HistorianMonitor.initialize();
this.scheduler = new Scheduler();
this.running = new AtomicBoolean(false);
}

/**
Expand Down Expand Up @@ -157,12 +172,18 @@ private void setupAuthProviders(final Graph<Monitor> graph)
* @param algorithm An instance of the Fork and Collect algorithm
*/
private void collect(final ForkAndCollectAlgorithm algorithm) {
if (this.running.getAndSet(true)) {
HistorianMonitor.LOGGER.info("Skipping scheduled data collection");
return;
}
try {
final JsonNode result = algorithm.data();
this.consumers.forEach(consumer -> consumer.accept(result));
} catch (final UnexpectedResponseCodeException | IOException
| com.rigiresearch.middleware.historian.runtime.ConfigurationException exception) {
throw new IllegalStateException(exception);
} finally {
this.running.set(false);
}
}

Expand Down

0 comments on commit c8b2441

Please sign in to comment.