We saw how we can do the relaxing Java Metering using Shirt occasion audience in one of our previously content.
Here we are going to see how to use Dropwizard Analytics structure to do the metering of our relaxing source techniques. Dropwizard Analytics is using Shirt activities audience internal to accomplish this. They have offered awesome wrapper and plenty of plug-in to collect the efficiency of each source techniques without much attempt.
There are 3 actions engaged in-order to accomplish this.
Analytics reliance in Expert pom
Sign-up ‘MetricRegistry’ and ‘ConsoleReporter’ in our source configuration
Offer @Timed or @Metered annotation for source methods
Since we are going to use Analytics structure within Shirt (restful Java) structure, the second reliance is needed. If your relax support execution is NOT using Shirt structure, then you can neglect the ‘metrics-jersey2’ reliance. This will bring the essential collections in our program after the synchronize function.
Register ‘MetricRegistry’ and ‘ConsoleReporter’:
Both MetricRegistry and ConsoleReporter implementations are arriving from Analytics structure. They actually deliver the chance to catch the efficiency of our source techniques and release the aggregated lead to system as a study.
public class RestSkolApplication extends ResourceConfig {
private static final Logger logger = LogManager.getLogger(RestSkolApplication.class);
private Set<Class<?>> classes = new HashSet<Class<?>>();
public RestSkolApplication() {
initializeApplication();
}
private void initializeApplication() {
registerListeners(); // Register listeners
}
private void registerListeners() {
final MetricRegistry metricRegistry = new MetricRegistry();
register(new InstrumentedResourceMethodApplicationListener(metricRegistry));
ConsoleReporter.forRegistry(metricRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build()
.start(1, TimeUnit.MINUTES);
logger.info(“Console reporter is enabled successfully!”);
}
}
The system review will review the efficiency metrics for every moment. This period can be configurable. So modify the period centered on your need.
@Timed or @Metered annotation:
The last phase is to add either @Timed or @Metered annotation in the REST source techniques like below:
@Path(“books”)
public class BookResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response getAllBooks() {
System.out.println(“Get all books resource is called”);
final List<Book> books = BookDataStore.getInstance().getBooks();
return Response.ok()
.entity(books)
.build();
}
@Path(“{id}”)
@GET
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response getBook(@PathParam(“id”) String id) {
final Book book = BookDataStore.getInstance().getBook(id);
return Response.ok() // (Response code)
.entity(book) // (response value)
.build();
}
}