我有两个托管服务工厂的蓝图,如下所示:
<cm:managed-service-factory factory-pid="com.foo" interface="com.foo.FooService">
<cm:managed-component class="com.foo.impl.FooServiceImpl" init-method="init">
<cm:managed-properties persistent-id="" update-method="update" update-strategy="component-managed"/>
</cm:managed-component>
</cm:managed-service-factory>
<cm:managed-service-factory factory-pid="com.foo" interface="com.foo.BarService">
<cm:managed-component class="com.foo.impl.BarServiceImpl" init-method="init">
<cm:managed-properties persistent-id="" update-method="update" update-strategy="component-managed"/>
</cm:managed-component>
</cm:managed-service-factory>
在集成测试(OPS4j Pax Exam)中使用上述服务时,偶尔会出现以下异常:
org.osgi.service.blueprint.container.ComponentDefinitionException: Cound not create component instance for .component-1
at org.apache.aries.blueprint.container.BlueprintContainerImpl.getComponentInstance(BlueprintContainerImpl.java:751)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.compendium.cm.CmManagedServiceFactory.doCreate(CmManagedServiceFactory.java:204)[10:org.apache.aries.blueprint.cm:1.0.1.redhat-60024]
at org.apache.aries.blueprint.compendium.cm.BaseManagedServiceFactory.internalUpdate(BaseManagedServiceFactory.java:138)[10:org.apache.aries.blueprint.cm:1.0.1.redhat-60024]
at org.apache.aries.blueprint.compendium.cm.BaseManagedServiceFactory.access$000(BaseManagedServiceFactory.java:37)[10:org.apache.aries.blueprint.cm:1.0.1.redhat-60024]
at org.apache.aries.blueprint.compendium.cm.BaseManagedServiceFactory$1.run(BaseManagedServiceFactory.java:87)[10:org.apache.aries.blueprint.cm:1.0.1.redhat-60024]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)[:1.7.0_45]
at java.util.concurrent.FutureTask.run(FutureTask.java:262)[:1.7.0_45]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_45]
at java.lang.Thread.run(Thread.java:744)[:1.7.0_45]
Caused by: java.lang.IllegalArgumentException: com.foo.impl
at java.lang.ClassLoader.definePackage(ClassLoader.java:1627)[:1.7.0_45]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2249)[org.apache.felix.framework-4.0.3.redhat-60024.jar:]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)[org.apache.felix.framework-4.0.3.redhat-60024.jar:]
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)[org.apache.felix.framework-4.0.3.redhat-60024.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)[org.apache.felix.framework-4.0.3.redhat-60024.jar:]
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)[:1.7.0_45]
at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1862)[org.apache.felix.framework-4.0.3.redhat-60024.jar:]
at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:937)[org.apache.felix.framework-4.0.3.redhat-60024.jar:]
at org.apache.aries.blueprint.container.BlueprintContainerImpl.loadClass(BlueprintContainerImpl.java:419)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BlueprintRepository.loadClass(BlueprintRepository.java:410)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.GenericType.parse(GenericType.java:113)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.di.AbstractRecipe.doLoadType(AbstractRecipe.java:168)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.di.AbstractRecipe.loadType(AbstractRecipe.java:161)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BeanRecipe.loadClass(BeanRecipe.java:249)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BeanRecipe.getType(BeanRecipe.java:895)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BeanRecipe.getInstance(BeanRecipe.java:323)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:806)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:787)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:106)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:245)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BlueprintRepository.createInstance(BlueprintRepository.java:230)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BlueprintRepository.create(BlueprintRepository.java:145)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
at org.apache.aries.blueprint.container.BlueprintContainerImpl.getComponentInstance(BlueprintContainerImpl.java:745)[13:org.apache.aries.blueprint.core:1.0.1.redhat-60024]
... 9 more
似乎蓝图正在尝试在两个不同的线程中初始化服务实例,并且当第二个线程到达 OSGi 容器(Felix)时,面临这个错误。因为包已经被第一个线程加载了。
正如我所提到的,这不是永久性的行为,并且会发生 10-20% 的时间。
有人对原因和潜在解决方案有任何想法吗?