【问题标题】:How can you access OSGi services from a (potentially remote) web application on JBoss AS 7?如何从 JBoss AS 7 上的(可能是远程的)Web 应用程序访问 OSGi 服务?
【发布时间】:2013-03-15 18:02:04
【问题描述】:

我正在使用 JBoss AS 7。据我了解,它带有 felix 作为 ogsi 容器。我一直将 JBoss 用作普通 Java EE Web 应用程序 (webapp) 的容器。但是,我遇到了很多依赖冲突,我正在重构我的一些代码以成为捆绑包(对于 osgi)。我的问题如下。

  • 我可以从我的 webapp 访问 osgi 服务吗?请注意,webapp 将正常部署,而不是通过 osgi(它不是 webapp 包,又名 wab)。如果是这样,请向我提供一些有关如何执行此操作的参考链接。我看到了相反的例子(从 osgi 包访问 webapp,但我认为 webapp 是作为 wab 部署的)。
  • 是否可以通过 webapp 以编程方式控制捆绑包的生命周期(停止、卸载、启动、安装)?

感谢您的帮助。

【问题讨论】:

    标签: java jakarta-ee jboss osgi


    【解决方案1】:

    从您的 web 应用程序访问 OSGi 服务很容易。

    首先你需要 MANIFEST.MF 中的依赖项,它通常会被部署
    到 webapp/META-INF 文件夹。

    要添加的依赖项是 org.osgi.core 和 org.jboss.osgi.framework 以及您部署的 Bundles 作为 deployment.yourbundle:version。

    例如,您的包名为“yourbundle_1.0.0.1.jar”:

    Manifest-Version: 1.0
    Built-By: me
    Build-Jdk: 1.7.0_09
    Dependencies: org.osgi.core,org.jboss.osgi.framework,deployment.yourbundle:1.0.0.1
    

    将您的捆绑包注册为 Activator 类中的服务(应该已经完成​​):

    public void start(BundleContext bundleContext) throws Exception {
    
        context.registerService(YourBundleService.class.getName(), new YourBundle(),null);
    }
    

    在 JBoss AS 中访问 OSGi BundleContext 需要一个 EJB:

    @Stateless
    public class OSGiServiceBean {
        @Resource
        BundleContext context;
    
        public YourBundleService getBundleService() {
            ServiceReference sref = context.getServiceReference(YourBundleService.class.getName());
    
            return (YourBundleService) context.getService(sref);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 2010-11-11
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多