org.mingle.pear.aop.RequestPerformance.java Source code

Java tutorial

Introduction

Here is the source code for org.mingle.pear.aop.RequestPerformance.java

Source

/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mingle.pear.aop;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.mingle.pear.exception.HttpRequestTimeoutException;
import org.springframework.stereotype.Component;

/**
 * web
 *
 * @author mingle
 */
@Component
@Aspect
public class RequestPerformance {
    private static final Logger logger = LogManager.getLogger(RequestPerformance.class.getName());

    /**
     * A join point is in the web layer if the method is defined
     * in a type in the org.mingle.pear.web package or any sub-ge
     * under that.
     */
    //   @Pointcut("within(org.mingle.pear.web..*)")   // ?JPApersist,service layer
    @Pointcut("within(org.mingle.pear.service.*)")
    public void performance() {
    }

    @Before("performance()")
    public void before() {
        logger.info("aop before");
    }

    @After("performance()")
    public void after() {
        logger.info("aop after");
    }

    @AfterReturning("performance()")
    public void afterReturning() {
        logger.info("aop afterReturning");
    }

    @AfterThrowing("performance()")
    public void afterThrowing() {
        logger.info("aop afterThrowing");
    }

    @Around("performance()")
    public void around(ProceedingJoinPoint joinPoint) {
        try {
            logger.info("aop around");
            long start = System.currentTimeMillis();
            joinPoint.proceed();
            long end = System.currentTimeMillis();

            if ((end = end - start) > (5 * 1000))
                throw new HttpRequestTimeoutException(end);

            logger.info(joinPoint.getSignature() + " took " + end + " milliseconds");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}