@Before("timeoutExecution(timeout)")
  public void beforeTimeoutExecution(JoinPoint thisJoinPoint, Timeout timeout) {
    final IPluginExecutable executable = (IPluginExecutable) thisJoinPoint.getTarget();

    activeExecutions.add(executable);

    try {
      timer.schedule(
          new TimerTask() {
            @Override
            public void run() {
              if (activeExecutions.contains(executable)) {
                executable.interrupted();
              }
            }
          },
          timeout.value());
    } catch (SecurityException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 2
-1
  /**
   * Creates a gateway instance for the given <code>gatewayInterface</code>. The returned instance
   * is a Proxy that implements that interface.
   *
   * @param gatewayInterface The interface declaring the gateway methods
   * @param <T> The interface declaring the gateway methods
   * @return A Proxy implementation implementing the given interface
   */
  @SuppressWarnings("unchecked")
  public <T> T createGateway(Class<T> gatewayInterface) {
    Map<Method, InvocationHandler> dispatchers = new HashMap<Method, InvocationHandler>();
    for (Method gatewayMethod : gatewayInterface.getMethods()) {
      MetaDataExtractor[] extractors = extractMetaData(gatewayMethod.getParameterAnnotations());

      InvocationHandler dispatcher =
          new DispatchOnInvocationHandler(
              commandBus, retryScheduler,
              dispatchInterceptors, extractors);
      final Class<?>[] arguments = gatewayMethod.getParameterTypes();
      if (Future.class.equals(gatewayMethod.getReturnType())) {
        // no wrapping
      } else if (arguments.length >= 3
          && TimeUnit.class.isAssignableFrom(arguments[arguments.length - 1])
          && (long.class.isAssignableFrom(arguments[arguments.length - 2])
              || int.class.isAssignableFrom(arguments[arguments.length - 2]))) {
        dispatcher =
            wrapToReturnWithTimeoutInArguments(
                dispatcher, arguments.length - 2, arguments.length - 1);
      } else {
        Timeout timeout = gatewayMethod.getAnnotation(Timeout.class);
        if (timeout == null) {
          timeout = gatewayMethod.getDeclaringClass().getAnnotation(Timeout.class);
        }
        if (timeout != null) {
          dispatcher = wrapToReturnWithFixedTimeout(dispatcher, timeout.value(), timeout.unit());
        } else if (!Void.TYPE.equals(gatewayMethod.getReturnType())
            || gatewayMethod.getExceptionTypes().length > 0) {
          dispatcher = wrapToWaitForResult(dispatcher);
        } else {
          dispatcher = wrapToFireAndForget(dispatcher);
        }
      }
      Class<?>[] declaredExceptions = gatewayMethod.getExceptionTypes();
      if (!contains(declaredExceptions, TimeoutException.class)) {
        dispatcher = wrapToReturnNullOnTimeout(dispatcher);
      }
      if (!contains(declaredExceptions, InterruptedException.class)) {
        dispatcher = wrapToReturnNullOnInterrupted(dispatcher);
      }
      dispatcher = wrapUndeclaredExceptions(dispatcher, declaredExceptions);
      dispatchers.put(gatewayMethod, dispatcher);
    }

    return gatewayInterface.cast(
        Proxy.newProxyInstance(
            gatewayInterface.getClassLoader(),
            new Class[] {gatewayInterface},
            new GatewayInvocationHandler(
                dispatchers, commandBus, retryScheduler, dispatchInterceptors)));
  }