4

我目前有两个 OSGi 包 (bundle1bundle2) 都通过 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)
4

1 回答 1

5

这是解决方案:OSGi 应用程序运行时处理远程服务与本地服务不同,因为不同的默认调用语义(本地传递引用与远程传递值)。为了防止应用程序意外调用仅为按值传递调用而设计的导出服务,它对本地查找隐藏。

解决这个问题的方法是将同一个 bean 导出两次,一次用于远程调用,第二次用于本地调用。换句话说,您将添加另一个<service />具有相同配置但没有service.exported.interfaces属性的元素。

<service ranking="0" id="TicketServiceExport" interface="com.example.b2.service.TicketService" ref="TicketServiceBean">
    <service-properties>
        <entry key="service.exported.interfaces" value="*" />
    </service-properties>
</service>  

<service ranking="0" id="TicketService" interface="com.example.b2.service.TicketService" ref="TicketServiceBean"/>

在 websphere 中实际上还有一个 osgi 控制台,可以在[local websphere installation]/profiles/[profileName]/bin/osgiApplicationConsole.bat. 启动后,help()会为您提供命令列表。要查看您从 SCA 导入的服务,您首先连接到您的应用程序(例如connect(2),在list()命令结果中给出了应用程序的编号)。然后您可以services("(service.imported=true)")查看 SCA 添加的服务代理。该命令services()将列出应用程序中的所有服务。

于 2011-03-18T20:51:44.800 回答