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

es.codeurjc.squirrel.drey.CloudWatchModule Maven / Gradle / Ivy

Go to download

Java framework aimed to support distributed execution of algorithms thanks to Hazelcast technology

There is a newer version: 2.0.0
Show newest version
package es.codeurjc.squirrel.drey;

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.MetricDatum;
import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest;
import com.amazonaws.services.cloudwatch.model.PutMetricDataResult;
import com.amazonaws.services.cloudwatch.model.StandardUnit;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class CloudWatchModule {
	
	final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.standard().withRegion("eu-west-1").build();
	HazelcastInstance hcClient;
	IMap queues;
	Thread publishMetricsThread;
	
	public CloudWatchModule(HazelcastInstance hcClient, IMap queues) {
		this.hcClient = hcClient;
		this.queues = queues;
		this.startPublishMetricsThread();
	}
	
	public void publishMetrics(Double nAlgorithms, Double nInstances) {

		MetricDatum datum = new MetricDatum()
		    .withMetricName("TASKS_QUEUED")
		    .withUnit(StandardUnit.None)
		    .withValue(nAlgorithms/nInstances);

		PutMetricDataRequest request = new PutMetricDataRequest()
		    .withNamespace("HAZELCAST_METRIC")
		    .withMetricData(datum);

		//PutMetricDataResult response = cw.putMetricData(request);
		
		System.out.println("METRICS PUSHED: " + nAlgorithms/nInstances);
	}
	
	public void startPublishMetricsThread() {
		this.publishMetricsThread = new Thread(() -> {
			while (true) {
				this.publishMetrics((double) this.queues.size(), (double) this.hcClient.getCluster().getMembers().size());
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		this.publishMetricsThread.start();
	}
	
	public void stopPublishMetricsThread() {
		this.publishMetricsThread.interrupt();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy