引用服务停止后终止 OSGI 蓝图包

Terminating OSGI blueprint bundle after stop of referenced service

我有这样的问题。我有两个 OSGI 蓝图包。其中一个就像一项服务,另一个正在使用它。我是 运行 他们在 karaf 上。所以,我想实现功能,所以当我停止服务时,我的其他包也应该停止。 我的 xml 的

    <?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/blueprint.xsd ">

    <reference id="weatherService" availability="mandatory" interface="com.myslyv4uk.weather.api.WeatherService" />  

    <bean id="showWeatherImpl" class="com.myslyv4uk.client.impl.ShowWeatherServiceImpl"
        init-method="start" destroy-method="stop" >
        <argument ref="weatherService" />
    </bean>

</blueprint>

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd ">

    <bean id="weatherServiceImpl" class="com.myslyv4uk.weather.impl.WeatherServiceImpl"
        init-method="start" destroy-method="stop" />

    <service ref="weatherServiceImpl">
        <interfaces>
            <value>com.myslyv4uk.weather.api.WeatherService</value>
        </interfaces>
    </service>  
</blueprint>

Java 代码已跳过。我只想说 ShowWeatherService 使用 Wea​​therService 来打印随机数。他们都有 start/stop 方法。我需要以这种方式实现配置或功能,因此在从 karaf 卸载 WeatherService 包后 ShowWeatherService 也停止了。问题是我不能做从 WeatherService 到 ShowWeatherService 的引用,因为它将循环引用它,这个包将不会启动。我应该怎么办?我如何从其他捆绑包中终止捆绑包?

当此服务出现故障时,我不会停止需要该服务的捆绑包。这不是在 OSGi 中处理的方式。

相反,您的 showWeatherImpl 包可以使用 http 白板模式将自身作为 servlet 提供。这意味着它提供了一个实现 servlet 的服务。如果强制服务引用出现故障,Blueprint 将自动注销捆绑包的所有服务。

当然,如果您在 showWeatherImpl 的 bean 中使用 java 代码将自己注册为 servlet,这当然无济于事。在这种情况下,您可以使用服务引用回调,它会在服务出现和消失时通知您。

当然,就像 Grzegorz 提到的那样,声明式服务在默认情况下比蓝图动态得多,并且可以更好地处理这种情况。

警告

在这个回答中,我解释了如何在所需服务消失时停止蓝图包。此代码按预期工作,但由于各种原因,坏主意


您可以在服务的 bind/unbind 上注册侦听器,并在服务删除时采取行动。这是有关如何操作的示例。

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/blueprint.xsd ">

    <bean id="referenceListener" class="your.ReferenceListener">
        <property name="bundleContext" ref="blueprintBundleContext"/>
    </bean>

    <reference id="weatherService"
               availability="mandatory"
               interface="com.myslyv4uk.weather.api.WeatherService">
        <reference-listener ref="referenceListener"
                            unbind-method="unbind" />
    </reference>

    <bean id="showWeatherImpl" class="com.myslyv4uk.client.impl.ShowWeatherServiceImpl"
        init-method="start" destroy-method="stop" >
        <argument ref="weatherService" />
    </bean>

</blueprint>

移除服务时调用 referenceListener bean。通过注入 bundle 上下文,您可以停止 bundle 本身:

public class ReferenceListener {

private Logger log; // set this up
private BundleContext bundleContext;

public void setBundleContext(BundleContext bundleContext) {
    this.bundleContext = bundleContext;
}

// Called when the service is injected
public void bind(ServiceReference<?> sr) {
    log.info("Bind of ServiceReference {} to bundle {}",
            sr, bundleContext.getBundle());
}

// Called when the service is removed
public void unbind(ServiceReference<?> sr) {
    log.info("Unbind of ServiceReference {} from bundle {}",
            sr, bundleContext.getBundle());
    try {
        if (bundleContext.getBundle().getState() == Bundle.ACTIVE) {
            log.warn("Bundle {} will be stopped after unbind of mandatory ServiceReference {}",
                    bundleContext.getBundle(), sr);
            bundleContext.getBundle().stop();
        }

    } catch (BundleException e) {
        log.error("Cannot stop bundle {} after unbind of ServiceReference {}",
                bundleContext.getBundle().getBundleId(),
                sr,
                e);
    }
}
}

此解决方案有效但有一些缺点,例如,如果您重新启动容器,服务将被删除,从而将捆绑包设置为 STOPPED 状态。