使用 Spring-DM 导入 o​​sgi 服务时发生死锁

Deadlock happens when importing an osgi service using Spring-DM

这个问题与使用 spring-dm 导入 o​​sgi 服务时发生的死锁有关(有时如下所述)。

我最近在 Spring-DM 的 ServiceDynamicInterceptor class 中遇到了导致死锁情况的问题。当从两个包中加载两个应用程序上下文时会发生这种情况。假设 app ctx A 导出一个 osgi 服务,app ctx B 导入从 A 导出的 osgi 服务。当两个 bundle 一起启动时就会出现问题。当app ctx A初始化时,spring在发布osgi服务时同步调用服务注册的监听器。假设app ctx B同时初始化并执行osgi import。首先,它将 osgi 服务的侦听器添加到 bundle context,然后去查找 osgi 服务。正如监听器被添加到 bundle 上下文中一样,app ctx A 将其拾取并执行监听器。此时,获取锁的 app ctx B 尝试查找尚未发布的 osgi 服务,因为正在调用等待同一锁释放的侦听器,从而导致死锁。在 spring bean 创建超时达到 5 分钟后(默认),应用程序 ctx B 因 ServiceUnavailableException 导致的 BeanInitializationException 失败。

我已经列出了在两个 threads.Note 中执行的代码,查找和侦听器都在同一个文件中定义 - ServiceDynamicInterceptor。

App Ctx A 正在等待

的锁定
145: synchronized (lock) {
146:      servicePresent = (wrapper != null && wrapper.isServiceAlive());
147: }

App Ctx B 正在等待 osgi 服务发布于

428: private Object lookupService() {
429:        synchronized (lock) {
430:              return (Object) retryTemplate.execute(retryCallback);
431:        }
432: }

正如我之前所说,当两个包一起重新启动时,有时会发生这种情况。然而,由于显而易见的原因,它不会每次都发生。 我的问题是是否有任何方法可以避免这种死锁?

从线程转储中提取

应用点击率 A

"SpringOsgiExtenderThread-90" Id=908 in BLOCKED on lock=java.lang.Object@5a9a333f
     owned by pool-45-thread-1 Id=798
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor$Listener.serviceChanged(ServiceDynamicInterceptor.java:146)
    at org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:934)
    at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:795)
    at org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:544)
    at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4596)
    at org.apache.felix.framework.Felix.registerService(Felix.java:3604)
    at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:346)
    at org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean.registerService(OsgiServiceFactoryBean.java:310)
    at org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean.registerService(OsgiServiceFactoryBean.java:279)
    at org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean$Executor.registerService(OsgiServiceFactoryBean.java:95)
    at org.springframework.osgi.service.exporter.support.internal.controller.ExporterController.registerService(ExporterController.java:40)
    at org.springframework.osgi.service.dependency.internal.DefaultMandatoryDependencyManager.startExporter(DefaultMandatoryDependencyManager.java:320)
    at org.springframework.osgi.service.dependency.internal.DefaultMandatoryDependencyManager.checkIfExporterShouldStart(DefaultMandatoryDependencyManager.java:261)
    at org.springframework.osgi.service.dependency.internal.DefaultMandatoryDependencyManager.discoverDependentImporterFor(DefaultMandatoryDependencyManager.java:254)
      - locked org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean@2ae6baf0
    at org.springframework.osgi.service.dependency.internal.DefaultMandatoryDependencyManager.addServiceExporter(DefaultMandatoryDependencyManager.java:187)
    at org.springframework.osgi.service.dependency.internal.MandatoryDependencyBeanPostProcessor.postProcessAfterInitialization(MandatoryDependencyBeanPostProcessor.java:46)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1461)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      - locked java.util.concurrent.ConcurrentHashMap@25dfde15
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:589)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access00(AbstractDelegatedExecutionApplicationContext.java:69)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.run(AbstractDelegatedExecutionApplicationContext.java:355)
      - locked java.lang.Object@c6d4820
    at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)
    at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)
    at java.lang.Thread.run(Thread.java:745)

应用点击率 B

"pool-45-thread-1" Id=798 in TIMED_WAITING on lock=java.lang.Object@2b767b9b
    at java.lang.Object.wait(Native Method)
    at org.springframework.osgi.service.importer.support.internal.support.RetryTemplate.execute(RetryTemplate.java:106)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.lookupService(ServiceDynamicInterceptor.java:430)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.getTarget(ServiceDynamicInterceptor.java:415)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.afterPropertiesSet(ServiceDynamicInterceptor.java:472)
    at org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean.createProxy(OsgiServiceProxyFactoryBean.java:215)
    at org.springframework.osgi.service.importer.support.AbstractServiceImporterProxyFactoryBean.getObject(AbstractServiceImporterProxyFactoryBean.java:86)
    at org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean.getObject(OsgiServiceProxyFactoryBean.java:161)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
      - locked java.util.concurrent.ConcurrentHashMap@1a8ffb40
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109)
    at org.apache.camel.spring.spi.ApplicationContextRegistry.lookup(ApplicationContextRegistry.java:43)
    at org.apache.camel.impl.CompositeRegistry.lookup(CompositeRegistry.java:51)
    at org.apache.camel.impl.PropertyPlaceholderDelegateRegistry.lookup(PropertyPlaceholderDelegateRegistry.java:62)
    at org.apache.camel.util.CamelContextHelper.lookup(CamelContextHelper.java:119)
    at org.apache.camel.model.RouteBuilderDefinition.createRouteBuilder(RouteBuilderDefinition.java:64)
    at org.apache.camel.core.xml.AbstractCamelContextFactoryBean.installRoutes(AbstractCamelContextFactoryBean.java:624)
    at org.apache.camel.core.xml.AbstractCamelContextFactoryBean.afterPropertiesSet(AbstractCamelContextFactoryBean.java:276)
    at org.apache.camel.spring.CamelContextFactoryBean.afterPropertiesSet(CamelContextFactoryBean.java:235)
    at org.apache.camel.osgi.CamelContextFactoryBean.afterPropertiesSet(CamelContextFactoryBean.java:64)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      - locked java.util.concurrent.ConcurrentHashMap@1a8ffb40
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:155)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:86)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:327)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.publishEvent(ServiceDynamicInterceptor.java:439)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.access0(ServiceDynamicInterceptor.java:67)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor$EventSenderRetryTemplate.onMissingTarget(ServiceDynamicInterceptor.java:96)
    at org.springframework.osgi.service.importer.support.internal.support.RetryTemplate.execute(RetryTemplate.java:95)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.lookupService(ServiceDynamicInterceptor.java:430)
      - locked java.lang.Object@5a9a333f
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.getTarget(ServiceDynamicInterceptor.java:415)
    at org.springframework.osgi.service.importer.support.internal.aop.ServiceDynamicInterceptor.afterPropertiesSet(ServiceDynamicInterceptor.java:472)
    at org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean.createProxy(OsgiServiceProxyFactoryBean.java:215)
    at org.springframework.osgi.service.importer.support.AbstractServiceImporterProxyFactoryBean.getObject(AbstractServiceImporterProxyFactoryBean.java:86)
    at org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean.getObject(OsgiServiceProxyFactoryBean.java:161)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
      - locked java.util.concurrent.ConcurrentHashMap@1a8ffb40
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      - locked java.util.concurrent.ConcurrentHashMap@1a8ffb40
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
      - locked java.lang.Object@62b8345f
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access1(AbstractDelegatedExecutionApplicationContext.java:69)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.run(AbstractDelegatedExecutionApplicationContext.java:186)
    at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.normalRefresh(AbstractDelegatedExecutionApplicationContext.java:182)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$NoDependenciesWaitRefreshExecutor.refresh(AbstractDelegatedExecutionApplicationContext.java:89)
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.refresh(AbstractDelegatedExecutionApplicationContext.java:175)
    at com.xxx.bbb.core.extender.service.bbbManagedServiceFactory.run(BbbManagedServiceFactory.java:178)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

过去我在使用 Spring-DM 时遇到过类似的问题,我所做的解决方法是通过 BundleActivator 使用 ServiceTracker 跟踪服务。在某些情况下,我还必须自定义依赖该服务的所有代码(例如:autowiring)。这很丑陋......但它有效。经过这样的一些实验,我记得我也成功地使用了具有相同方法的 FactoryBean ...