2016-01-22 59 views
1

我是Apache Karaf的新手,我正在部署一个简单的捆绑软件,它在启动时打印日期。我已经使用Activator类实现了它,它调用Activator启动方法并打印出日期。我正在阅读的是,如果没有Activator并使用蓝图注册我的服务,我可以实现另一种方法。Apache Karaf捆绑软件注册监听器注册方法永远不会触发

我已经写了包,并在Karaf容器部署它,但它并没有打印出日期,下面所描述的甚至从来都没有调用注册方法:

<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
       xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> 
     <bean id="timeGreetServiceImpl" class="com.cengage.wiring.TimeGreetServiceImpl"> 
      <argument ref="timeService"/> 
      <argument ref="sampleService"/> 
     </bean> 
     <bean id="serviceLaunch" class="com.cengage.wiring.ServiceLaunch"></bean> 
     <reference id="sampleService" activation="eager" availability="mandatory" interface="com.cengage.api.SampleService"> 
     </reference> 
     <reference id="timeService" activation="eager" availability="mandatory" interface="com.cengage.register.api.SimpleTimeService"> 
     </reference> 
     <service id="timeGreetService" interface="com.cengage.wiring.TimeGreetService" ref="timeGreetServiceImpl"> 
      <registration-listener ref="serviceLaunch" registration-method="register" unregistration-method="unregister"/> 
     </service> 
    </blueprint> 

这是我的POJO类,它有注册方法

public class ServiceLaunch { 

    public void register(final TimeGreetService service) { 
     System.out.println("TimeGreetService registered - output: " 
       + service.print()); 
    } 

    public void unregister() { 

    } 
} 

有人可以告诉我我错过了什么吗?卡拉夫没有错误或日志。

谢谢

尝试2: 正如我改变了我的蓝图的意见要求,但它仍然没有命中寄存器方法

<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
         xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> 
<bean id="timeGreetServiceImpl" class="com.cengage.wiring.TimeGreetServiceImpl"> 
    <argument ref="timeService"/> 
    <argument ref="sampleService"/> 
</bean> 
<bean id="serviceLaunch" class="com.cengage.wiring.ServiceLaunch"></bean> 
<reference id="sampleService" activation="eager" availability="optional" interface="com.cengage.api.SampleService"> 
</reference> 
<reference id="timeService" activation="eager" availability="optional" interface="com.cengage.register.api.SimpleTimeService"> 
</reference> 
<service id="timeGreetService" interface="com.cengage.wiring.TimeGreetService" ref="timeGreetServiceImpl"> 
    <registration-listener ref="serviceLaunch" registration-method="register" unregistration-method="unregister"/> 
</service> 
</blueprint> 
+0

请忽略我的第一个答案。我以为你引用了你发布的同样的服务。情况并非如此,因此可选不是必需的。 –

+0

您可以使用以下服务查看karaf中的服务:列表命令。它显示你依赖的服务,也可能是你发布的服务? –

+0

也许问题在于你试图注册一个类型为'TimeGreetService'的服务,但是你的类没有实现'TimeGreetService'。此外,为什么你关心你自己的服务注册的确切时间?为什么不去一个简单的激活方法? –

回答

1

尝试移除注册监听器。然后,您可以使用karaf服务:list命令来检查服务是否已注册。

当初始化服务类时,可以使用init-method和destroy-method属性来获取回调。我从未在所有现实生活蓝图中看到过注册倾听者。所以可能你不会需要它。

如果您需要更多蓝图示例,请查看karaf-tutorials

<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
      xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> 
    <bean id="timeGreetServiceImpl" class="com.cengage.wiring.TimeGreetServiceImpl"> 
     <argument ref="timeService"/> 
     <argument ref="sampleService"/> 
    </bean> 
    <bean id="serviceLaunch" class="com.cengage.wiring.ServiceLaunch"></bean> 
    <reference id="sampleService" activation="eager" availability="mandatory" interface="com.cengage.api.SampleService"> 
    </reference> 
    <reference id="timeService" activation="eager" availability="mandatory" interface="com.cengage.register.api.SimpleTimeService"> 
    </reference> 
    <service id="timeGreetService" interface="com.cengage.wiring.TimeGreetService" ref="timeGreetServiceImpl"/> 
</blueprint> 
+0

非常感谢你! – alyn000r

1

蓝图的解决方案似乎是非常令人费解,实现什么应该是一个简单的目标:注册一项服务并在激活时获得回叫。

以下是声明式服务的代码示例,做什么,我相信你正在尝试做的:

@Component 
public class TimeGreetServiceImpl implements TimeGreetService { 

    @Activate 
    void activate() { 
     System.out.printf("TimeGreet service registered, time now is %s%n", printTime()); 
    } 

    @Override 
    public String printTime() { 
     // ... 
    } 

} 

注意,当你运行它,您的组件将不会被直到服务消费者尝试使用激活TimeGreetService服务。这是因为默认情况下DS是懒惰的。如果您愿意,可以将immediate=true放入@Component注释的属性中进行更改。

+0

嗨,谢谢你的回答。然而,我正在努力学习蓝图方法,因为那是我工作中使用的方法。我会研究这个方法,但是你知道任何解释这个的教程吗?关于这个时间,这并不重要,这只是我所追求的一个例子。 – alyn000r