// Path: src/main/java/org/rhq/wfly/monitor/extension/MonitorLogger.java
// @MessageLogger(projectCode = "<<none>>")
// public interface MonitorLogger extends BasicLogger {
//     /**
//      * A logger with the category {@code org.rhq.wfly.monitor}.
//      */
//     MonitorLogger LOGGER = Logger.getMessageLogger(MonitorLogger.class, "org.rhq.wfly.monitor");
// 
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/ModelControllerClientFactory.java
// public interface ModelControllerClientFactory {
//     ModelControllerClient createClient();
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/SchedulerLogger.java
// @MessageLogger(projectCode = "<<none>>")
// public interface SchedulerLogger extends BasicLogger {
//     /**
//      * A logger with the category {@code org.wildfly.metrics.scheduler}.
//      */
//     SchedulerLogger LOGGER = Logger.getMessageLogger(SchedulerLogger.class, "org.wildfly.metrics.scheduler");
// 
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/diagnose/Diagnostics.java
// public interface Diagnostics {
//     Timer getRequestTimer();
//     Meter getErrorRate();
//     Meter getDelayedRate();
// 
//     Meter getStorageErrorRate();
//     Counter getStorageBufferSize();
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/storage/DataPoint.java
// public final class DataPoint {
//     private Task task;
//     private long timestamp;
//     private double value;
// 
//     public DataPoint(Task task, double value) {
//         this.task = task;
//         this.timestamp = System.currentTimeMillis();
//         this.value = value;
//     }
// 
//     public Task getTask() {
//         return task;
//     }
// 
//     public long getTimestamp() {
//         return timestamp;
//     }
// 
//     public double getValue() {
//         return value;
//     }
// }


import com.codahale.metrics.Timer;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.Property;
import org.rhq.wfly.monitor.extension.MonitorLogger;
import org.wildfly.metrics.scheduler.ModelControllerClientFactory;
import org.wildfly.metrics.scheduler.SchedulerLogger;
import org.wildfly.metrics.scheduler.diagnose.Diagnostics;
import org.wildfly.metrics.scheduler.storage.DataPoint;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.wildfly.metrics.scheduler.polling.Scheduler.State.RUNNING;
import static org.wildfly.metrics.scheduler.polling.Scheduler.State.STOPPED;
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.wildfly.metrics.scheduler.polling;




/**
 * @author Harald Pehl
 * @author Heiko Braun
 */
public class IntervalBasedScheduler extends AbstractScheduler {

    private final ScheduledExecutorService executorService;
    private final List<ScheduledFuture> jobs;
    private final int poolSize;

    private final ModelControllerClientFactory clientFactory;
    private final Diagnostics monitor;

    private ConcurrentLinkedQueue<ModelControllerClient> connectionPool = new ConcurrentLinkedQueue<>();

    public IntervalBasedScheduler(ModelControllerClientFactory clientFactory, Diagnostics monitor, final int poolSize) {
        this.clientFactory = clientFactory;
        this.monitor = monitor;
        this.poolSize = poolSize;

        this.executorService = Executors.newScheduledThreadPool(poolSize, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
// Path: src/main/java/org/rhq/wfly/monitor/extension/MonitorLogger.java
// @MessageLogger(projectCode = "<<none>>")
// public interface MonitorLogger extends BasicLogger {
//     /**
//      * A logger with the category {@code org.rhq.wfly.monitor}.
//      */
//     MonitorLogger LOGGER = Logger.getMessageLogger(MonitorLogger.class, "org.rhq.wfly.monitor");
// 
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/ModelControllerClientFactory.java
// public interface ModelControllerClientFactory {
//     ModelControllerClient createClient();
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/SchedulerLogger.java
// @MessageLogger(projectCode = "<<none>>")
// public interface SchedulerLogger extends BasicLogger {
//     /**
//      * A logger with the category {@code org.wildfly.metrics.scheduler}.
//      */
//     SchedulerLogger LOGGER = Logger.getMessageLogger(SchedulerLogger.class, "org.wildfly.metrics.scheduler");
// 
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/diagnose/Diagnostics.java
// public interface Diagnostics {
//     Timer getRequestTimer();
//     Meter getErrorRate();
//     Meter getDelayedRate();
// 
//     Meter getStorageErrorRate();
//     Counter getStorageBufferSize();
// }
// 
// Path: src/main/java/org/wildfly/metrics/scheduler/storage/DataPoint.java
// public final class DataPoint {
//     private Task task;
//     private long timestamp;
//     private double value;
// 
//     public DataPoint(Task task, double value) {
//         this.task = task;
//         this.timestamp = System.currentTimeMillis();
//         this.value = value;
//     }
// 
//     public Task getTask() {
//         return task;
//     }
// 
//     public long getTimestamp() {
//         return timestamp;
//     }
// 
//     public double getValue() {
//         return value;
//     }
// }


// Path: src/main/java/org/wildfly/metrics/scheduler/polling/IntervalBasedScheduler.java
import com.codahale.metrics.Timer;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.Property;
import org.rhq.wfly.monitor.extension.MonitorLogger;
import org.wildfly.metrics.scheduler.ModelControllerClientFactory;
import org.wildfly.metrics.scheduler.SchedulerLogger;
import org.wildfly.metrics.scheduler.diagnose.Diagnostics;
import org.wildfly.metrics.scheduler.storage.DataPoint;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.wildfly.metrics.scheduler.polling.Scheduler.State.RUNNING;
import static org.wildfly.metrics.scheduler.polling.Scheduler.State.STOPPED;

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.wildfly.metrics.scheduler.polling;




/**
 * @author Harald Pehl
 * @author Heiko Braun
 */
public class IntervalBasedScheduler extends AbstractScheduler {

    private final ScheduledExecutorService executorService;
    private final List<ScheduledFuture> jobs;
    private final int poolSize;

    private final ModelControllerClientFactory clientFactory;
    private final Diagnostics monitor;

    private ConcurrentLinkedQueue<ModelControllerClient> connectionPool = new ConcurrentLinkedQueue<>();

    public IntervalBasedScheduler(ModelControllerClientFactory clientFactory, Diagnostics monitor, final int poolSize) {
        this.clientFactory = clientFactory;
        this.monitor = monitor;
        this.poolSize = poolSize;

        this.executorService = Executors.newScheduledThreadPool(poolSize, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                SchedulerLogger.LOGGER.debug("Creating new executor thread");