
com.pubnub.internal.endpoints.remoteaction.RetryingRemoteAction.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-core-impl Show documentation
Show all versions of pubnub-core-impl Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!
package com.pubnub.internal.endpoints.remoteaction
import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.endpoints.remoteaction.ExtendedRemoteAction
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.v2.callbacks.Result
import java.util.concurrent.ExecutorService
import java.util.function.Consumer
internal class RetryingRemoteAction(
private val remoteAction: ExtendedRemoteAction,
private val maxNumberOfAutomaticRetries: Int,
private val executorService: ExecutorService,
) : ExtendedRemoteAction {
private lateinit var cachedCallback: Consumer>
@Throws(PubNubException::class)
override fun sync(): T {
validate()
var thrownException: PubNubException? = null
for (i in 0 until maxNumberOfAutomaticRetries) {
thrownException =
try {
return remoteAction.sync()
} catch (ex: Throwable) {
PubNubException.from(ex)
}
}
throw thrownException!!
}
override fun async(callback: Consumer>) {
cachedCallback = callback
executorService.execute(
Runnable {
try {
validate()
} catch (ex: PubNubException) {
callback.accept(
Result.failure(ex),
)
return@Runnable
}
var lastException: Throwable? = null
for (i in 0 until maxNumberOfAutomaticRetries) {
try {
callback.accept(Result.success(remoteAction.sync()))
} catch (e: Throwable) {
lastException = e
}
}
callback.accept(Result.failure(PubNubException.from(lastException!!).copy(remoteAction = this)))
},
)
}
override fun retry() {
async(cachedCallback)
}
override fun silentCancel() {
remoteAction.silentCancel()
}
override fun operationType(): PNOperationType {
return remoteAction.operationType()
}
@Throws(PubNubException::class)
private fun validate() {
if (maxNumberOfAutomaticRetries < 1) {
throw PubNubException(PubNubError.INVALID_ARGUMENTS)
}
}
companion object {
fun autoRetry(
remoteAction: ExtendedRemoteAction,
maxNumberOfAutomaticRetries: Int,
executorService: ExecutorService,
): RetryingRemoteAction {
return RetryingRemoteAction(remoteAction, maxNumberOfAutomaticRetries, executorService)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy