这是一个Spring项目。我想运行一个守护进程协程来调用远程方法。我在Spring Component postConstruct之后启动一个协程,并调用泛型调用。例外情况如下:
[23/11/22 18:28:28:471 CST] DefaultDispatcher-worker-1 ERROR deploy.DefaultModuleDeployer: [DUBBO] Dubbo Module[1.1.1] start failed: java.util.ConcurrentModificationException, dubbo version: 3.1.0, current host: 192.168.101.1 java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.exportMetadataService(DefaultApplicationDeployer.java:920) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.prepareApplicationInstance(DefaultApplicationDeployer.java:613) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.checkState(DefaultApplicationDeployer.java:824) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.notifyModuleChanged(DefaultApplicationDeployer.java:812) at org.apache.dubbo.config.deploy.DefaultModuleDeployer.onModuleStarted(DefaultModuleDeployer.java:251) at org.apache.dubbo.config.deploy.DefaultModuleDeployer.start(DefaultModuleDeployer.java:158) at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:223) at com.gmair.schedproject.scheduler.service.GenericCallService.getConfig(GenericCallService.kt:28) at com.gmair.schedproject.scheduler.service.GenericCallService.invoke(GenericCallService.kt:31) at com.gmair.schedproject.scheduler.service.DaemonComponent$run$1.invokeSuspend(DaemonComponent.kt:19) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) [23/11/22 18:28:28:471 CST] DefaultDispatcher-worker-1 INFO metadata.ServiceInstanceMetadataUtils: [DUBBO] Start registering instance address to registry., dubbo version: 3.1.0, current host: 192.168.101.1 [23/11/22 18:28:28:481 CST] DefaultDispatcher-worker-1 WARN client.AbstractServiceDiscovery: [DUBBO] No valid instance found, stop registering instance address to registry., dubbo version: 3.1.0, current host: 192.168.101.1 [23/11/22 18:28:28:481 CST] DefaultDispatcher-worker-1 ERROR deploy.DefaultApplicationDeployer: [DUBBO] Dubbo Application1.1 found failed module: Dubbo Module[1.1.1], dubbo version: 3.1.0, current host: 192.168.101.1 java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.exportMetadataService(DefaultApplicationDeployer.java:920) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.prepareApplicationInstance(DefaultApplicationDeployer.java:613) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.checkState(DefaultApplicationDeployer.java:824) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.notifyModuleChanged(DefaultApplicationDeployer.java:812) at org.apache.dubbo.config.deploy.DefaultModuleDeployer.onModuleStarted(DefaultModuleDeployer.java:251) at org.apache.dubbo.config.deploy.DefaultModuleDeployer.start(DefaultModuleDeployer.java:158) at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:223) at com.gmair.schedproject.scheduler.service.GenericCallService.getConfig(GenericCallService.kt:28) at com.gmair.schedproject.scheduler.service.GenericCallService.invoke(GenericCallService.kt:31) at com.gmair.schedproject.scheduler.service.DaemonComponent$run$1.invokeSuspend(DaemonComponent.kt:19) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask Exception in thread "DefaultDispatcher-worker-1" java.util.ConcurrentModificationException at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.exportMetadataService(DefaultApplicationDeployer.java:920) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.prepareApplicationInstance(DefaultApplicationDeployer.java:613) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.checkState(DefaultApplicationDeployer.java:824) at org.apache.dubbo.config.deploy.DefaultApplicationDeployer.notifyModuleChanged(DefaultApplicationDeployer.java:812) at org.apache.dubbo.config.deploy.DefaultModuleDeployer.onModuleStarted(DefaultModuleDeployer.java:251) at org.apache.dubbo.config.deploy.DefaultModuleDeployer.start(DefaultModuleDeployer.java:158) at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:223) at com.gmair.schedproject.scheduler.service.GenericCallService.getConfig(GenericCallService.kt:28) at com.gmair.schedproject.scheduler.service.GenericCallService.invoke(GenericCallService.kt:31) at com.gmair.schedproject.scheduler.service.DaemonComponent$run$1.invokeSuspend(DaemonComponent.kt:19) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
以下是主要代码。当我从TestController调用GenericCallService.invoke方法时,它可以很好地工作。
// DaemonComponent.kt @Component final class DaemonComponent: DisposableBean { @Autowired private lateinit var genericCallService: GenericCallService @PostConstruct fun run() { CoroutineScope(Dispatchers.Default).launch { genericCallService.invoke(GenericCall("com.gmair.schedproject.TestInterface", "message", "com.gmair.schedproject.GenericCallParams", "{"id": 5}")) } }
override fun destroy() {
println("destroy")
}
}
// GenericCallService.kt @Service class GenericCallService { private val applicationConfig = ApplicationConfig("scheduler") init { val registryConfig = RegistryConfig() registryConfig.address = "zookeeper://127.0.0.1:2181" applicationConfig.registry = registryConfig } private fun newConfig(inter: String): ReferenceConfig { val referenceConfig = ReferenceConfig() referenceConfig.setInterface(inter) referenceConfig.application = applicationConfig referenceConfig.generic = "true" return referenceConfig } private fun getConfig(inter: String): GenericService { return newConfig(inter).get() } fun invoke(params: GenericCall): Any { val genericService = getConfig(params.inter) val param = JSON.parseObject(params.genericCallParams, Class.forName(params.paramName)) val map = genericService.$invoke
(params.methodName, arrayOf(params.paramName), arrayOf(param)) as Map<*, *> return ObjectMapper().convertValue(map, Class.forName(map["class"] as String)) } }
原提问者GitHub用户Coegle
private fun getConfig(inter: String): GenericService {
return newConfig(inter).get()
}
在短期内,这个函数可以串行调用而不是并发调用吗?
对于这个并发问题,我将仔细研究是什么导致了它,然后对它进行优化。
原回答者GitHub用户AlbumenJ
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。