Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 2D147200BA5 for ; Wed, 19 Oct 2016 11:59:56 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 2B9E5160AEA; Wed, 19 Oct 2016 09:59:56 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7072F160ADE for ; Wed, 19 Oct 2016 11:59:55 +0200 (CEST) Received: (qmail 31712 invoked by uid 500); 19 Oct 2016 09:59:54 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 31703 invoked by uid 99); 19 Oct 2016 09:59:54 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 19 Oct 2016 09:59:54 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 85E68E0381; Wed, 19 Oct 2016 09:59:54 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-7072] Support for auto-discovering MetricsFeature Date: Wed, 19 Oct 2016 09:59:54 +0000 (UTC) archived-at: Wed, 19 Oct 2016 09:59:56 -0000 Repository: cxf Updated Branches: refs/heads/3.1.x-fixes ae6fbfb0e -> e629ea636 [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/e629ea63 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/e629ea63 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/e629ea63 Branch: refs/heads/3.1.x-fixes Commit: e629ea6368ac788faccd37c469639af2c2d3a6ed Parents: ae6fbfb Author: Sergey Beryozkin Authored: Wed Oct 19 10:57:14 2016 +0100 Committer: Sergey Beryozkin Committed: Wed Oct 19 10:59:37 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/e629ea63/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/e629ea63/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/e629ea63/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; + } + } + } }