List of usage examples for org.aspectj.lang ProceedingJoinPoint getSignature
Signature getSignature();
getStaticPart().getSignature()
returns the same object From source file:org.esupportail.commons.aop.AopUtils.java
License:Apache License
/** * @param joinPoint/*from www . j a v a2 s . co m*/ * @return The log signature */ public static String getLogSignature(final ProceedingJoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); String signature = joinPoint.getSignature().toLongString(); String[] signatureParts = signature.split("\\("); String[] pathParts = signatureParts[0].split("\\."); String methodName = pathParts[pathParts.length - 1]; String className = pathParts[pathParts.length - 2]; String str = className + "." + methodName + "("; String separator = ""; for (int i = 0; i < args.length; i++) { str += separator; Object o = args[i]; if (o == null) { str += "null"; } else { String[] nameParts = o.getClass().getSimpleName().split("\\."); String argClassName = nameParts[nameParts.length - 1]; if (o instanceof String || o instanceof Number || o instanceof Boolean) { str += argClassName + "[" + o + "]"; } else { str += argClassName + "#" + o.hashCode(); } } separator = ", "; } str += ")"; return str; }
From source file:org.esupportail.commons.aop.monitor.MonitoringMethodCall.java
License:Apache License
/** * Constructor.// w w w .j a v a 2 s . co m * @param joinPoint * @param time */ public MonitoringMethodCall(final ProceedingJoinPoint joinPoint, final long time) { super(); this.className = joinPoint.getSignature().getDeclaringTypeName(); this.methodName = joinPoint.getSignature().getName(); this.fullString = AopUtils.getLogSignature(joinPoint); this.times = new ArrayList<Long>(); addTime(time); }
From source file:org.eurekastreams.server.aop.PerformanceTimer.java
License:Apache License
/** * Method for logging timing data./* w ww . j a v a2 s .c o m*/ * * @param call * {@link ProceedingJoinPoint} * @return result of wrapped method. * @throws Throwable * on error. */ public Object profile(final ProceedingJoinPoint call) throws Throwable { StopWatch clock = null; // get the perf log for target object. Log log = LogFactory.getLog("perf.timer." + call.getTarget().getClass().getCanonicalName()); try { if (log.isInfoEnabled()) { clock = new StopWatch(); clock.start(call.toShortString()); } return call.proceed(); } finally { if (log.isInfoEnabled() && clock != null) { clock.stop(); Object[] args = call.getArgs(); StringBuffer params = new StringBuffer(); for (Object obj : args) { params.append("Param: " + ((obj == null) ? "null" : obj.toString()) + "\n\t"); } log.info(clock.getTotalTimeMillis() + " (ms) - " + call.getTarget().getClass().getSimpleName() + "." + call.getSignature().toShortString() + "\n\t" + params.toString()); } } }
From source file:org.finra.dm.core.StopWatchAdvice.java
License:Apache License
/** * Logs the time it takes to execute the method at the join point if the class or method isn't annotated with SuppressLogging and if the log level is set to * info.//from w w w. j a va 2s . c o m * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ @SuppressWarnings("rawtypes") public static Object logMethodTime(ProceedingJoinPoint pjp) throws Throwable { // Get the target class being called. Class targetClass = pjp.getTarget().getClass(); // Get the target method being called. MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); Method targetMethod = targetMethodSignature.getMethod(); if (targetMethod.getDeclaringClass().isInterface()) { // Get the underlying implementation if we are given an interface. targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), targetMethod.getParameterTypes()); } // Only keep a stop watch if the class and method aren't suppressing logging and the log level is info. if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null) && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null) && (LOGGER.isInfoEnabled())) { // Start the stop watch. StopWatch stopWatch = new StopWatch(); stopWatch.start(); // Proceed to the join point (i.e. call the method and let it return). Object returnValue = pjp.proceed(); // Log the duration. LOGGER.info("Method " + targetClass.getName() + "." + targetMethodSignature.getName() + " took " + DmDateUtils.formatDuration(stopWatch.getTime(), true) + "."); // Return the method return value. return returnValue; } else { // Invoke the method normally. return pjp.proceed(); } }
From source file:org.finra.dm.dao.helper.AwsExceptionRetryAdvice.java
License:Apache License
/** * Invokes the method, catches following various exceptions and retries as needed: 1. Throttling exception. 2. 5xx exception. 3. Error codes defined in * configuration to be retried.//from w w w . j a v a 2 s. co m * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ public Object retryOnException(ProceedingJoinPoint pjp) throws Throwable { // Get the method name being invoked. Class<?> targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); String methodName = targetClass.getName() + "." + targetMethodSignature.getName(); // Get the max delay in seconds. long maxTotalDelay = getMaxRetryDelaySecs() * 1000L; // Initialize a retry count to know the number of times we have retried calling the method. int retryCount = 0; // Get the minimum and maximum retry delay. long minAwsDelay = dmStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MIN_RETRY_DELAY_SECS) * 1000L; long maxAwsDelay = dmStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MAX_RETRY_DELAY_SECS) * 1000L; StopWatch totalElapsedTimeStopWatch = new StopWatch(); totalElapsedTimeStopWatch.start(); // Loop indefinitely. We will exit the loop if the method returns normally or a non-retryable exception is thrown. // If a retryable exception is thrown, the loop will exit after we have exceeded the maximum retry delay seconds. while (true) { try { // Proceed to the join point (i.e. call the method and let it return normally). return pjp.proceed(); } catch (AmazonServiceException ase) { // Retry if: // 1) Is throttling exception. // 2) Is 5xx exception. // 3) Is an error to be re-tried on as defined in configuration. if (RetryUtils.isThrottlingException(ase) || is5xxException(ase) || isRetryableException(ase)) { long totalElapsedTime = totalElapsedTimeStopWatch.getTime(); // It's a retryable exception. Check if we've retried for enough time. if (totalElapsedTime >= maxTotalDelay) { // We've retried for enough time so re-throw the original exception. LOGGER.warn("An exception occurred while calling " + methodName + ". The method has been retried for " + (totalElapsedTime / 1000.0f) + " second(s) and will not be retried anymore since it would exceed the maximum retry delay." + " The exception will now be thrown."); throw ase; } // Get the next delay. long nextSleepDelay = Math.min(((long) Math.pow(2, retryCount) * minAwsDelay), maxAwsDelay); long timeLeftToRetry = maxTotalDelay - totalElapsedTime; // If time left to try is less than next sleep delay, then use time left as next sleep delay to be retried the last time. if (timeLeftToRetry < nextSleepDelay) { nextSleepDelay = timeLeftToRetry; } // Log a warning so we're aware that we are retrying. LOGGER.warn("A retryable exception occurred while calling " + methodName + ". " + (totalElapsedTime / 1000.0f) + " second(s) have elapsed since initial exception and maximum retry delay is " + (maxTotalDelay / 1000.0f) + " second(s). Will retry in " + (nextSleepDelay / 1000.0f) + " second(s)."); // We can retry again so increment a counter to keep track of the number of times we retried and recalculate the total elapsed time. retryCount++; // Sleep for the next sleep delay. dmThreadHelper.sleep(nextSleepDelay); } else { // It's not a retryable exception (i.e. some other type of service exception) so just re-throw it. throw ase; } } } }
From source file:org.finra.dm.service.helper.CheckAllowedMethodAdvice.java
License:Apache License
/** * Checks whether the requested operation is permitted. * * @param pjp the join point.//from w ww .j a va 2 s . c o m * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ @SuppressWarnings("rawtypes") public Object checkNotAllowedMethods(ProceedingJoinPoint pjp) throws Throwable { // Get the method name being invoked. Class targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); String methodName = targetClass.getName() + "." + targetMethodSignature.getName(); dmDaoHelper.checkNotAllowedMethod(methodName); return pjp.proceed(); }
From source file:org.finra.herd.core.StopWatchAdvice.java
License:Apache License
/** * Logs the time it takes to execute the method at the join point if the class or method isn't annotated with SuppressLogging and if the log level is set to * info.//www . j a v a 2s . co m * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ @SuppressWarnings("rawtypes") public static Object logMethodTime(ProceedingJoinPoint pjp) throws Throwable { // Get the target class being called. Class targetClass = pjp.getTarget().getClass(); // Get the target method being called. MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); Method targetMethod = targetMethodSignature.getMethod(); if (targetMethod.getDeclaringClass().isInterface()) { // Get the underlying implementation if we are given an interface. targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), targetMethod.getParameterTypes()); } // Only keep a stop watch if the class and method aren't suppressing logging and the log level is info. if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null) && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null) && (LOGGER.isInfoEnabled())) { // Start the stop watch. StopWatch stopWatch = new StopWatch(); stopWatch.start(); // Proceed to the join point (i.e. call the method and let it return). Object returnValue = pjp.proceed(); // Log the duration. LOGGER.info("Method " + targetClass.getName() + "." + targetMethodSignature.getName() + " took " + HerdDateUtils.formatDuration(stopWatch.getTime(), true) + "."); // Return the method return value. return returnValue; } else { // Invoke the method normally. return pjp.proceed(); } }
From source file:org.finra.herd.dao.helper.AwsExceptionRetryAdvice.java
License:Apache License
/** * Invokes the method, catches following various exceptions and retries as needed: 1. Throttling exception. 2. 5xx exception. 3. Error codes defined in * configuration to be retried./*ww w. ja v a 2 s . com*/ * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ public Object retryOnException(ProceedingJoinPoint pjp) throws Throwable { // Get the method name being invoked. Class<?> targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); String methodName = targetClass.getName() + "." + targetMethodSignature.getName(); // Get the max delay in seconds. long maxTotalDelay = getMaxRetryDelaySecs() * 1000L; // Initialize a retry count to know the number of times we have retried calling the method. int retryCount = 0; // Get the minimum and maximum retry delay. long minAwsDelay = herdStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MIN_RETRY_DELAY_SECS) * 1000L; long maxAwsDelay = herdStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MAX_RETRY_DELAY_SECS) * 1000L; StopWatch totalElapsedTimeStopWatch = new StopWatch(); totalElapsedTimeStopWatch.start(); // Loop indefinitely. We will exit the loop if the method returns normally or a non-retryable exception is thrown. // If a retryable exception is thrown, the loop will exit after we have exceeded the maximum retry delay seconds. while (true) { try { // Proceed to the join point (i.e. call the method and let it return normally). return pjp.proceed(); } catch (AmazonServiceException ase) { // Retry if: // 1) Is throttling exception. // 2) Is 5xx exception. // 3) Is an error to be re-tried on as defined in configuration. if (RetryUtils.isThrottlingException(ase) || is5xxException(ase) || isRetryableException(ase)) { long totalElapsedTime = totalElapsedTimeStopWatch.getTime(); // It's a retryable exception. Check if we've retried for enough time. if (totalElapsedTime >= maxTotalDelay) { // We've retried for enough time so re-throw the original exception. LOGGER.warn("An exception occurred while calling " + methodName + ". The method has been retried for " + (totalElapsedTime / 1000.0f) + " second(s) and will not be retried anymore since it would exceed the maximum retry delay." + " The exception will now be thrown."); throw ase; } // Get the next delay. long nextSleepDelay = Math.min(((long) Math.pow(2, retryCount) * minAwsDelay), maxAwsDelay); long timeLeftToRetry = maxTotalDelay - totalElapsedTime; // If time left to try is less than next sleep delay, then use time left as next sleep delay to be retried the last time. if (timeLeftToRetry < nextSleepDelay) { nextSleepDelay = timeLeftToRetry; } // Log a warning so we're aware that we are retrying. LOGGER.warn("A retryable exception occurred while calling " + methodName + ". " + (totalElapsedTime / 1000.0f) + " second(s) have elapsed since initial exception and maximum retry delay is " + (maxTotalDelay / 1000.0f) + " second(s). Will retry in " + (nextSleepDelay / 1000.0f) + " second(s)."); // We can retry again so increment a counter to keep track of the number of times we retried and recalculate the total elapsed time. retryCount++; // Sleep for the next sleep delay. herdThreadHelper.sleep(nextSleepDelay); } else { // It's not a retryable exception (i.e. some other type of service exception) so just re-throw it. throw ase; } } } }
From source file:org.finra.herd.dao.MethodLoggingAdvice.java
License:Apache License
/** * Around advice that logs methods being invoked for all DAO operations methods. * * @param pjp the proceeding join point. * * @return the return value of the method we are advising. * @throws Throwable if there were any problems executing the method. *///from w w w .j a v a 2s .co m @Around("operationsMethods()") public Object logMethodBeingInvoked(ProceedingJoinPoint pjp) throws Throwable { // Get the target class being called. Class<?> targetClass = pjp.getTarget().getClass(); // Get the target method being called. MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); Method targetMethod = targetMethodSignature.getMethod(); if (targetMethod.getDeclaringClass().isInterface()) { // Get the underlying implementation if we are given an interface. targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), targetMethod.getParameterTypes()); } // Only log the method if the class and method aren't suppressing logging and the log level is debug. if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null) && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null) && (LOGGER.isDebugEnabled())) { LOGGER.debug("javaMethod=\"{}.{}\"", targetClass.getName(), targetMethodSignature.getName()); } // Proceed to the join point (i.e. call the method and let it return). return pjp.proceed(); }
From source file:org.finra.herd.service.advice.PublishJmsMessagesAdvice.java
License:Apache License
/** * Publishes all JMS messages stored in the "in-memory" JMS message queue. * * @param joinPoint the join point//from ww w . j av a 2 s . co m * * @return the return value of the method at the join point * @throws Throwable if any errors were encountered */ @Around("serviceMethods()") public Object publishJmsMessages(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); boolean publishJmsMessages = method.isAnnotationPresent(PublishJmsMessages.class); // Proceed to the join point (i.e. call the method and let it return). try { Object returnValue = joinPoint.proceed(); if (publishJmsMessages) { if (LOGGER.isDebugEnabled()) { // Get the target class being called. Class<?> targetClass = joinPoint.getTarget().getClass(); LOGGER.debug( "Method is initiating JMS message publishing. javaMethod=\"{}.{}\" jmsMessageInMemoryQueueSize={}", targetClass.getName(), methodSignature.getName(), jmsMessageInMemoryQueue.size()); } // Publish all JMS messages stored in the "in-memory" JMS message queue. while (!jmsMessageInMemoryQueue.isEmpty()) { JmsMessage jmsMessage = jmsMessageInMemoryQueue.remove(); try { // Send a text message to the specified AWS SQS queue. sqsDao.sendSqsTextMessage(awsHelper.getAwsParamsDto(), jmsMessage.getJmsQueueName(), jmsMessage.getMessageText()); LOGGER.info("Published JMS message. jmsQueueName=\"{}\" jmsMessagePayload={}", jmsMessage.getJmsQueueName(), jmsMessage.getMessageText()); } catch (Exception sqsException) { LOGGER.warn( "Failed to publish message to the JMS queue. jmsQueueName=\"{}\" jmsMessagePayload={}", jmsMessage.getJmsQueueName(), jmsMessage.getMessageText(), sqsException); try { jmsPublishingService.addJmsMessageToDatabaseQueue(jmsMessage.getJmsQueueName(), jmsMessage.getMessageText()); } catch (Exception dbException) { LOGGER.error( "Failed to add JMS message to the database queue. jmsQueueName=\"{}\" jmsMessagePayload={}", jmsMessage.getJmsQueueName(), jmsMessage.getMessageText(), dbException); } } } } return returnValue; } finally { // Removes all of the elements from the queue, since the thread might be reused from the thread pool. if (publishJmsMessages) { jmsMessageInMemoryQueue.clear(); } } }