我是Apache Karaf的新手,我正在部署一个简单的捆绑包,它可以在启动时打印出日期。我已经使用 Activator 类实现了这个目标,它调用了 Activator start 方法并打印出日期。我正在阅读另一种方法,我可以在没有激活器的情况下实现这一点,并使用蓝图注册我的服务。
我已经编写了捆绑包并将其部署在 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>
尝试删除注册侦听器。然后,您可以使用 karaf service:list 命令检查服务是否已注册。
您可以使用 init 方法和销毁方法属性在初始化服务类时获取回调。到目前为止,我从未在我看到的所有现实生活中的蓝图中看到过注册听众。所以你可能不需要它。
如果您需要更多蓝图示例,请查看 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"/>
</blueprint>
蓝图解决方案似乎非常复杂,以实现一个简单的目标:注册服务并在激活时获得回调。
下面是声明性服务中的一个代码示例,它执行我相信您正在尝试执行的操作:
@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 默认是懒惰的。如果您愿意,可以通过在@Component
批注的属性中放置immediate=true
来更改此设置。