Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多个OSGi服务_Java_Service_Osgi_Osgi Bundle - Fatal编程技术网

Java 多个OSGi服务

Java 多个OSGi服务,java,service,osgi,osgi-bundle,Java,Service,Osgi,Osgi Bundle,我是OSGi新手,正在尝试使用OSGi开发应用程序。我有一个OSGi服务,它有一个接口和两个实现 接口:ExportService 实现:ExcelExportServiceImpl,PDFEExportServiceImpl ExportService是我的界面,excelexportserviceinpl,pdfexportserviceinpl是ExportService的实现 我想要ExcelExportServiceImpl和PDFEExportServiceImpl作为两种不同的服务

我是OSGi新手,正在尝试使用OSGi开发应用程序。我有一个OSGi服务,它有一个接口和两个实现

接口:
ExportService

实现:
ExcelExportServiceImpl
PDFEExportServiceImpl

ExportService
是我的界面,
excelexportserviceinpl
pdfexportserviceinpl
ExportService
的实现

我想要
ExcelExportServiceImpl
PDFEExportServiceImpl
作为两种不同的服务

从我的应用程序包中,如果我想使用excel导出,我应该能够调用
ExcelExportServiceImpl
服务,而不涉及
PDFEExportServiceImpl

如何注册两个具有相同接口的不同服务

@Override
public void start(BundleContext context) throws Exception {
        context.registerService(ExportService.class.getName(), new ExcelExportServiceImpl(), null);
        context.registerService(ExportService.class.getName(), new PdfExportServiceImpl(), null);
    }
}
现在,我在activator中找到了上面的代码,但它似乎不起作用,因为这两个服务的类名都是
ExportService.class.getName()
。如何使用一个接口在同一捆绑包中实现两个不同的服务

@Override
public void start(BundleContext context) throws Exception {
        context.registerService(ExportService.class.getName(), new ExcelExportServiceImpl(), null);
        context.registerService(ExportService.class.getName(), new PdfExportServiceImpl(), null);
    }
}
更新/解决方案:

我在我的服务包的activator中更改了以下代码

@Override
public void start(BundleContext context) throws Exception {
        Hashtable excelProperty = new Hashtable();
        excelProperty.put("type", "excel");
        excelServiceRegistration = context.registerService(ExportService.class.getName(), new ExcelExportServiceImpl(), excelProperty);
        Hashtable pdfProperty = new Hashtable();
        pdfProperty.put("type", "pdf");
        pdfServiceRegistration = context.registerService(ExportService.class.getName(), new PdfExportServiceImpl(), pdfProperty);
}
在我的应用程序包中,我添加了下面的过滤器

public static void startBundle(BundleContext context) throws InvalidSyntaxException {
    String EXCEL_FILTER_STRING = "(&(" + Constants.OBJECTCLASS + "=com.stpl.excel.api.ExportService)" + "(type=excel))";
    String PDF_FILTER_STRING = "(&(" + Constants.OBJECTCLASS + "=com.stpl.excel.api.ExportService)" + "(type=pdf))";
    Filter excelFilter = context.createFilter(EXCEL_FILTER_STRING);
    Filter pdfFilter = context.createFilter(PDF_FILTER_STRING);
    ServiceTracker excelService = new ServiceTracker(context, excelFilter, null);
    ServiceTracker pdfService = new ServiceTracker(context, pdfFilter, null);
    excelService.open();
    pdfService.open();
}

上面的代码将使用同一接口注册两个不同的服务。这是正确的

问题是,通过接口绑定服务的使用者将获得这些服务中的一个,并且无法确定哪个是正确的服务

解决此问题的一种方法是为每个服务注册添加属性。例如,您可以在pdf上设置一个proerty type=pdf


然后,客户端可以通过接口和ldap筛选器(type=pdf)绑定服务。然后,它将只匹配pdf ExportService服务。

有没有办法获得服务接口所有实现者的句柄?ServiceTracker始终跟踪所有匹配的服务。您可以使用getServices获取所有实现。一般来说,虽然使用普通的OSGiAPI是非常困难的。因此,您还应该了解应用程序的声明性服务。