Repository: cxf Updated Branches: refs/heads/master f201abe20 -> 93462d66a
[CXF-7072] Support for auto-discovering MetricsFeature Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/93462d66 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/93462d66 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/93462d66 Branch: refs/heads/master Commit: 93462d66a1e706ec028f5ebbaf55d619d02fae62 Parents: f201abe Author: Sergey Beryozkin <[email protected]> Authored: Wed Oct 19 10:57:14 2016 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Wed Oct 19 10:57:14 2016 +0100 ---------------------------------------------------------------------- .../rs/service/SampleRestApplication.java | 15 ++------- .../src/main/resources/application.yml | 2 +- .../org/apache/cxf/metrics/MetricsFeature.java | 33 ++++++++++++++++++-- 3 files changed, 35 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/93462d66/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/java/sample/rs/service/SampleRestApplication.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/java/sample/rs/service/SampleRestApplication.java b/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/java/sample/rs/service/SampleRestApplication.java index e1d8ffb..17f6fb0 100644 --- a/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/java/sample/rs/service/SampleRestApplication.java +++ b/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/java/sample/rs/service/SampleRestApplication.java @@ -17,18 +17,14 @@ * under the License. */ package sample.rs.service; -import org.apache.cxf.Bus; -import org.apache.cxf.feature.Feature; -import org.apache.cxf.metrics.MetricsFeature; -import org.apache.cxf.metrics.codahale.CodahaleMetricsProvider; +import com.codahale.metrics.JmxReporter; +import com.codahale.metrics.MetricRegistry; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; -import com.codahale.metrics.JmxReporter; -import com.codahale.metrics.MetricRegistry; - @SpringBootApplication @EnableEurekaClient public class SampleRestApplication { @@ -42,11 +38,6 @@ public class SampleRestApplication { return JmxReporter.forRegistry(metricRegistry).build(); } - @Bean - public Feature metricsFeature(Bus bus){ - return new MetricsFeature(new CodahaleMetricsProvider(bus)); - } - public static void main(String[] args) { SpringApplication.run(SampleRestApplication.class, args); } http://git-wip-us.apache.org/repos/asf/cxf/blob/93462d66/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/resources/application.yml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/resources/application.yml b/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/resources/application.yml index 9604995..ee5a4fb 100644 --- a/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/resources/application.yml +++ b/distribution/src/main/release/samples/jax_rs/spring_boot_scan/application/src/main/resources/application.yml @@ -8,7 +8,7 @@ cxf: service-list-path: /info jaxrs: component-scan: true - classes-scan-packages: org.apache.cxf.jaxrs.swagger + classes-scan-packages: org.apache.cxf.jaxrs.swagger,org.apache.cxf.metrics eureka: client: http://git-wip-us.apache.org/repos/asf/cxf/blob/93462d66/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java ---------------------------------------------------------------------- diff --git a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java index 1d0b57d..45719a9 100644 --- a/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java +++ b/rt/features/metrics/src/main/java/org/apache/cxf/metrics/MetricsFeature.java @@ -19,10 +19,15 @@ package org.apache.cxf.metrics; +import java.lang.reflect.Constructor; +import java.util.Collection; + import org.apache.cxf.Bus; import org.apache.cxf.annotations.Provider; import org.apache.cxf.annotations.Provider.Type; +import org.apache.cxf.common.classloader.ClassLoaderUtils; import org.apache.cxf.common.injection.NoJSR250Annotations; +import org.apache.cxf.configuration.ConfiguredBeanLocator; import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.endpoint.Server; @@ -40,9 +45,9 @@ import org.apache.cxf.metrics.interceptors.MetricsMessageOutInterceptor; * */ @NoJSR250Annotations -@Provider(value = Type.Feature) +@Provider(Type.Feature) public class MetricsFeature extends AbstractFeature { - final MetricsProvider[] providers; + MetricsProvider[] providers; public MetricsFeature() { this.providers = null; @@ -56,6 +61,7 @@ public class MetricsFeature extends AbstractFeature { @Override public void initialize(Server server, Bus bus) { + createDefaultProvidersIfNeeded(bus); //can optimize for server case and just put interceptors it needs Endpoint provider = server.getEndpoint(); MetricsMessageOutInterceptor out = new MetricsMessageOutInterceptor(providers); @@ -73,6 +79,7 @@ public class MetricsFeature extends AbstractFeature { @Override public void initialize(Client client, Bus bus) { + createDefaultProvidersIfNeeded(bus); //can optimize for client case and just put interceptors it needs MetricsMessageOutInterceptor out = new MetricsMessageOutInterceptor(providers); CountingOutInterceptor countingOut = new CountingOutInterceptor(); @@ -88,6 +95,7 @@ public class MetricsFeature extends AbstractFeature { @Override protected void initializeProvider(InterceptorProvider provider, Bus bus) { + createDefaultProvidersIfNeeded(bus); //if feature is added to the bus, we need to add all the interceptors MetricsMessageOutInterceptor out = new MetricsMessageOutInterceptor(providers); CountingOutInterceptor countingOut = new CountingOutInterceptor(); @@ -105,4 +113,25 @@ public class MetricsFeature extends AbstractFeature { provider.getOutFaultInterceptors().add(countingOut); provider.getOutFaultInterceptors().add(out); } + private void createDefaultProvidersIfNeeded(Bus bus) { + if (providers == null) { + ConfiguredBeanLocator b = bus.getExtension(ConfiguredBeanLocator.class); + if (b != null) { + Collection<?> coll = b.getBeansOfType(MetricsProvider.class); + if (coll != null) { + providers = coll.toArray(new MetricsProvider[]{}); + } + } + } + if (providers == null) { + try { + Class<?> cls = ClassLoaderUtils.loadClass("org.apache.cxf.metrics.codahale.CodahaleMetricsProvider", + MetricsFeature.class); + Constructor<?> c = cls.getConstructor(Bus.class); + providers = new MetricsProvider[] {(MetricsProvider)c.newInstance(bus)}; + } catch (Throwable t) { + // ignore; + } + } + } }
