在RxJava 1中,订阅Observer返回了一个可以取消订阅的订阅.
在RxJava 2中,使用Observer订阅返回void并且没有Disposeable.怎么可能停止"订阅"?
// v1
rx.Observable<Long> v1hot = rx.Observable.interval(1, TimeUnit.SECONDS);
rx.Observer<Long> v1observer = new TestSubscriber<>();
Subscription subscription = v1hot.subscribe(v1observer);
subscription.unsubscribe();
// v2
Observable<Long> v2hot = Observable.interval(1, TimeUnit.SECONDS);
Observer<Long> v2Observer = new TestObserver<>();
v2hot.subscribe(v2Observer); // void
Run Code Online (Sandbox Code Playgroud)
编辑:如何处理案例,我们使用一个本身不实现的观察者Disposable,如BehaviorSubject?就像在这个例子中:
// v1
rx.Observable<Long> v1hot = rx.Observable.interval(1, TimeUnit.SECONDS);
rx.Observer<Long> v1observer = rx.subjects.BehaviorSubject.create();
Subscription subscription = v1hot.subscribe(v1observer);
subscription.unsubscribe();
// v2
Observable<Long> v2hot = Observable.interval(1, TimeUnit.SECONDS);
Observer<Long> v2Observer = BehaviorSubject.createDefault(-1L);
v2hot.subscribe(v2Observer); // void
Run Code Online (Sandbox Code Playgroud) 我有一个通用方法,它接受任何类型作为其参数。
例如,我想要一个切入点,该切入点仅以“String”类型作为其参数来匹配对方法的调用。最终的要求是将建议执行的范围限制为“字符串”参数。
这是我的通用类和方法:
public class Param<T> {
public T execute(T s){
return s;
}
}
Run Code Online (Sandbox Code Playgroud)
主类:我的应用程序使用布尔值和字符串作为参数调用方法。
public static void main(String[] args) {
Param<String> sp = new Param<String>();
String rs = sp.execute("myString"); //want a joint point
Param<Boolean> bp = new Param<Boolean>();
Boolean rb = bp.execute(true); //dont want a joint point
}
Run Code Online (Sandbox Code Playgroud)
下面的切入点对 String 和 Boolean 参数都有效(实际上适用于任何类型)。但是我想要一个切入点来拦截方法调用,只有当参数是字符串类型时。
@Pointcut("call(* com.amazon.auiqa.aspectj.generics.Param.execute(**))")
void param(){}
@Pointcut("execution(Object com.amazon.auiqa.aspectj.generics.Param.execute(Object))")
void param(){}
Run Code Online (Sandbox Code Playgroud)
下面的对我不起作用:
@Pointcut("execution(String com.amazon.auiqa.aspectj.generics.Param.execute(String))")
@Pointcut("call(String com.amazon.auiqa.aspectj.generics.Param.execute(String))")
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能在这里实现我想要实现的目标。我想对方法返回类型做同样的事情。
我正在尝试更改 System.currentTimeMillis() 方法的行为以进行测试。我找到了下面的方法,但我不能在代码中使用 aspect 关键字。我真的不明白如何使用这种方法。有什么建议?
public aspect CurrentTimeInMillisMethodCallChanger {
long around():
call(public static native long java.lang.System.currentTimeMillis())
&& within(user.code.base.pckg.*) {
return 0; // provide your own implementation returning a long
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅包含上述代码来源的相关答案
我想抛出从ExceptionifTry.ofCallable()失败扩展而来的异常。
我有一个类型的可调用:
final Callable<MyResponse> decoratedCallable =
circuitBreakerService.getDecoratedMethod(
myArg1,
() -> myFunction(myArg1, myArg2, myArg3)
);
Run Code Online (Sandbox Code Playgroud)
我正在尝试这样的事情:
Try.ofCallable(decoratedCallable).onFailure(throwable -> {
if (throwable instanceof CallNotPermittedException) {
throw new MyRuntimeExceptionA("msg1", throwable);
} else {
throw new MyRuntimeExceptionB("msg2", throwable);
}
});
Run Code Online (Sandbox Code Playgroud)
如果两者都有效,则此方法有效(包装上述两个语句的函数会抛出正确的异常 MyRuntimeExceptionA 和 MyRuntimeExceptionB)MyRuntimeExceptionA,但如果它们扩展,则MyRuntimeExceptionB无效。RuntimeExceptionException
如果它们扩展Exception,那么我无法将它们从主函数中抛出。IDE 要求将它们包装在 try/catch 中 - 我不希望这样做。
java ×3
aspectj ×2
aop ×1
aspect ×1
generics ×1
resilience4j ×1
rx-java ×1
rx-java2 ×1
spring-aop ×1
vavr ×1