【问题标题】:Embed OSGI in war在战争中嵌入 OSGI
【发布时间】:2014-05-11 20:23:06
【问题描述】:

我有兴趣将 OSGI 容器添加到我的 WAR 中,但我找不到有关如何执行此操作的教程或文档。我发现了一些根本没用的东西。 我对 Felix 实现和 Atlassian 实现感兴趣。

我愿意这样做,以便我的 war 接受插件,并且我可以动态扩展我的 Web 应用程序并将其部署到任何 Web 服务器。

任何指向文档或其他内容的链接?任何帮助表示赞赏。

【问题讨论】:

    标签: web-applications osgi war osgi-bundle embedded-osgi


    【解决方案1】:

    我可以看到这是一篇旧帖子,但也许它对某人有用: 这个writing 在这个主题中包含了很多有用的东西,至少对我来说这是一个非常大的帮助。 值得看看该页面上的其他帖子。

    【讨论】:

      【解决方案2】:

      如果您使用 WebLogic 来托管您的应用程序,您可以将 OSGi 包嵌入到您的 WAR 中,并将它们部署到系统定义的 OSGi 服务器。这很好,因为来自 OSGi 日志服务的日志消息可以自动在 WebLogic 日志中看到。此外,当您取消部署应用程序时,您的包将从目标 OSGi 服务器中删除。

      有关详细信息,请参阅 Configuration OSGi containersdeveloping OSGi apps 或此 blog post

      【讨论】:

        【解决方案3】:

        将 OSGi 框架启动器添加到 Web 应用程序并不是什么大问题。

        您需要在 web.xml 中添加一个监听器来启动框架启动器

        <listener>
          <listener-class>at.badgateway.StartupListener</listener-class>
        </listener>
        

        启动监听器可能看起来像这样

        public class StartupListener implements ServletContextListener {
        
        //vars
        
            @Override
            public void contextInitialized(ServletContextEvent event) {
                // set props
            Map<String, String> config = new HashMap<String, String>();
            config.put(Constants.FRAMEWORK_STORAGE, "path to cache");
            config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
        
                try {
                            // get framework and start it
                    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
                    framework = frameworkFactory.newFramework(config);
                    framework.start();
        
                            // start existing bundles
                    bundleContext = framework.getBundleContext();
                    starter = new MyBundleStarter(servletContext, bundleContext);
                    starter.launch();
        
                } catch (Exception ex)  
            }
        
            @Override
            public void contextDestroyed(ServletContextEvent arg0) {
                 // stop framework
            }
        }
        

        注意上面引号中的 MyBundlestarter 类,它是激活包含在你的战争中的所有包的类。 (例如 /WEB-INF/Osgi-Bundles)

        import org.osgi.framework.Bundle;
        import org.osgi.framework.BundleContext;
        public class MyBundleStarter{
        
            private BundleContext bundleContext = null;
        
            public void launch() throws Exception {
        
                ArrayList<Bundle> availableBundles= new ArrayList<Bundle>();
                //get and open available bundles
                for (URL url : getBundlesInWar()) {
                    Bundle bundle = bundleContext.installBundle(url.getFile(), url.openStream());
                    availableBundles.add(bundle);
                }
        
                //start the bundles
                for (Bundle bundle : availableBundles) {
                    try{
                    bundle.start();
                    }catch()
                }
        
            private List<URL> getBundlesInWar() throws Exception {
                // returns a list of URLs located at destination
            }
        }
        

        最后但同样重要的是,您必须将 osgi 框架添加到您的项目中。

            <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            </dependency>
        

            <dependency>
                <groupId>org.eclipse.osgi</groupId>
                <artifactId>org.eclipse.osgi</artifactId>
            </dependency>
        

        【讨论】:

        • Martin.. 你能给我一些文件吗?一些指向允许您执行此操作的 OSGI 实现的链接?等
        • 此代码适用于 equinox 和 felix。只需将 org.eclipse.osgi 或 org.apache.felix.framework 添加到您的 pom 中,它就会被检测到,因为它们包含一个实现 FrameworkFactory-Interface 的类。
        • 太棒了。我回家后会试试的。谢谢
        • 注意:将 felix file-install 添加为 bundle 可能很有用,这增加了在测试环境中添加 bundle 的可能性。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-26
        • 2012-10-29
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        • 2012-08-11
        相关资源
        最近更新 更多