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は、2 番目のスニペットとは異なるライフサイクル フェーズで呼び出されます。

PrintServiceLookup最初の例では、Container または Extender は、いつlookupPrintServices呼び出されるかについてすべての依存関係を満たしていない可能性があります。

lookupPrintServices2 番目の例では、バンドル アクティベーターのメソッドで呼び出されるため、これらの依存関係が満たされる可能性があります。このstartメソッドは、STARTING フェーズでコンテナーによって呼び出されます。STARTING フェーズでは、バンドルのすべての依存関係がコンテナーによって既に解決されています。

私が助けてくれることを願っています。

于 2015-06-22T07:32:38.137 に答える