All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metricssampler.daemon.Daemon Maven / Gradle / Ivy

The newest version!
package org.metricssampler.daemon;

import org.metricssampler.config.SamplerConfig;
import org.metricssampler.resources.SamplerTask;
import org.metricssampler.resources.SamplerThreadPool;
import org.metricssampler.sampler.Sampler;
import org.metricssampler.service.Bootstrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class Daemon {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private final Bootstrapper bootstrapper;

	private Thread controllerThread;
	private final Map tasks = new HashMap<>();

	public Daemon(final Bootstrapper bootstrapper) {
		this.bootstrapper = bootstrapper;
	}

	/**
	 * The order of operations is significant here:
	 * 
    *
  1. Setup the thread pool
  2. *
  3. Create the controller which makes sure there is no other process running on the * local machine because it would otherwise fail to bind the server socket
  4. *
  5. Schedule the samplers
  6. *
  7. Startup the controller thread. From this point it can really process requests
  8. *
*/ public void start() { createController(); scheduleSamplers(); controllerThread.start(); } private void createController() { try { final Runnable controller = new DefaultTCPController(bootstrapper, tasks, bootstrapper.getSharedResources()); controllerThread = new Thread(controller); } catch (final IllegalStateException e) { logger.error(e.getMessage(), e); System.exit(1); } } private void scheduleSamplers() { for (final Sampler sampler : bootstrapper.getSamplers()) { final SamplerConfig config = sampler.getConfig(); logger.info("Scheduling {} at fixed rate of {} seconds", sampler, config.getInterval()); final SamplerThreadPool threadPool = (SamplerThreadPool) bootstrapper.getSharedResource(sampler.getConfig().getPool()); final SamplerTask task = threadPool.schedule(sampler); tasks.put(config.getName(), task); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy