VOOZH about

URL: https://www.javacodegeeks.com/2015/07/how-does-flexypool-support-the-dropwizard-metrics-package-renaming.html

⇱ How does FlexyPool support the Dropwizard Metrics package renaming - Java Code Geeks


Introduction

FlexyPool relies heavily on Dropwizard (previously Codahale) Metrics for monitoring the connection pool usage. Being integrated into Dropwizard, the package name was bound to be renamed.

So instead of com.codahale.metrics the 4.0.0 release will use the io.dropwizard.metrics package name.

The challenge

Apart from the obvious backward incompatibility, the most challenging aspect of this change is that the Maven dependency will only see a version incrementation. This means that you won’t be able to include both versions in the same Maven module, because the groupId and the artifactId will not change between the 3.x.x and the 4.x.x version change.

<dependency>
 <groupId>io.dropwizard.metrics</groupId>
 <artifactId>metrics-core</artifactId>
 <version>${codahale.metrics.version}</version>
</dependency>

<dependency>
 <groupId>io.dropwizard.metrics</groupId>
 <artifactId>metrics-core</artifactId>
 <version>${dropwizard.metrics.version}</version>
</dependency>

This change is manageable in an end-user application as you only have to migrate from one version to the other. An open source framework built on top of Dropwizard Metrics is much more difficult to refactor as you need to support two incompatible versions of the same library. After all, you don’t want to force your clients to migrate to a certain Metrics dependency.

Luckily, FlexyPool has had its own Metrics abstraction layer from the very beginning. Insulating a framework from external dependencies is a safe measure, allowing you to swap dependencies without much effort.

To support both Codahale and Dropwizard package names, FlexyPool metrics are build like this:

πŸ‘ flexypoolmetricscodahaledropwizard

Because those classes cannot reside in one jar, there are three modules hosting this hierarchy:

  • flexy-pool-core: defines the FlexyPool Metrics abstraction
  • flexy-codahale-metrics: implements the FlexyPool metrics abstraction on top of Codahale Matrics
  • flexy-dropwizard-metrics: implements the FlexyPool metrics abstraction on top of Dropwizard Matrics

Each MetricsFactory is registered as a Service Provider:

public class CodahaleMetricsFactoryService 
 implements MetricsFactoryService {

 public static final String METRICS_CLASS_NAME = 
 "com.codahale.metrics.Metric";

 @Override
 public MetricsFactory load() {
 return ClassLoaderUtils
 .findClass(METRICS_CLASS_NAME) ? 
 CodahaleMetrics.FACTORY : null;
 }
}

public class DropwizardMetricsFactoryService 
 implements MetricsFactoryService {

 public static final String METRICS_CLASS_NAME = 
 "io.dropwizard.metrics.Metric";

 @Override
 public MetricsFactory load() {
 return ClassLoaderUtils
 .findClass(METRICS_CLASS_NAME) ? 
 DropwizardMetrics.FACTORY : null;
 }
}

and the services are resolved at runtime:

private ServiceLoader<MetricsFactoryService> 
 serviceLoader = ServiceLoader.load(
 MetricsFactoryService.class);

public MetricsFactory resolve() {
 for(MetricsFactoryService service : serviceLoader) {
 MetricsFactory metricsFactory = service.load();
 if(metricsFactory != null) {
 return metricsFactory;
 }
 }
 throw new IllegalStateException(
 "No MetricsFactory could be loaded!"
 );
}

Conclusion

This way FlexyPool can use both Metrics implementations and the decision is taken dynamically based on the currently available library. The Dropwizard metrics 4.0.0 is not yet released, but FlexyPool is ready for the upcoming changes.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Vlad Mihalcea
Vlad Mihalcea
July 6th, 2015Last Updated: July 3rd, 2015
0 105 2 minutes read

Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz