Example usage for org.aspectj.lang ProceedingJoinPoint proceed

List of usage examples for org.aspectj.lang ProceedingJoinPoint proceed

Introduction

In this page you can find the example usage for org.aspectj.lang ProceedingJoinPoint proceed.

Prototype

public Object proceed() throws Throwable;

Source Link

Document

Proceed with the next advice or target method invocation

Usage

From source file:com.brienwheeler.lib.monitor.work.impl.MonitoredWorkAspect.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Around("monitoredWorkPointcut()")
public Object aroundMonitoredWork(final ProceedingJoinPoint joinPoint) throws InterruptedException {
    ValidationUtils.assertTrue(joinPoint.getTarget() instanceof IWorkMonitorProvider,
            "@MonitoredWork target must be subclass of IWorkMonitorProvider");
    ValidationUtils.assertTrue(joinPoint.getSignature() instanceof MethodSignature,
            "@MonitoredWork signature must be a method");

    final WorkMonitor workMonitor = ((IWorkMonitorProvider) joinPoint.getTarget()).getWorkMonitor();
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    MonitoredWork annotation = signature.getMethod().getAnnotation(MonitoredWork.class);

    final String workName = annotation != null && !annotation.value().isEmpty() ? annotation.value()
            : signature.getName();/*  w  w  w .  j a  v a2 s .co m*/

    long start = System.currentTimeMillis();
    try {
        Object ret = joinPoint.proceed();
        workMonitor.recordWorkOk(workName, System.currentTimeMillis() - start);
        return ret;
    } catch (InterruptedException e) {
        workMonitor.recordWorkError(workName, System.currentTimeMillis() - start);
        Thread.currentThread().interrupt();
        throw e;
    } catch (RuntimeException e) {
        workMonitor.recordWorkError(workName, System.currentTimeMillis() - start);
        throw e;
    } catch (Error e) {
        workMonitor.recordWorkError(workName, System.currentTimeMillis() - start);
        throw e;
    } catch (Throwable e) {
        workMonitor.recordWorkError(workName, System.currentTimeMillis() - start);
        throw new RuntimeException(e);
    }
}

From source file:com.brienwheeler.lib.svc.impl.GracefulShutdownAspect.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Around("gracefulShutdownPointcut()")
public Object aroundGracefulShutdown(final ProceedingJoinPoint joinPoint) {
    ValidationUtils.assertTrue(joinPoint.getTarget() instanceof StartableServiceBase,
            "@GracefulShutdown target must be subclass of StartableServiceBase");

    ServiceWork work = new ServiceWork() {
        @Override//from w  w  w  . java 2s .  c  o m
        public Object doServiceWork() throws InterruptedException {
            try {
                return joinPoint.proceed();
            } catch (InterruptedException e) {
                throw e;
            } catch (RuntimeException e) {
                throw e;
            } catch (Error e) {
                throw e;
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
    };

    StartableServiceBase service = (StartableServiceBase) joinPoint.getTarget();
    return service.executeWithGracefulShutdown(work);
}

From source file:com.canelmas.let.LetAspect.java

License:Apache License

/**
 * Advice that triggers the runtime permission request,
 * executed around {@link AskPermission} annotated methods.
 *
 * #ProceedingJoinPoint is proceeded if the runtime is below {@link android.os.Build.VERSION_CODES#M}
 *
 * @see {@link LetContext}// ww  w  .  ja  v  a 2s  .  c o m
 * @see {@link AskPermission}
 *
 * @param joinPoint Annotated method
 * @param source Source of the annotated method i.e {@link android.app.Fragment} or
 * {@link android.app.Activity}
 * @return #joinPoint.proceed if annotated method should be executed; {@link RuntimePermissionRequest#proceed()}
 * otherwise
 * @throws Throwable
 */
@Around("execution(@com.canelmas.let.AskPermission * *(..)) && this(source)")
public Object annotatedMethods(final ProceedingJoinPoint joinPoint, Object source) throws Throwable {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return joinPoint.proceed();
    }

    return new RuntimePermissionRequest(joinPoint, source).proceed();

}

From source file:com.chnoumis.commons.log.LogAspect.java

License:Apache License

@Around("execution(* com.chnoumis..*.*(..))")
public Object doTrace(ProceedingJoinPoint pjp) throws Throwable {
    Log log = LogFactory.getLog(pjp.getSignature().getDeclaringType());
    Object retVal = null;/*from   ww w .  jav  a  2 s  .  c o  m*/
    //if (log.isDebugEnabled()) {
    log.info("Starting method " + pjp.getSignature().toLongString());
    retVal = pjp.proceed();
    log.info("Ending method " + pjp.getSignature().toLongString());
    // log.info("Method returned " + retVal);
    //}
    return retVal;
}

From source file:com.clicktravel.cheddar.application.continuation.DeferResultAspect.java

License:Apache License

@Around("@annotation(com.clicktravel.cheddar.application.continuation.DeferResult)")
public Object invokeAndDeferResult(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    continuationHandler.createContinuation();
    try {/*w w w . ja v  a 2 s  .co  m*/
        proceedingJoinPoint.proceed(); // discard returned value
        return continuationHandler.pollForMethodReturnValue();
    } finally {
        continuationHandler.removeContinuation();
    }
}

From source file:com.clicktravel.cheddar.application.retry.RetryableAspect.java

License:Apache License

@Around("@annotation(retryable)")
public Object attemptMethodAndRetryIfNeeded(final ProceedingJoinPoint proceedingJoinPoint,
        final Retryable retryable) throws Throwable {
    int attempts = 0;
    do {/* ww  w . j  av  a  2 s  . c om*/
        try {
            return proceedingJoinPoint.proceed();
        } catch (final Throwable thrownException) {
            attempts++;
            if (shouldRetryMethod(thrownException.getClass(), retryable, attempts)) {
                Thread.sleep(retryable.retryDelayMillis());
            } else {
                return processMethodFailure(proceedingJoinPoint, retryable.exceptionHandlers(),
                        thrownException);
            }
        }
    } while (true);
}

From source file:com.clicktravel.cheddar.application.retry.RetryableAspectTest.java

License:Apache License

@Test
public void shouldAttemptMethodAndNotRetry_withExceptionThrownAndRetryDisabled() throws Throwable {
    // Given/* ww  w.  j  a  va 2 s . c o m*/
    final RetryableAspect retryableAspect = new RetryableAspect();
    final ProceedingJoinPoint mockProceedingJoinPoint = setupSimpleProceedingJoinPointMock();
    final Retryable mockRetryable = setupSimpleRetryableMock();

    RetryableConfiguration.setRetryableEnabled(false);
    when(mockProceedingJoinPoint.proceed()).thenThrow(new RetryAspectTestException());

    // When
    RetryAspectTestException actualException = null;
    try {
        retryableAspect.attemptMethodAndRetryIfNeeded(mockProceedingJoinPoint, mockRetryable);
    } catch (final RetryAspectTestException e) {
        actualException = e;
    }

    // Then
    verify(mockProceedingJoinPoint).proceed();

    assertNotNull(actualException);
}

From source file:com.clicktravel.cheddar.application.retry.RetryableAspectTest.java

License:Apache License

@Test
public void shouldAttemptMethodAndRetry_withExceptionThrownAndRetryEnabledAndMaxAttemptsAdjusted()
        throws Throwable {
    // Given/*from w ww.jav  a2 s.c  o m*/
    final RetryableAspect retryableAspect = new RetryableAspect();
    final ProceedingJoinPoint mockProceedingJoinPoint = setupSimpleProceedingJoinPointMock();
    final int maxAttempts = randomIntInRange(2, 4);
    final Retryable mockRetryable = setupMockRetryable(maxAttempts, 2000);

    RetryableConfiguration.setRetryableEnabled(true);
    when(mockProceedingJoinPoint.proceed()).thenThrow(new RetryAspectTestException());

    // When
    RetryAspectTestException actualException = null;
    try {
        retryableAspect.attemptMethodAndRetryIfNeeded(mockProceedingJoinPoint, mockRetryable);
    } catch (final RetryAspectTestException e) {
        actualException = e;
    }

    // Then
    verify(mockProceedingJoinPoint, times(maxAttempts)).proceed();

    assertNotNull(actualException);
}

From source file:com.clicktravel.cheddar.application.retry.RetryableAspectTest.java

License:Apache License

@Test
public void shouldAttemptMethodAndFailImmedaitelyOnException_withImmediateFailureExceptionClassThrownAndRetryEnabled()
        throws Throwable {
    // Given//  w  w w. jav  a2  s .  c  o  m
    final RetryableAspect retryableAspect = new RetryableAspect();
    final ProceedingJoinPoint mockProceedingJoinPoint = setupSimpleProceedingJoinPointMock();
    final int maxAttempts = 3;
    final Retryable mockRetryable = setupMockRetryable(maxAttempts, 2000);

    RetryableConfiguration.setRetryableEnabled(true);
    when(mockRetryable.failImmediatelyOn()).thenReturn(new Class[] { RetryAspectTestException.class });
    when(mockProceedingJoinPoint.proceed()).thenThrow(new RetryAspectTestException());

    // When
    RetryAspectTestException actualException = null;
    try {
        retryableAspect.attemptMethodAndRetryIfNeeded(mockProceedingJoinPoint, mockRetryable);
    } catch (final RetryAspectTestException e) {
        actualException = e;
    }

    // Then
    verify(mockProceedingJoinPoint, atMost(1)).proceed();

    assertNotNull(actualException);
}

From source file:com.clicktravel.cheddar.application.retry.RetryableAspectTest.java

License:Apache License

@Test
public void shouldAttemptMethodAndRetry_withExceptionThrownAndRetryEnabledAndDelayAdjusted() throws Throwable {
    // Given//from w w w .  j  ava2 s  .  c  o m
    final RetryableAspect retryableAspect = new RetryableAspect();
    final ProceedingJoinPoint mockProceedingJoinPoint = setupSimpleProceedingJoinPointMock();
    final long EPSILON_MS = 300; // test timing tolerance
    final int retryDelayInMillis = 400;
    final int maxAttempts = randomIntInRange(2, 6);
    final int expectedRuntime = retryDelayInMillis * (maxAttempts - 1);
    final Retryable mockRetryable = setupMockRetryable(maxAttempts, retryDelayInMillis);

    RetryableConfiguration.setRetryableEnabled(true);
    when(mockProceedingJoinPoint.proceed()).thenThrow(new RetryAspectTestException());

    // When
    DateTime startDateTime = null;
    RetryAspectTestException actualException = null;
    try {
        startDateTime = DateTime.now();
        retryableAspect.attemptMethodAndRetryIfNeeded(mockProceedingJoinPoint, mockRetryable);
    } catch (final RetryAspectTestException e) {
        actualException = e;
    }

    // Then
    verify(mockProceedingJoinPoint, times(maxAttempts)).proceed();

    assertNotNull(actualException);
    final long elapsedMillis = DateTime.now().getMillis() - startDateTime.getMillis();
    assertTrue(Math.abs(elapsedMillis - expectedRuntime) < EPSILON_MS);
}