0

我有 CDI+OSGI javase 应用程序。CDI-Weld、OSGI-felix 和 pax-cdi。我在“CDI-main”中有以下代码

@ApplicationScoped
public class Foo{

    public void postCreate(@Observes ContainerInitialized event, BundleContext ctx) throws Exception {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        System.out.println("$Number of print services: " + printServices.length);
        for (PrintService printer : printServices)
            System.out.println("$Printer: " + printer.getName()); 
    }
  }

当我运行这个应用程序时,我会得到以下输出(尽管我有带有正确驱动程序的打印机!)

$打印服务数量:0

注意,第一个符号是 $;如果我将以下代码添加到捆绑激活器并启动它

public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        System.out.println("#Number of print services: " + printServices.length);
        for (PrintService printer : printServices)
            System.out.println("#Printer: " + printer.getName());
    }

    public void stop(BundleContext context) throws Exception {
  }
}

注意,第一个符号是#。然后我的所有打印机都被检测到:

#Number of print services: 1
#Printer: MF3110
Jun 14, 2015 1:47:34 PM org.jboss.weld.bootstrap.WeldStartup startContainer...
....
$Number of print services: 1
$Printer: MF3110

怎么解释?

4

2 回答 2

0

PrintServiceLookup 是在单独的包中定义还是使用来自单独 OSGI 服务的代码?可能与 osgi 服务基数有关吗?

于 2015-06-14T11:53:28.003 回答
0

在您的第一个代码片段中,PrintServiceLookup.lookupPrintServices在与第二个片段不同的生命周期阶段中调用。

在第一个示例中,Container 或 Extender 可能没有满足调用PrintServiceLookup时的所有依赖关系。lookupPrintServices

在第二个示例中,这些依赖关系可能会得到满足,因为lookupPrintServices在 Bundle Activator 的start方法中调用 - 该方法在 STARTING 阶段由容器调用。在 STARTING 阶段,Bundle 的所有依赖项都已由 Container 解析。

希望我能帮上忙。

于 2015-06-22T07:32:38.137 回答