问题:
我正在尝试在运行时从我的应用程序代码中从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端口。不幸的是,他们中的任何一个我都没有成功。
org.osgi.service.http.port服务从配置文件org.ops4j.pax.web.cfg读取属性0会产生值0。@Reference HttpService ...)读取HTTP端口不起作用,因为HttpService没有提供获取HTTP端口的方法。@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端口,代码片段如下:
@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();发布于 2022-09-12 12:27:11
在Pax 8中(可从Karaf4.4.0中获得),您可以获取org.osgi.service.http.runtime.HttpServiceRuntime服务的一个实例并检查它的标准osgi.http.endpoint属性。就我而言,它是:
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时
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 = falsehttps://stackoverflow.com/questions/73687978
复制相似问题