spring - perf4j @Profiled 注释不起作用

标签 spring log4j seam spring-aop perf4j

我已经通过 perf4J 网站的以下链接进行了相同的操作:http://perf4j.codehaus.org/devguide.html#Using_Spring_AOP_to_Integrate_Timing_Aspects

在我的 spring.xml 中添加了以下内容。

<aop:aspectj-autoproxy/>
<bean id="timingAspect" class="org.perf4j.log4j.aop.TimingAspect"/>
<bean id="wscClientBase" class="com.xyz.csa.core.common.WscClientBase"/>

在 WscClientBase 类中,我有以下带有 @Profiled 注释的方法。

@Profiled(tag = "SOAPCALLTEST")
public Object sendMessage(Object message) {
    String msg = message.toString();
    if (msg.indexOf(' ') > 1) {
        msg = msg.substring(1, msg.indexOf(' '));
    }
    try {
        Object ret = marshalSendAndReceive(message);
        return ret;
    } catch (RuntimeException ex) {
        throw ex;
    }
}

我在应用程序日志中没有看到 perf4j TimingLogger 语句。但是,如果我如下所示强制使用它(没有注释),我会成功看到日志语句。

public Object sendMessage(Object message) {
    String msg = message.toString();
    if (msg.indexOf(' ') > 1) {
        msg = msg.substring(1, msg.indexOf(' '));
    }
    StopWatch stopWatch = new Slf4JStopWatch();
    try {
        Object ret = marshalSendAndReceive(message);
        stopWatch.stop("PERF_SUCCESS_TAG", msg);
        return ret;
    } catch (RuntimeException ex) {
        stopWatch.stop("PERF_FAILURE_TAG", msg);
        throw ex;
    }
}

我错过了什么吗?

最佳答案

Perf4j

这是一个应用程序的性能分析和检查插件。可以使用spring AOP与spring集成。它创建一个日志文件,提供给解析器来分析和生成相关信息。默认可以提供平均值、均值、标准差。 欲了解更多一般信息,请查看http://perf4j.codehaus.org/index.html

如何设置 Perf4j。 对于正常设置,您只需添加 perf4j jar 并为您想要监控的每个代码片段创建 StopWatch 实例。

StopWatch stopWatch= new StopWatch(“snipletTagName”)
…
//{your code sniplet}
…
stopwatch.stop();

这将创建 perf4j 监视器,您将在控制台上获取日志信息。

本文档的主要目的是通过设置了解 perf4j 与 spring 的集成。

1.添加以下所有 Jar 文件。

   1.perf4j-0.9.16-slf4jonly.jar
   2.aspectjweaver-1.6.12.jar
   3.aopalliance-1.0.jar
   4.commons-logging-1.1.1.jar
   5.logback-classic-1.0.7.jar
   6.logback-core-1.0.7.jar
   7.slf4j-api-1.7.1.jar
   8.perf4j-0.9.16.jar
   9.aspectjrt-1.6.1.jar
   10.commons-jexl-1.1.jar
   11.asm-1.5.3.jar
   12.cglib-2.1_3.jar

确保您的类路径中包含所有这些 jar 以及 spring 库。

2.创建您自己的 logback.xml,它将被 perf4j 隐式使用 logback.xml 的内容为

<configuration>
    <appender name="perf4jFileAppender"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>logs/perf4j.log</File>
        <encoder>
            <Pattern>%date %-5level [%thread] %logger{36} [%file:%line] %msg%n
            </Pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>logs/perf4j.%d{yyyy-MM-dd}.log</FileNamePattern>
        </rollingPolicy>
    </appender>

    <appender name="CoalescingStatistics"
        class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
        <param name="TimeSlice" value="1" />        
        <appender-ref ref="perf4jFileAppender" />       
    </appender>

    <appender name="RootConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%date %-5level [%thread] %logger{36} [%file:%line] %msg%n
            </pattern>
        </layout>
    </appender>

    <!-- Loggers -->
    <!-- The Perf4J logger. Note that org.perf4j.TimingLogger is the value of 
        the org.perf4j.StopWatch.DEFAULT_LOGGER_NAME constant. Also, note that additivity 
        is set to false, which is usually what is desired - this means that timing 
        statements will only be sent to this logger and NOT to upstream loggers. -->
    <logger name="org.perf4j.TimingLogger" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="CoalescingStatistics" />
        <appender-ref ref="perf4jFileAppender" />
        <appender-ref ref="RootConsoleAppender" />
    </logger>
</configuration>

3.在你的spring配置文件中,你需要添加aspectj标签来启用perf4j的@Profiled注释。

(Note: What is @Profiled annotation?: you will add this tag to all the methods in all the classes that are called from spring instance or use dependency injection. The object basically should be spring context registered and the method should be invoked by the object that are registered in spring context. I wasted one day thinking why my method was not logged then I realized that the object I tested was not part of spring context.

好的,您需要添加到spring配置xml的代码是

<!-- this is my spring-context.xml -->
<beans>

    <aop:aspectj-autoproxy>
        <aop:include name="timingAspect" />
    </aop:aspectj-autoproxy>

    <bean id="timingAspect" class="org.perf4j.slf4j.aop.TimingAspect" />

<!-- this is the class that will be registered with the spring and now we can get this class and call the method that we need to monitor-->
    <bean class="com.perf4jexample.Test" />


</beans>

4.创建将实现@Profiled注释的Test类。

public class Test {

    private String testVal;

    public Test() {
        // TODO Auto-generated constructor stub
    }

    @Profiled
    public void testing() {
        System.out.println("testt" );
    }

    public String getTestVal() {
        return testVal;
    }

    public void setTestVal(String testVal) {
        this.testVal = testVal;
    }
}

5.现在你已经设置好了一切,剩下的就是测试类,它将启动 spring 上下文并加载 perf4j。

public class Test(){

public static void main(){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "spring-context.xml");

        context.start();

        Test bean = context.getBean(Test.class);
        bean.testing();
}

我希望通过遵循这些设置,您应该能够通过 perf4j 控制台附加程序在控制台上显示一行。

Perf4j日志上的监控命令:

要生成性能统计信息,请在记录器路径上执行

java -jar perf4j-0.9.16.jar myLogger.log

用于生成图表

java -jar perf4j-0.9.16.jar --graph perfGraphs.out myLogger.log

我希望本教程可以帮助您将 Spring、perf4j、logback 与 Profiled 注解集成。

关于spring - perf4j @Profiled 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5414715/

相关文章:

java - 避免由于 Log4j 中的默认行为而多次记录同一消息

java - 使用 JSF 的 Seam 与使用 GWT 的 Seam

jsf - Seam/Spring WebFlow 应用程序中的 StackOverflowError

java - Seam 3 教程,Spring 3 + Seam 3 集成

java - CXF JAX-RS 导致 BusException

java - 结合 Spring-Data for MongoDB 和 ElasticSearch

java - Log4j 2.5 PatternLayout Nano 时间戳不起作用

java - 无法确定缺少类型的已实现接口(interface)

java - SpringIntegration 在不处理的情况下从队列中删除消息

java - log4j2 将参数传递给自定义附加程序