簡體   English   中英

如何確保OSGi捆綁包以正確的順序加載

[英]How to make sure OSGi bundles load in right order

我有一個以編程方式啟動OSGi容器,加載包然后啟動它們的類。 我遇到的問題是捆綁包的啟動順序不正確。 在某些捆綁包依賴之前,它們會先被加載和啟動。

我如何確保所有捆綁包都以正確的順序啟動,而不是先依賴於它們?

PS: 到目前為止,我對所建議的任何代碼改進都表示歡迎。

謝謝大家。

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

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

public class App {

    public static void main(String[] args) throws BundleException, URISyntaxException {
        App app = new App();
        app.initialize();
    }

    private void initialize() throws BundleException, URISyntaxException {
        Map<String, String> map = new HashMap<String, String>();

        // make sure the cache is cleaned
        map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

        map.put("ds.showtrace", "true");
        map.put("ds.showerrors", "true");

        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
        Framework framework = frameworkFactory.newFramework(map);

        System.out.println("Starting OSGi Framework");
        framework.init();

        File baseDir = new File("target/dist-1.0-SNAPSHOT-bin/plugins/");

        loadScrBundle(framework);

        // get all the Bundles from our plugins directory
        File[] fList = baseDir.listFiles();

        for (File file : fList) {
            if (file.isFile()) {
                System.out.println(file.getAbsolutePath());

                if (file.getName().endsWith(".jar")) {
                    framework.getBundleContext().installBundle(file.toURI().toString());
                }
            } else if (file.isDirectory()) {
                // recurse
            }
        }

        for (Bundle bundle : framework.getBundleContext().getBundles()) {
            bundle.start();
            System.out.println("Bundle: " + bundle.getSymbolicName());
            if (bundle.getRegisteredServices() != null) {
                for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
                    System.out.println("\tRegistered service: " + serviceReference);
            }
        }
    }

    private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
        URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
        if (url == null)
            throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
        String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
        System.out.println("Found declarative services implementation: " + jarPath);
        framework.getBundleContext().installBundle(jarPath).start();
    }
}

如果您編寫依賴於啟動順序的OSGi代碼,那么您就沒有OSGi代碼...在過去的18年中,我認識的任何試圖破壞啟動順序的人在OSGi方面都有很多可預防的問題。 由於在OSGi中,任何捆綁包都可以在任何時候啟動/停止,因此您可能可以在啟動時使其一次運行,但是任何干擾都會殺死您的代碼。

如果您使用OSGi聲明性服務,那么這些問題將不存在。 只需將任何時間依賴關系轉換為服務並依賴該服務即可。

真的,克服它,OSGi中沒有排序...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM