对于我的 Spark API,我正在构建集成测试。有时我想停止并启动 Spark 实例。当我这样做时,有时会遇到这样的问题:我正在创建一个新的 Spark 实例,而旧的实例仍在单独的线程上关闭。了解 Spark 实例何时实际关闭会很有帮助。
首先,我像这样启动 Spark 实例:
Spark.init();
Spark.awaitInitialization();
然后我像这样停止它:
Spark.stop();
现在我打电话后stop()
,Spark服务实际上并没有停止!
有没有类似的功能awaitInitialization()
或者知道 Spark 服务何时实际停止的另一种方法?
Spark 2.8.0 引入了awaitStop()
方法:https://github.com/perwendel/spark/pull/730 https://github.com/perwendel/spark/pull/730
如果您陷入以下版本(例如使用 Spark 2.6.0 的 Spark-kotlin),您可以使用一些反射来识别 Spark 的当前状态:
fun awaitShutdown() {
Spark.stop()
while (isSparkInitialized()) {
Thread.sleep(100)
}
}
/**
* Access the internals of Spark to check if the "initialized" flag is already set to false.
*/
private fun isSparkInitialized(): Boolean {
val sparkClass = Spark::class.java
val getInstanceMethod = sparkClass.getDeclaredMethod("getInstance")
getInstanceMethod.isAccessible = true
val service = getInstanceMethod.invoke(null) as Service
val serviceClass = service::class.java
val initializedField = serviceClass.getDeclaredField("initialized")
initializedField.isAccessible = true
val initialized = initializedField.getBoolean(service)
return initialized
}
(摘自https://github.com/debuglevel/sparkmicroserviceutils/blob/ec6b9692d808ecc448f1828f5487739101a2f62e/src/main/kotlin/de/debuglevel/microservices/utils/spark/SparkTestUtils.kt https://github.com/debuglevel/sparkmicroserviceutils/blob/ec6b9692d808ecc448f1828f5487739101a2f62e/src/main/kotlin/de/debuglevel/microservices/utils/spark/SparkTestUtils.kt)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)