我找到了一种方法来实现这一点。在这个解决方案中,不是重新创建所有相关服务的实例的Blueprint。它是这样的:
- 连接“看门狗”豆。
而不是创建“ChatProtocolClient”豆,我从XML创建ConnectionWatchDog豆。在这些bean中注入BundleContext,并从.xml文件设置连接属性。 ConnectionWatchDog然后尝试创建/连接ChatProtocolClient实例。如果连接成功,它会在BundleContext中注册一个服务(使用bundleContext.registerService(..))。 ServiceRegistration保存在看门狗中。看门狗在设定的时间间隔内测试连接(它运行它自己的线程)。如果连接似乎失败;看门狗调用serviceRegistration.unregister()并清理客户端连接实例的剩余部分,并且启动创建,连接和注册新的ChatProtocolClient实例的整个过程。
- 的ChatChannel
的ChatChannel现在在蓝图配置有一个。的XML看起来像这样:
<blueprint xmlns=...>
<reference-list id="chat-connection" member-type="service-object" interface="com.example.ChatProtocolClientInterface">
<reference-listener bind-method="onBind" unbind-method="onUnbind" ref="Channel1"/>
</reference-list>
<bean id="Channel1" class="ChatChannel" init-method="startUp">
<property name="chatProtocolClient" ref="chat-connection">
... some other properties ...
</bean>
</blueprint>
的构件型设置为服务对象时,是指当一个服务被注册或未注册时,ChatChannel将与“onBind”和“onUnbind”的方法通知。作为参数,他们将获得一个ChatProtocolClientInterface实例。
我不确定这是唯一的还是最好的解决方案,但它适用于我。请注意,在这个xml例子中,你还需要一个“chatProtocolClient”的setter;目前我不使用blueprint设置的列表,而只使用onBind和onUnbind方法。