Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Generics 在重试之前,如何在RxJava2(RxJava3)链中使超时运算符触发一次异常_Generics_Kotlin_Timeout_Rx Java_Rx Java2 - Fatal编程技术网

Generics 在重试之前,如何在RxJava2(RxJava3)链中使超时运算符触发一次异常

Generics 在重试之前,如何在RxJava2(RxJava3)链中使超时运算符触发一次异常,generics,kotlin,timeout,rx-java,rx-java2,Generics,Kotlin,Timeout,Rx Java,Rx Java2,我正在尝试扩展我的自定义可流动变压器的功能,我使用该变压器有条件地从一个可流动切换到另一个,由提供的函数返回,即,在我的切换功能中我向服务器发送一个项目,如果初始项目符合某些条件,则切换到服务器响应流: /** * Switches from upstreamFlowable to anotherFlowable * using switchFunction if item from upstreamFlowable * matches test condition * * usage

我正在尝试扩展我的自定义
可流动变压器
的功能,我使用该变压器有条件地从一个
可流动
切换到另一个,由提供的
函数
返回,即,在我的
切换功能中
我向服务器发送一个项目,如果初始项目符合某些条件,则切换到服务器响应流:

/**
 * Switches from upstreamFlowable to anotherFlowable
 * using switchFunction if item from upstreamFlowable
 * matches test condition
 *
 * usage:
 *       upstreamFlowable
 *       .compose(RxSwitchFlowablesOnCondition(condition, switchFunction))
 **/
open class
RxSwitchFlowablesOnCondition<T, R>(private val condition : Predicate<T>,
                                   private val switchFunction : Function<T, Flowable<R>>) : FlowableTransformer<T, R>
{
  override fun
  apply(upstreamFlowable : Flowable<T>) : Publisher<R>
  {
    return upstreamFlowable
    .compose(RxFlowableOnIo<T>())
    .filter {emittedItem : T ->
      condition.test(emittedItem)
    }
    .switchMap {emittedItem : T ->
      val anotherFlowable = switchFunction.apply(emittedItem)

      anotherFlowable
      //.timeout(CONNECTION_TIMEOUT_SECONDS, SECONDS) // #1
    }
    //.timeout(CONNECTION_TIMEOUT_SECONDS, SECONDS) // #2
  }
}
我想做的是添加
timeout
逻辑,例如,如果通过
upstreamFlowable
发送一个项目和在另一个
流中获得响应之间的时间大于
连接超时时间\u秒
,我想触发一个错误并重新订阅
upstreamFlowable
,这样我就可以等待下一个
emittedItem

我所拥有的:

  • 如果我取消注释行
    #1
    ,则超时会产生一个
    TimeoutException
    ,重试重新订阅到链,并且即使没有下一个发出的项目,我也会在每
    连接超时时间段
    秒继续获得超时异常如何跳过它们?
  • 如果我取消注释行
    #2
    ,则自订阅后,而不是自项目发出后,
    连接超时秒后,我开始接收
    TimeoutException
    s如何修复它?

  • 我需要你的帮助。提前谢谢你

    要获得所需的行为,只需在
    开关映射
    块中进行小的更改。因此,我修改了您的
    apply
    实现

    override fun
      apply(upstreamFlowable : Flowable<T>) : Publisher<R>
      {
        return upstreamFlowable
        .compose(RxFlowableOnIo<T>())
        .filter {emittedItem : T ->
          condition.test(emittedItem)
        }
        .switchMap {
            Flowable.just(it)
                .switchMap { emittedItem : T ->
                    val anotherFlowable = switchFunction(emittedItem)
    
                     anotherFlowable
                  }
                 .timeout(CONNECTION_TIMEOUT_SECONDS, SECONDS)
             }
      }
    
    //RXS可切换状态

    open class
        RxSwitchFlowablesOnCondition<T, R>(private val condition : Predicate<T>,
                                           private val switchFunction : (T) -> Flowable<R>) : FlowableTransformer<T, R>
        {
            override fun
                    apply(upstreamFlowable : Flowable<T>) : Publisher<R>
            {
                return upstreamFlowable
                        .observeOn(Schedulers.io())
                        .filter {emittedItem : T ->
                            condition.test(emittedItem)
                        }
                        .switchMap {
                            Flowable.just(it)
                                    .doOnNext { println("onNext ${System.currentTimeMillis()}") }
                                    .switchMap { emittedItem : T ->
                                        val anotherFlowable = switchFunction(emittedItem)
    
                                        anotherFlowable
                                    }
                                    .timeout(500, TimeUnit.MILLISECONDS)
                        }
    
            }
        }
    

    这与您的期望一致吗?

    谢谢您的回答,但我仍然在订阅后的连接\u超时\u秒后开始接收TimeoutException,而不是从项目发布后开始接收。我添加了示例代码以显示TimeoutException发生在项目发布后。它与您的期望一致吗?嗯,它确实在订阅并发出下一项后触发超时。谢谢你的关注!
    val upstream = Flowable.timer(2, TimeUnit.SECONDS)
            upstream
                    .doOnSubscribe { println("subscribed ${System.currentTimeMillis()}") }
                    .compose(
                            RxSwitchFlowablesOnCondition<Long, Long>(
                                    Predicate { true },
                                    { Flowable.timer(1, TimeUnit.SECONDS)})
                    )
                    .doOnError { println("timeout ${System.currentTimeMillis()}") }
                    .retry()
                    .subscribe()
    
    open class
        RxSwitchFlowablesOnCondition<T, R>(private val condition : Predicate<T>,
                                           private val switchFunction : (T) -> Flowable<R>) : FlowableTransformer<T, R>
        {
            override fun
                    apply(upstreamFlowable : Flowable<T>) : Publisher<R>
            {
                return upstreamFlowable
                        .observeOn(Schedulers.io())
                        .filter {emittedItem : T ->
                            condition.test(emittedItem)
                        }
                        .switchMap {
                            Flowable.just(it)
                                    .doOnNext { println("onNext ${System.currentTimeMillis()}") }
                                    .switchMap { emittedItem : T ->
                                        val anotherFlowable = switchFunction(emittedItem)
    
                                        anotherFlowable
                                    }
                                    .timeout(500, TimeUnit.MILLISECONDS)
                        }
    
            }
        }
    
    subscribed 1569286225769
    onNext 1569286227790
    timeout 1569286228292  // around 500 ms after onNext
    subscribed 1569286228295
    onNext 1569286230297
    timeout 1569286230800
    subscribed 1569286230800
    onNext 1569286232803
    timeout 1569286233306
    subscribed 1569286233306