content: package cn.com.superLei.aoparms.aspect;
  
  import android.os.Looper;
  
  import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.annotation.Around;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Pointcut;
  
  import io.reactivex.BackpressureStrategy;
  import io.reactivex.Flowable;
  import io.reactivex.FlowableEmitter;
  import io.reactivex.FlowableOnSubscribe;
  import io.reactivex.android.schedulers.AndroidSchedulers;
  import io.reactivex.schedulers.Schedulers;
  
  /**
   * description $desc$
   * created by jerry on 2019/5/30.
   */
  @Aspect
  public class AsyncAspect {
  
      private static final String POINTCUT_METHOD = "execution(@cn.com.superLei.aoparms.annotation.Async * *(..))";
  
      @Around("onAsyncMethod()")
      public void doAsyncMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
          asyncMethod(joinPoint);
      }
  
      @Pointcut(POINTCUT_METHOD)
      public void onAsyncMethod() {
      }
  
      private void asyncMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
  
          Flowable.create(new FlowableOnSubscribe<Object>() {
                              @Override
                              public void subscribe(FlowableEmitter<Object> e) throws Exception {
                                  Looper.prepare();
                                  try {
                                      joinPoint.proceed();
                                  } catch (Throwable throwable) {
                                      throwable.printStackTrace();
                                  }
                                  Looper.loop();
                              }
                          }
                  , BackpressureStrategy.BUFFER)
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe();
      }
  }
  