【问题标题】:OSGI - Find Bundle Reference By Service IDOSGI - 按服务 ID 查找包引用
【发布时间】:2015-02-19 20:02:18
【问题描述】:

我正在使用一个参考侦听器,在我的侦听器方法中带有 Service、Dictionary 参数。

Dictionary 有一个 service.id,那么我需要有那个 Service 的 Bundle。

我的问题是,有了服务和服务 ID,我如何获取 Bundle 对象。

例子:

<reference-listener
            bind-method="bindFormProcessor"
            unbind-method="unbindFormProcessor"
            ref="mainSvc"
            >

    </reference-listener>

我的 Java 方法

public void bindFormProcessor(IFormProcessor formProcessor,Dictionary dictionary) {
    try {
        Bundle bundle = OsgiUtil.getBundleByObject(dictionary.get("service.id"));
        logger.info("************************ GOOOAAL " + bundle.getSymbolicName());
        //Now, I have the bundle of my Service
    } catch (Exception e) {
        e.printStackTrace();
    }

}

问候,

【问题讨论】:

    标签: java osgi blueprint-osgi


    【解决方案1】:

    你可以定义ServiceReference作为绑定方法的参数。通过使用它,您可以获得提供服务的捆绑包。

    如果不想将 ServiceReference 定义为参数类型(无论出于何种原因),可以通过以下代码获取:

    ServiceReference serviceRef = bundleContext.getServiceReferences(IFormProcessor.class, "(service.id=" + serviceId + ")");
    

    【讨论】:

    • 谢谢 Balazs,我不知道过滤器参数,我想使用 ServiceReference 参数。但是当我使用该 ServiceReference 检索服务时,我遇到了 Cast Exception 问题。非常感谢
    猜你喜欢
    • 2014-05-15
    • 2014-04-23
    • 2015-07-20
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2016-02-09
    • 2013-02-24
    相关资源
    最近更新 更多