我有一个OSGi包,它有一个引用监听器类,可以跟踪其他OSGi包注册的服务。我已经使用蓝图配置了参考监听器,如下所示,并在Felix中部署了所有必要的Aries包:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0">
<reference-list id="ServiceList" interface="com.test.MyService" availability="optional">
<reference-listener bind-method="bind" unbind-method="unbind">
<bean class="com.test.MyServiceMonitor" />
</reference-listener>
</reference-list>
</blueprint>
一切都适用于此配置,并且当服务来来往往时,会调用MyServiceMonitor类的bind和unbind方法。作为下一步,我希望bind方法也在数据库中写入一些数据,所以我尝试在MyServiceMonitor中注入一个实体管理器,并通过更改bean标记创建一个事务,如下所示:
<bean class="com.test.MyServiceMonitor">
<tx:transaction method="bind" value="RequiresNew" />
<jpa:context property="entityManager" unitname="myunit" />
</bean>
当我启动bundle时,尝试为bind方法创建事务会导致下面的异常。当我删除tx:transaction标记时,捆绑包启动没有问题,而我能够使用具有相同蓝图配置的其他(非参考监听器)bean中的实体管理器和事务。
相对较新的OSGi我无法弄清楚这是否是预期的行为,如果是,在参考监听器中执行数据库操作的正确方法是什么。对此有何见解?
2013-07-11 12:40:12,592 | ERROR | 20 - org.apache.aries.blueprint - 1.1.0 | org.apache.aries.blueprint.container.BlueprintContainerImpl | Unable to start blueprint container for bundle com.test.my-bundle
org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to convert value BeanRecipe[name='#recipe-2'] to type class java.lang.Object
at org.apache.aries.blueprint.di.CollectionRecipe.internalCreate(CollectionRecipe.java:92)
at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:106)
at org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe.createListeners(AbstractServiceReferenceRecipe.java:246)
at org.apache.aries.blueprint.container.ReferenceListRecipe.internalCreate(ReferenceListRecipe.java:74)
at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:79)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:88)
at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:245)
at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:183)
at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:668)
at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:370)
at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:261)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at org.apache.aries.blueprint.container.ExecutorServiceWrapper.run(ExecutorServiceWrapper.java:106)
at org.apache.aries.blueprint.utils.threading.impl.DiscardableRunnable.run(DiscardableRunnable.java:48)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.osgi.service.blueprint.container.ComponentDefinitionException: Error setting property: PropertyDescriptor <name: listener, getter: null, setter: [class org.apache.aries.blueprint.container.AbstractServiceReferenceRecipe$Listener.setListener(class java.lang.Object)]
at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:941)
at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:907)
at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:888)
at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:820)
at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:787)
at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:106)
at org.apache.aries.blueprint.di.CollectionRecipe.internalCreate(CollectionRecipe.java:90)
... 25 more
Caused by: org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to create proxy for bean #recipe-3 in bundle com.test.my-bundle version 1.0.0.SNAPSHOT
at org.apache.aries.blueprint.container.BeanRecipe.addInterceptors(BeanRecipe.java:771)
at org.apache.aries.blueprint.container.BeanRecipe.wrap(BeanRecipe.java:841)
at org.apache.aries.blueprint.container.AggregateConverter.convert(AggregateConverter.java:149)
at org.apache.aries.blueprint.container.BlueprintRepository.convert(BlueprintRepository.java:402)
at org.apache.aries.blueprint.utils.ReflectionUtils$PropertyDescriptor.convert(ReflectionUtils.java:394)
at org.apache.aries.blueprint.utils.ReflectionUtils$MethodPropertyDescriptor.internalSet(ReflectionUtils.java:628)
at org.apache.aries.blueprint.utils.ReflectionUtils$PropertyDescriptor.set(ReflectionUtils.java:378)
at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:939)
... 31 more
Caused by: org.apache.aries.proxy.UnableToProxyException: The class com.test.MyServiceMonitor is not an interface and therefore a proxy cannot be generated.
at org.apache.aries.proxy.impl.JdkProxyManager.getInterfaces(JdkProxyManager.java:43)
at org.apache.aries.proxy.impl.JdkProxyManager.createNewProxy(JdkProxyManager.java:36)
at org.apache.aries.proxy.impl.AbstractProxyManager.createDelegatingInterceptingProxy(AbstractProxyManager.java:75)
at org.apache.aries.proxy.impl.AbstractProxyManager.createInterceptingProxy(AbstractProxyManager.java:53)
at org.apache.aries.blueprint.container.BeanRecipe.addInterceptors(BeanRecipe.java:767)
... 38 more
答案 0 :(得分:0)
您的事务管理器正在使用JdkProxyManager来代理事务。内置的JDK代理管理器需要一个接口才能进行代理。最好的办法是建立一个所需方法的接口并实现它。另一种选择是切换到基于ASM,CGLib或JavaAssist的ProxyManager。