programming-the-iot / book-exercise-tasks

This repo is for issues / tasks ONLY. All programming and related exercises for each chapter of 'Programming the Internet of Things' are listed here.
Other
11 stars 12 forks source link

PIOT-GDA-02-006: Create module - SystemMemUtilTask #46

Open labbenchstudios opened 4 years ago

labbenchstudios commented 4 years ago

Description

Review the README

Estimated effort may vary greatly

Actions

NOTE: The implementation examples depicted here are only one way to implement the requirements listed. Your own implementation may vary of course.

import java.util.logging.Logger;

import programmingtheiot.common.ConfigConst;

- Add in an override for the inherited template method `getTelemetryValue()`. It will retrieve JVM memory utilization and return the value as a float. Use the `@Override` annotation and be sure to remove the abstract keyword if you're copying / pasting from the base class. You'll have to perform a simple calculation to derive this, as follows:
```java
MemoryUsage memUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
double memUtil = ((double) memUsage.getUsed() / (double) memUsage.getMax()) * 100.0d;

@Override public float getTelemetryValue() { MemoryUsage memUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); double memUsed = (double) memUsage.getUsed(); double memMax = (double) memUsage.getMax();

_Logger.fine("Mem used: " + memUsed + "; Mem Max: " + memMax);

double memUtil = (memUsed / memMax) * 100.0d;

return (float) memUtil;

}



**Estimate**

- Small

**Tests**

- Unit tests (in ./src/test/java/programmingtheiot/part01/unit)
  - Run `./system/SystemMemUtilTaskTest`. The `testGetTelemetryValue()` unit test should pass while logging values between 0.0% and 100.0%.
htayyar commented 1 month ago

in order to use _Logger from BaseSystemUtilTask, it should become protected or public in BaseSystemUtilTask:

protected static final Logger _Logger = Logger.getLogger(BaseSystemUtilTask.class.getName());

labbenchstudios commented 1 month ago

Logging is on a per class / type basis, so each specific type has its own private instance by design.