helper.Timer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-sim-ecu-dsl Show documentation
Show all versions of doip-sim-ecu-dsl Show documentation
This is a kotlin based domain specific language (dsl), to quickly and intuitively write custom DoIP ECU simulations.
package helper
import org.slf4j.LoggerFactory
import java.util.*
private val logger = LoggerFactory.getLogger(EcuTimerTask::class.java)
class EcuTimerTask(private val action: TimerTask.() -> Unit) : TimerTask() {
private var _canBeRemoved = false
override fun run() {
try {
action()
} catch (e: Exception) {
logger.error("Error while executing timer: " + e.stackTraceToString())
} finally {
_canBeRemoved = true
}
}
override fun cancel(): Boolean {
try {
return super.cancel()
} finally {
_canBeRemoved = true
}
}
val canBeRemoved
get() = _canBeRemoved
}
fun Timer.scheduleEcuTimerTask(delay: Long, action: TimerTask.() -> Unit): EcuTimerTask {
val task = EcuTimerTask(action)
schedule(task, delay)
return task
}