使用 Java 蓝图服务定义

using a Java blueprint service defintion

我无法获得在本地使用服务的正确蓝图。 OSGi 服务器 (karaf) 显示 GracePeriod 并最终超时等待 ILookupMfgService

如果我删除参考线,捆绑包将启动并有一个 ILookupMfgService 可用。

对我做错了什么有什么建议吗?

感谢您帮助我学习!

蒂莫西

<blueprint default-activation="eager" xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"

    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0 
            http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance 
            http://aries.apache.org/xmlns/jpa/v1.0.0 http://aries.apache.org/xmlns/jpa/v1.0.0 
            http://aries.apache.org/xmlns/transactions/v1.0.0 http://aries.apache.org/xmlns/transactions/v1.0.0
            http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 ">

<!-- implemenation of the service -->
    <bean id="lookupMfgServiceStubImpl" class="com.services.jpa.LookupMfgServiceStub" scope="singleton"
        init-method="init" />

<!-- create a service with the implementation -->
    <service id="lookupMfgServiceStubLocal" ref="lookupMfgServiceStubImpl" interface="com.services.ILookupMfgService" />

<!-- create a reference for injecting into another bean - this line causes the GracePeriod / timeout --> 
    <reference id="lookupMfgServiceStubRef" interface="com.services.ILookupMfgService" availability="mandatory" ctivation="eager" />

    <bean id="EntityImpl" class="com.services.jpa.Entity" scope="singleton" init-method="init">
<!-- use the service - won't work without the reference above being valid -->
        <property name="lookupMfg" ref="lookupMfgServiceRef" />      
    </bean>
 </blueprint>

没有参考线

karaf@root()> services -p 123

com.server.services (123) provides:
----------------------------------------
objectClass = [com.services.ILookupMfgService]
osgi.service.blueprint.compname = lookupMfgServiceStubImpl
service.id = 932
----

您不得声明对同一 blueprint/bundle 中公开的服务的强制引用。容器会感到困惑,因为它想启动一个服务并引用一个尚不存在的服务(他自己),这会导致无法恢复的 GracePeriod。

引用的粗略类比是从另一个包中Java导入。服务的类比是 public class。 如果你想使用来自不同包的 class,你需要导入(=reference)。

在您的示例中,您不需要引用,因为您在您的包中。您可以直接引用声明的 Bean:

<!-- implemenation of the service -->
<bean id="lookupMfgServiceStubImpl" class="com.services.jpa.LookupMfgServiceStub" scope="singleton"
    init-method="init" />

<!-- create a service with the implementation -->
<service id="lookupMfgServiceStubLocal" ref="lookupMfgServiceStubImpl" interface="com.services.ILookupMfgService" />

<bean id="EntityImpl" class="com.services.jpa.Entity" scope="singleton" init-method="init">
<!-- use the service - won't work without the reference above being valid -->
    <property name="lookupMfg" ref="lookupMfgServiceStubImpl" />      
</bean>

p.s。如果没有其他包需要您的 LookupMfgService 那么您甚至可以省略 service 声明。