开发者社区 > 云原生 > 正文

如何在守护进程协程中进行通用调用?

这是一个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

展开
收起
大圣东游 2023-05-11 15:54:13 123 0
1 条回答
写回答
取消 提交回答
  • private fun getConfig(inter: String): GenericService {
        return newConfig(inter).get()
    }
    

    在短期内,这个函数可以串行调用而不是并发调用吗?

    对于这个并发问题,我将仔细研究是什么导致了它,然后对它进行优化。

    原回答者GitHub用户AlbumenJ

    2023-05-12 10:25:29
    赞同 展开评论 打赏

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
服务上云加速大家居产业C2M进程 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多