package com.safframework.aop; 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; /** * Created by Tony Shen on 16/3/23. */ @Aspect public class AsyncAspect { @Around("execution(!synthetic * *(..)) && onAsyncMethod()") public void doAsyncMethod(final ProceedingJoinPoint joinPoint) throws Throwable { asyncMethod(joinPoint); } @Pointcut("@within(com.safframework.aop.annotation.Async)||@annotation(com.safframework.aop.annotation.Async)") 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(); } }