【发布时间】:2011-03-17 18:16:54
【问题描述】:
我目前有两个 OSGi 包(bundle1 和 bundle2)都通过 EBA 中的蓝图公开服务。在bundle2 的blueprint.xml 中,我想从bundle1 引用一个服务并将其注入 到BuildService(下面的代码)中,因为BuildService 将用于调用TicketService。然而,这会导致超时异常(也在下面)。似乎 BuildService 永远不会在 OSGi 中注册。我该如何做这样的事情?
blueprint.xml 为bundle1:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:bptx="http://aries.apache.org/xmlns/transactions/v1.0.0">
<bean id="TicketServiceBean" class="com.example.b2.impl.TicketServiceImpl">
<bptx:transaction value="Required" method="*" />
</bean>
<service ranking="0" id="TicketService" interface="com.example.b2.service.TicketService" ref="TicketServiceBean">
<service-properties>
<entry key="service.exported.interfaces" value="*" />
</service-properties>
</service>
</blueprint>
blueprint.xml 为bundle2
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean
id="BuildServiceImplBean"
class="com.example.b1.impl.BuildServiceImpl"
activation="eager" >
<property name="ticketService" ref="TicketServiceRef" />
</bean>
<service
id="BuildService"
ref="BuildServiceImplBean"
interface="com.example.b1.service.BuildService"
activation="eager">
<service-properties>
<entry key="service.exported.interfaces" value="*" />
</service-properties>
</service>
<reference
id="TicketServiceRef"
interface="com.example.b2.service.TicketService"
availability="mandatory"
activation="eager" />
</blueprint>
BuildService 的实现:
public class BuildServiceImpl implements BuildService {
private TicketService ticketService;
@Override
public TicketBuildResponse ticketBuild(TicketBuildRequest ticketBuildRequest) throws BuildServiceException {
//do stuff here
}
public TicketService getTicketService() {
return ticketService;
}
public void setTicketService(TicketService ticketService) {
this.ticketService = ticketService;
}
}
启动应用程序服务器(Websphere)时出现以下异常:
BlueprintCont E org.apache.aries.blueprint.container.BlueprintContainerImpl$1 run Unable to start blueprint container for bundle com.example.b1.module due to unresolved dependencies [(objectClass=com.example.b2.service.TicketService)]
java.util.concurrent.TimeoutException
at org.apache.aries.blueprint.container.BlueprintContainerImpl$1.run(BlueprintContainerImpl.java:273)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:453)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:315)
at java.util.concurrent.FutureTask.run(FutureTask.java:150)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:736)
【问题讨论】:
-
Websphere 是否允许您访问 OSGi 控制台,以便您可以列出部署的捆绑包?另外,您如何部署捆绑包?在 WAB 中?
-
应用程序部署在 EBA 中。 Websphere 确实有一个控制台,您可以在其中查看已部署的工件,但它只允许您查看 eba,而不是深入了解它包含的内容。 RAD 中还有一个 Servers 选项卡,您可以在该选项卡下查看该服务器实例上部署的内容。一切看起来都很好,而且似乎应用程序及其所有捆绑包都已正确部署。
-
同样,如果您能够访问 OSGi 控制台,您可以运行“services”命令来查看服务是否已注册。如果它已注册但 bundle2 仍然无法获取它,那么您可能遇到接口兼容性问题...确保只有一个 TicketService 接口副本,并且 bundle1 和 bundle2 从同一个地方导入它。跨度>