com.greenline.guahao.web.module.common.interceptor.MethodExecuteTimeInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.guahao.web.module.common.interceptor.MethodExecuteTimeInterceptor.java

Source

/*
 * Project: vip-portal-web-home
 * 
 * File Created at 2014-5-28
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.guahao.web.module.common.interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Type MethodExecuteTime
 * @Desc 
 * @author alex
 * @date 2014-5-28
 * @Version V1.0
 */
public class MethodExecuteTimeInterceptor implements MethodInterceptor {
    private static final Log logger = LogFactory.getLog(MethodExecuteTimeInterceptor.class);

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept
     * .MethodInvocation)
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        //  commons-lang ?? StopWatch Spring ?? StopWatch
        StopWatch clock = new StopWatch();
        clock.start(); // 
        Object result = invocation.proceed();
        clock.stop(); // ?

        // ????
        Class<?>[] params = invocation.getMethod().getParameterTypes();
        String[] simpleParams = new String[params.length];
        for (int i = 0; i < params.length; i++) {
            simpleParams[i] = params[i].getSimpleName();
        }
        logger.info(":" + clock.getTime() + " ms [" + invocation.getThis().getClass().getName()
                + "." + invocation.getMethod().getName() + "(" + StringUtils.join(simpleParams, ",") + ")] ");
        return result;
    }

}