Adding Metrics AspectJ article code changes (#7387)

* Adding Metrics AspectJ article code changes

* BAEL-3050 - Changes required by the code review in the PR

* BAEL-3050 - Small fixes

* fixed a { in the wrong place
This commit is contained in:
Norberto Ritzmann Júnior 2019-08-19 22:27:36 +02:00 committed by ashleyfrieze
parent 2c7c520a44
commit 92aa2bd437
3 changed files with 75 additions and 0 deletions

View File

@ -86,6 +86,24 @@
<version>${spectator-api.version}</version>
</dependency>
<dependency>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj</artifactId>
<version>${metrics-aspectj.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.astefanutti.metrics.aspectj</groupId>
<artifactId>metrics-aspectj-deps</artifactId>
<version>${metrics-aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
@ -104,6 +122,7 @@
<spectator-api.version>0.57.1</spectator-api.version>
<spring-boot-starter-web.version>2.0.7.RELEASE</spring-boot-starter-web.version>
<assertj-core.version>3.11.1</assertj-core.version>
<metrics-aspectj.version>1.1.0</metrics-aspectj.version>
</properties>
</project>

View File

@ -0,0 +1,34 @@
package com.baeldung.metrics.aspectj;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;
public class MetricsAspectJMain {
private static final MetricRegistry REGISTRY = new MetricRegistry();
public static void main(String args[]) throws InterruptedException {
startReport();
ObjectRunner runner = new ObjectRunner();
for (int i = 0; i < 5; i++) {
runner.run();
}
Thread.sleep(3000L);
}
private static void startReport() {
SharedMetricRegistries.add(ObjectRunner.REGISTRY_NAME, REGISTRY);
ConsoleReporter.forRegistry(REGISTRY)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.outputTo(new PrintStream(System.out))
.build()
.start(3, TimeUnit.SECONDS);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.metrics.aspectj;
import com.codahale.metrics.annotation.Timed;
import io.astefanutti.metrics.aspectj.Metrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Metrics( registry = ObjectRunner.REGISTRY_NAME)
public class ObjectRunner {
private static final Logger logger = LoggerFactory.getLogger(ObjectRunner.class);
public static final String REGISTRY_NAME = "ObjectRunner";
@Timed(name = "timerName")
public void run() {
logger.info("run");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}
}