首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OPS4J Pax / Apache :在运行时获得动态选择的HTTP

OPS4J Pax / Apache :在运行时获得动态选择的HTTP
EN

Stack Overflow用户
提问于 2022-09-12 10:39:51
回答 1查看 66关注 0票数 1

问题:

我正在尝试在运行时从我的应用程序代码中从OPS4J Pax获取动态选择的HTTP。

设置:

我有一个定制的Apache 4.4.0发行版,它将http功能安装为一个引导功能。http特性使用带有Jetty服务器的OPS4J服务规范的Http实现。根据Pax Web用户指南,配置文件org.ops4j.pax.web.cfg中的属性org.osgi.service.http.port已设置为0,以便Pax自动选择一个HTTP。这一部分如预期的那样工作。

Investigations:

我尝试了以下方法在运行时从不同的来源读取HTTP端口。不幸的是,他们中的任何一个我都没有成功。

  1. 通过org.osgi.service.http.port服务从配置文件org.ops4j.pax.web.cfg读取属性0会产生值0
  2. 通过注入的HttpService (@Reference HttpService ...)读取HTTP端口不起作用,因为HttpService没有提供获取HTTP端口的方法。
  3. 通过注入的WebContainer服务从Pax (@Reference WebContainer ...)读取HTTP端口不起作用,因为WebContainer服务没有提供获取HTTP端口的方法。

当前解决方案:

作为当前的解决方法,我将根Karaf容器的配置文件org.ops4j.pax.web.cfg中的属性org.ops4j.pax.web.cfg设置为固定值。对于所有子Karaf容器,属性被注释掉,HTTP端口由一个小代码片段选择,并在通过InstanceService启动子Karaf容器之前作为JVM参数传递。这允许我将HTTP作为系统属性(System.getProperty(...))读入我的应用程序代码中。

但是,我想利用允许Pax自动选择HTTP端口的可能性,因此可能有人知道如何在运行时获得动态选择的HTTP端口。

在此之前,非常感谢您。

解决方案:

由于Grzegorz Grzybek,我成功地在运行时获得了动态选择的HTTP端口,代码片段如下:

代码语言:javascript
复制
@Reference
private HttpServiceRuntime httpServiceRuntime;
...
Map<String, Object> properties = httpServiceRuntime.getRuntimeDTO().serviceDTO.properties;
String[] localHttpEndpoints = (String[]) properties.get(HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT);
URL localHttpEndpointUrl = new URL(localHttpEndpoints[0]);
int httpPort = localHttpEndpointUrl.getPort();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-12 12:27:11

在Pax 8中(可从Karaf4.4.0中获得),您可以获取org.osgi.service.http.runtime.HttpServiceRuntime服务的一个实例并检查它的标准osgi.http.endpoint属性。就我而言,它是:

代码语言:javascript
复制
objectClass = [org.osgi.service.http.runtime.HttpServiceRuntime]
osgi.http.endpoint = [http://0.0.0.0:44067/]
osgi.http.service.id = [107]
service.bundleid = 70
service.changecount = 0
service.id = 108
service.scope = singleton

当PID属性为0

代码语言:javascript
复制
karaf@root()> property-list --pid org.ops4j.pax.web
   felix.fileinstall.filename = file:/data/servers/apache-karaf-4.4.1/etc/org.ops4j.pax.web.cfg
   javax.servlet.context.tempdir = /data/servers/apache-karaf-4.4.1/data/pax-web/tmp
   org.apache.karaf.features.configKey = org.ops4j.pax.web
   org.osgi.service.http.enabled = true
   org.osgi.service.http.port = 0
   org.osgi.service.http.secure.enabled = false
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73687978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档