close

此篇文章只是工作上使用紀錄, 完整學習待後面的文章補充

 

工作上需要開發 Eclipse 插件, 想起研究所時有讀過一本 "專業Java整合開發工具 Eclipse 3"

剛好清明返鄉就帶回來了, 重新讀這本書, 也有新體悟

 

裡面提到 OSGi 架構的其中一項優點是讓外掛程是可以隨時被加入/移除 Eclipse 而不需要重啟

這優點令我眼睛為之一亮, 畢竟 Debug 時, 一直重開是很累人的事, 但除了上述的話, 它後續並沒有提到怎麼使用

由於心急+書尚未看完, 先在網路上搜尋了相關主題, 最下面可以看到我參考的文章

1. start OSGi 控制台

    在 Eclipse 啟動時加入 -console 參數, 可在 Eclipse 啟動的同時建立一個 OSGi 控制台

    啟動 OSGi console

                                            (輸入-console)

    OSGi console  

                                                                  (osgi console)

    OSGi 上的各種應用被稱為 OSGi Bundle, 以下列出可執行的操作

命令 說明
start 開始Bundle
stop 停止Bundle
install 安裝指定Bundle
uninstall 卸載指定Bundle
update 更新指定Bundle
active 列出被註冊且處於活動狀態的Bundle
ss 列出所有被註冊的Bundle
diag 診斷Bundle

    Bundle 的狀態顯示為 LAZY 時, 代表 Bundle 還沒有被加載入內存

    關於 Bundle 狀態後續會提到我在測試時遇到的情況

 

2. 利用 OSGi 管理和診斷 Bundle

    在 OSGi 控制台可利用上述的命令來管理 Bundle, 其中有一個命令 diag 可用來診斷一個 Bundle 無法執行的原因

    命令格式:

        1. diag bundle-id

            bundle-id 可以利用 ss(列出所有) or ss bundle-name 來取得資料

        2. diag bundle-name

    診斷 Bundle  

                                                          (diag 命令診斷結果)

 

3. Debug 中如何使用 OSGi

    Debug Configurations 中設定 Eclipse -> Arguments -> Program arguments, 加入 "-console" 指令

    執行 Debug 時開啟一個 OSGi console  

                                                             (Run Configuration)

    執行時, 即可在原本的 Eclipse 的 console 中使用 OSGi 指令來安裝/移除/更新 Bundle

    所以現在開起測試後, 只要直接修改 Bundle 設定, 再使用 OSGi 指令即可看到結果

    利用 OSGi 安裝/卸載 Bundle  

                                                            (嘗試安裝/卸載Bundle)

    上述圖片說明: 測試時發現修改後的 bundle 在 update 後, 直接消失了, 於是查看 bundle 狀態

    發現該 bundle 呈現 INSTALL, 所以用 start 指令來啟動 bundle, 並用 ss bundle-name 確認 start 後的狀態

    顯示為 "474    ACTIVE    bundle-name", 再去 Run-time 的 Eclipse 查看, 這時才看到剛剛更新的 bundle

    ps. Run Configuration 也可做一樣的設定, 我測試時使用的就是 Run Configuration

 

4. 更便利的使用 OSGi

    第三點的敘述中, 需要在 configuration 中設定 -console 參數, 但 Debug 起來的 Run-time 的 Eclipse 自帶了 OSGi 控制台

    只要在它的畫面中打開 console 視圖, 並切換到 Host OSGi 控制台, 即可擁有和第三點一樣的功能

    Window->Show Viw->Console or Window->Show View->Other...->Console

    開啟 Eclipse 自帶 OSGi console

                                               (開啟 console 視圖)

    切換到 Host OSGi  

                                                         (切換到 Host OSGi console)

 

Reference:

    1. OSGi 控制台在 Eclipse 插件開發中的妙用

    2. Eclipse 插件開發之基礎篇(4) OSGi 框架

    3. How to get the host OSGi console for Eclipse

arrow
arrow
    文章標籤
    OSGi Eclipse Host OSGi
    全站熱搜

    Nola 發表在 痞客邦 留言(0) 人氣()