0

In my Kotlin project

I want start two independent timer. First must run every 30 seconds.

Second must run every 20 seconds (start after 5 seconds).

I try this:

import kotlin.concurrent.schedule
import kotlin.concurrent.timerTask

  Timer().scheduleAtFixedRate(timerTask { login() }, 1000, 30 * 1000)
  Timer().schedule(5_000) { Timer().scheduleAtFixedRate(timerTask { updateState() }, 1000, 20 * 1000) }

First timer success run (login) periodically ever 20 seconds. Nice.

But seconds timer start only ONCE. Not run (updateState) periodically every 30 seconds.

Method login do sync http request. Method updateState also do sync http request.

import okhttp3.MediaType.Companion.toMediaType
    import okhttp3.OkHttpClient
    import okhttp3.Request
    import okhttp3.RequestBody.Companion.toRequestBody
    import okhttp3.Response

     fun login() {
        val loginRequestURL =
                    "${Config.instance.loginApiUrl}/importAPILogist.php"
            
            val requestToc= Request.Builder()
                    .url(loginRequestURL)
                    .get()
                    .build()
            val httpClient = OkHttpClient()
            val loginResponse: Response = httpClient.newCall(requestToc).execute() // sync request
    }

and another method:

   import okhttp3.MediaType.Companion.toMediaType
    import okhttp3.OkHttpClient
    import okhttp3.Request
    import okhttp3.RequestBody.Companion.toRequestBody
    import okhttp3.Response
    
      fun updateOrdersState() {
            logger.info("updateOrdersState:")
            val someRequestURL = "${Config.instance.someApiUrl}/exportAPI"
            val requestOrderStateTocan = Request.Builder()
                    .url(someRequestURL)
                    .get()
                    .build()
            val httpClient = OkHttpClient()
            val tocanOrderStateResponse: Response = httpClient.newCall(requestOrderStateTocan).execute() // sync request

Method updateState can throw exception(java.net.SocketTimeoutException)

Maybe I need to catch exception when http request is not success? E.g.

Exception in thread "Timer-2" java.net.SocketTimeoutException: timeout
Alexei
  • 14,350
  • 37
  • 121
  • 240
  • Works fine with simple `println` instead of `updateState()`. What's inside this function? – Михаил Нафталь Feb 09 '21 at 14:16
  • @МихаилНафталь do sync http request. I update my post – Alexei Feb 09 '21 at 14:24
  • @МихаилНафталь Maybe I need to catch exception when http request is not success? Method updateState can throw exception – Alexei Feb 09 '21 at 14:46
  • 1
    Don't know about your issue, but wanted to mention I don't think you need to wrap your second timer in another timer just to delay when it first starts. There's already a `delay` parameter for the `scheduleAtFixedRate` function. – Tenfour04 Feb 09 '21 at 15:00
  • You don't set any timeout in your `Request.Builder()`, so by default it's infinite (due to infinite callTimeout). See: https://www.baeldung.com/okhttp-timeouts and https://stackoverflow.com/questions/55122236/okhttp3-never-timeout-on-slow-internet – Михаил Нафталь Feb 09 '21 at 15:14
  • @МихаилНафталь After I use try/catch in method updateOrdersState - the problem is gone. And now success periodically start updateOrdersState every 30 sec – Alexei Feb 09 '21 at 15:16
  • @МихаилНафталь Can't set timeout. Error https://stackoverflow.com/questions/66184378/okhttp4-4-0-cant-set-timeout-val-cannot-be-reassigned – Alexei Feb 13 '21 at 11:44

0 Answers0