我是Apache Karaf的新手,我正在部署一个简单的捆绑包,它在启动时打印出Date。我使用Activator类实现了这一点,它调用了Activator start方法并打印出日期。我正在读到,如果没有Activator并使用蓝图注册我的服务,我可以实现另一种方法。
我已经编写了捆绑包并将其部署在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() {
}
}
有人可以告诉我,我错过了什么? Karaf没有错误或登录。
谢谢
尝试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 :(得分: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
注释的属性中来更改此设置。
答案 1 :(得分:1)
尝试重新注册注册侦听器。然后,您可以使用karaf service: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>