java - 使用 Java 蓝图服务定义

标签 java karaf blueprint

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

如果我删除引用线, bundle 将启动并具有可用的 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
----

最佳答案

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

引用的粗略比喻是从另一个包中导入Java。服务类比为公共(public)类。 如果您想使用不同包中的类,则需要导入(=引用)。

在您的示例中,您不需要引用,因为您在 bundle 中。您可以直接引用您声明的 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>

附:如果没有其他包需要您的 LookupMfgService,那么您甚至可以省略服务声明。

关于java - 使用 Java 蓝图服务定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38818652/

相关文章:

java - Spring 至少必须指定一个基础包异常(exception)

java - 反复获取带有映射规范的 WhiteLabel 错误页面

java - 使用通用数据类型参数将数组传递给函数

sass - 使用不同版本运行 Compass/SASS

azure - 我们可以在管理组级别拥有用户身份吗?

reactjs - 如何在 TypeScript 中使用 React BluePrint 组件?

java - 在 JBoss AS : Can not find a free port for use 中部署 Java EE 应用程序

java - 带蓝图的 Camel 代理

java - 清理 apache-karaf 缓存的命令

linux - Apache Karaf 在 Intel 主板上运行速度较慢