2013-07-21 32 views
0

我正在嘗試學習OSGI。 (主要是捆綁的動態加載和卸載)。OSGI軟件包已安裝並已啓動,但沒有可見的輸出

繼Neil Bartlett的教程How To Embed OSGi後,我將Equinox OSGi框架實現添加到類路徑並啓動了遊戲。

下面的代碼:

import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.ServiceLoader; 

import org.osgi.framework.Bundle; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.BundleException; 
import org.osgi.framework.Constants; 
import org.osgi.framework.launch.Framework; 
import org.osgi.framework.launch.FrameworkFactory; 


public class BundleManager { 

    /** 
    * @param args 
    * @throws Exception 
    */ 
    public static void main(String[] args) throws Exception { 
     // TODO Auto-generated method stub 

     FrameworkFactory frameworkFactory = ServiceLoader.load(
       FrameworkFactory.class).iterator().next(); 
     Map<String, String> config = new HashMap<String, String>(); 
     //TODO: add some config properties 
     Framework framework = frameworkFactory.newFramework(config); 
     framework.start(); 



     BundleContext context = framework.getBundleContext(); 
     List<Bundle> installedBundles = new LinkedList<Bundle>(); 

     installedBundles.add(context.installBundle(
           "file:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar")); 


     for (Bundle bundle : installedBundles) { 

     if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) 
       bundle.start(); 

     } 


     System.out.println("done!!"); 



    } 

} 

是的,它的工作原理。根本沒有錯誤。但是,我在路徑中安裝的是一個jar文件的包:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar在其啓動方法中包含一個「HelloWorld」。我在eclipse控制檯中沒有看到「HelloWold」。爲什麼我看不到那個消息,雖然捆綁包已經啓動了?我感謝任何簡單的幫助。

注:HellowService.jar是我前面創建的插件項目,在其類中的一個實現BundleActivator的在開始添加方法「HelloWorld」的消息,終於導出它作爲一個jar文件的目錄C:/Users/student/Documents/eclipse/myPlugins/

編輯:下面是我安裝的軟件包和啓動我的Activator類:

package com.javaworld.sample.service.impl; 

import org.osgi.framework.BundleActivator; 
import org.osgi.framework.BundleContext; 
import org.osgi.framework.ServiceRegistration; 

import com.javaworld.sample.service.HelloService; 

public class HelloServiceActivator implements BundleActivator { 

    ServiceRegistration helloServiceRegistration; 


     public void start(BundleContext context) throws Exception { 

      HelloServiceFactory helloServiceFactory = new HelloServiceFactory(); 
      helloServiceRegistration =context.registerService(HelloService.class.getName(), helloServiceFactory, null); 

      System.out.println("Hello World!"); 
     } 
     public void stop(BundleContext context) throws Exception { 
      helloServiceRegistration.unregister(); 
     } 


} 

而這裏的包的MANIFEST.MF文件:

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: HelloService 
Bundle-SymbolicName: com.javaworld.sample.HelloService 
Bundle-Version: 1.0.0.qualifier 
Bundle-Activator: com.javaworld.sample.service.impl.HelloServiceActivator 
Bundle-Vendor: JAVAWORLD 
Bundle-RequiredExecutionEnvironment: JavaSE-1.7 
Import-Package: org.osgi.framework;version="1.3.0" 
Export-Package: com.javaworld.sample.service 

我導出包的方式是右鍵單擊bundle項目 - >導出 - >運行jar文件 - >然後我選擇啓動配置爲BundleManager(這是安裝包的類)。

我還沒有看到「Hello World!」當我從我的應用程序啓動包時出現消息。

回答

0

原來我錯誤地導出了這個包。那是因爲我試圖自己做。這裏的包應如何導出爲一個jar文件:

Open the plugin export wizard File > Export... > Plug-in Development > 
    Deployable plug-ins and fragments . 
    Then select the bundle you want to export and the destination directory. Done! 

您現在可以使用的jar文件的路徑安裝包。在我的情況,那就是:

installedBundles.add(context.installBundle(
           "file:C:/Users/student/Documents/eclipse/myPlugins/plugins/com.javaworld.sample.HelloService_1.0.0.201307220322.jar")); 

來源:http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.pde.doc.user%2Fguide%2Ftools%2Fexport_wizards%2Fexport_plugins.htm

而且可以肯定由於@Neil巴特利特

1

您的啓動程序不會等待OSGi Framework停止。我希望這個程序能夠啓動一切,但之後立即關閉,因爲我們到達了方法的末尾。參考我的教程,我將展示如何使用Framework.waitForStop方法。

話雖如此,我希望你的HelloWorld包的輸出實際上出現在關閉之前。因此,該捆綁包中也可能存在錯誤。也許你還沒有宣佈催化劑?我只能猜測,因爲你沒有給出任何細節。

+0

是,尼爾......我不爲一時照顧,當它停止,但我會稍後添加它(現在我添加了它,但我仍然看不到'你好')。聲明激活器是什麼意思?我似乎沒有這樣做。哪裏,我該怎麼做?謝謝。 –

+0

以下是關於Bundle-Activator的更多信息:http://wiki.osgi。org/wiki/Bundle-Activator –

+0

如果你的意思是實現BundleActivator啓動和停止方法,那麼我已經提到過我在我的問題中這樣做了。我沒有在您發送的鏈接中看到任何新內容。這基本上是我開始的。 –

相關問題