尚未应用带有 AspectJ 抛出建议的删除向导指标

Drop wizard metrics with AspectJ throws advice has not been applied

我正在尝试在一个简单的 java 项目中设置 AspectJ for Metrics。 我在 pom.xml 中添加了所需的依赖项。当我做 mvn compile, 我收到以下警告。它说,建议不适用。我哪里错了

[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[WARNING] advice defined in io.astefanutti.metrics.aspectj.TimedAspect has not been applied [Xlint:adviceDidNotMatch]
    /root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/TimedAspect.class:26

[WARNING] advice defined in io.astefanutti.metrics.aspectj.ExceptionMeteredAspect has not been applied [Xlint:adviceDidNotMatch]
    /root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/ExceptionMeteredAspect.class:26

[WARNING] advice defined in io.astefanutti.metrics.aspectj.MeteredAspect has not been applied [Xlint:adviceDidNotMatch]
    /root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/MeteredAspect.class:26

[WARNING] advice defined in io.astefanutti.metrics.aspectj.ExceptionMeteredStaticAspect has not been applied [Xlint:adviceDidNotMatch]
    /root/.m2/repository/io/astefanutti/metrics/aspectj/metrics-aspectj/1.1.0/metrics-aspectj-1.1.0.jar!io/astefanutti/metrics/aspectj/ExceptionMeteredStaticAspect.class:26

这是我的pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.dropwizard.metrics</groupId>
      <artifactId>metrics-core</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>io.dropwizard.metrics</groupId>
      <artifactId>metrics-graphite</artifactId>
      <version>3.1.2</version>
    </dependency>
    <dependency>
      <groupId>io.dropwizard.metrics</groupId>
      <artifactId>metrics-annotation</artifactId>
      <version>3.1.2</version>
    </dependency>
    <dependency>
      <groupId>io.astefanutti.metrics.aspectj</groupId>
      <artifactId>metrics-aspectj</artifactId>
      <version>1.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.7</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjtools</artifactId>
      <version>1.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.8</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.8</version>
        <configuration>
          <complianceLevel>1.6</complianceLevel>
          <source>1.6</source>
          <target>1.6</target>
          <aspectLibraries>
            <aspectLibrary>
              <groupId>io.astefanutti.metrics.aspectj</groupId>
              <artifactId>metrics-aspectj</artifactId>
            </aspectLibrary>
          </aspectLibraries>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>dropwizard.App</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

这是我的代码:

package dropwizard;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import io.astefanutti.metrics.aspectj.Metrics;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 *
 */
@Metrics(registry = "graphiteregistry2")
public class App 
{
    static final MetricRegistry registry = new MetricRegistry();
    public static void main(String args[]) {
        startReport();
        test();
        wait5Seconds();
    }

    static void startReport() {

        final Graphite graphite = new Graphite(new InetSocketAddress("127.0.0.1", 2003));

        final GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)
                .prefixedWith("test8.example.com")
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .filter(MetricFilter.ALL)
                .build(graphite);
        reporter.start(1, TimeUnit.SECONDS);

        ConsoleReporter reporter1 = ConsoleReporter.forRegistry(registry)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter1.start(3, TimeUnit.SECONDS);

        SharedMetricRegistries.add("graphiteregistry2", registry);

    }

    static void wait5Seconds() {
        try {
            Thread.sleep(5*1000);
        }
        catch(InterruptedException e) {}
    }

    @Timed(name = "test8method")
    @Metered(name = "methodmeter")
    static void test() {

        //Timer.Context time = responses.timer("test8.update").time();
        System.out.println("inside test");
        try {

            for(int i=0;i<10000;i++){}

        }
        finally {
            //time.stop();
        }
    }

}

警告仅表示在工具库中定义了某些切入点不会触发,因此未应用的切入点。查看您的代码,您确实没有使用它们或在不应应用它们的地方使用它们,因此警告只是描述了这种情况。详细:

  • @Metrics:已应用,无警告。
  • @Timed:未应用,因为在静态方法上使用,因此出现警告。如果您查看 aspect source code,您会发现切入点仅针对非静态方法:execution(@Timed !static * (@Metrics Profiled+).*(..))
  • @Metered:未应用,因为在静态方法上使用,因此出现警告。如果您查看 aspect source code,您会发现切入点仅针对非静态方法:execution(@Metered !static * (@Metrics Profiled+).*(..))

从您的 test() 方法中删除 static 并查看重新编译时会发生什么。