OSGi 包中使用 com.sun.management.OperatingSystemMXBean

发布于 2024-08-28 12:21:16 字数 318 浏览 9 评论 0原文

我有一些遗留代码用于监视我的应用程序 cpu、内存等,我想将它们转换为捆绑包。现在,当我启动这个包时,它抱怨

Missing Constraint: Import-Package: com.sun.management; version="0.0.0"

我使用了 OperatingSystemMXBean 来访问 JVM 上的统计信息。

我的问题是我可以在 OSGI 容器中使用这个类吗?如果可以的话怎么办?或者我应该使用其他方式来监控我的应用程序。我从 Web 前端对应用程序进行 RMI 调用,以获取 OSGi 之前的节点性能数据。

I have some legacy code that was used to monitor my applications cpu,memory etc that I want to convert to a bundle. Now when i start this bundle its complaining

Missing Constraint: Import-Package: com.sun.management; version="0.0.0"

I had used the OperatingSystemMXBean to get access to stats on the JVM.

My question is can I use this class inside an OSGI container and if so how? Or should I use some other way to monitor my application. I was making an RMI call to the application from a web frontend to get the nodes performance figures pre OSGi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御弟哥哥 2024-09-04 12:21:16

以下是我必须做的才能使其正常工作。

我必须将 com.sun.management 添加到系统捆绑包的 systemProperties 值中,因为我是 OSGI 新手,这花了我一段时间才弄清楚。我使用 maven-pax-plugin 所以我需要添加以下属性。默认情况下它不起作用的原因是,我选择的 osgi 容器默认不包含系统捆绑包中的 com.sun.* 包。

通过使用bundle 0 命令查看系统捆绑包,这一点很明显,因为捆绑包 0 始终是系统捆绑包,这对我来说是新的东西。

<param>--sp=com.sun.management</param>

添加此命令后,系统捆绑包包括 com.sun.management 并且我的捆绑包部署没有任何问题。

默认情况下,Equinox 在 systemProperties 中不包含 com.sun 包的原因请参见 此处。 (直接调用 sun.* 包的 Java 程序不能保证在所有 Java 兼容平台上工作。事实上,即使在同一平台上的未来版本中,也不能保证这样的程序也能工作。)

因此,您有两个选择用于将 com.sun 添加到 osgi 容器。

解决方案 A':扩展包

这些充当片段;它们不是自己的捆绑包,而是附加到主机上。扩展捆绑包是一种特殊的片段,仅附加到系统捆绑包,以便提供框架的可选部分。人们可以使用这种机制来创建一个空扩展,该扩展仅声明所需的包,而将加载留给其托管包(在本例中为框架)。我没有选择这条路线,因为第二个选项实施起来更快。

解决方案 B:引导委派

我最终选择的选项是引导委派。这允许用户创建将始终由框架父类加载器加载的“隐含”包,即使包不提供正确的导入也是如此。我通过将系统包设置为包含 com.sun.management 来实现。

请参阅以下优秀的链接更详细地描述了整个问题。

The following is what I had to do in order to get this working.

I had to add com.sun.management to the systemProperties value for the system bundle, as I was new to OSGI this took me a while to figure out. I use the maven-pax-plugin so i needed to add the following property. The reason it didn't work by default was equinox my osgi container of choice does not include the com.sun.* packages in the system bundle by default.

This was obvious by looking at the system bundle with the bundle 0 command as bundle 0 is always the system bundle which was something new to me.

<param>--sp=com.sun.management</param>

after adding this command the system bundle includes com.sun.management and my bundle deployed with no issues.

The reason that equinox doesn't include the com.sun packages in the systemProperties by default see here. (A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.)

So you have two options for adding com.sun to the osgi container.

Solution A': Extension Bundles

These act as fragments; they are not bundles of their own but rather are attached to a host. Extension bundles are a special kind of fragments that get attached only to the System bundle in order to deliver optional parts of the Framework. One can use this mechanism to create an empty extension that just declares the needed packages, leaving the loading to its hosting bundle (in this case the Framework). I didn't go for this route as the second option was quicker to implement.

Solution B: Boot Delegation

The option I went for in the the end was boot delegation. This allows the user to create 'implied' packages that will always be loaded by the framework parent class loader, even if the bundles do not provide the proper imports. I achieved by setting the system packages to include com.sun.management.

See the following excellent link that describes the whole issue in more detail.

挽清梦 2024-09-04 12:21:16

您可以尝试在交互式 OSGi 会话中安装它吗?
例如,请参阅本文

osgi> ss

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900

osgi>  install file:bundles/FirstBundle-1.0.0.jar
Bundle id is 1

//Try starting 
osgi> start 1
org.osgi.framework.BundleException: The bundle could not be resolved. 
  Reason: Missing Constraint: Import-Package: com.so.samples.osgi.second; 
                                              version="0.0.0"
 at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker
    (BundleHost.java:305)

您可以诊断问题:

osgi> diag 1
file:bundles/FirstBundle-1.0.0.jar [1]
  Direct constraints which are unresolved:
    Missing imported package com.so.samples.osgi.second_0.0.0.

并安装缺少的依赖项,前提是您知道在哪里获取 jar
(这很可能是你问题的症结所在,对此我没有确切的答案,除了转换 OSGi 包中的遗留 jar,比如 包装协议OSGi 框架的扩展):

osgi> install file:bundles/SecondBundle-1.0.0.jar
Bundle id is 2

Could you try to install it in an interactive OSGi session?
See this article for example.

osgi> ss

Framework is launched.

id State       Bundle
0 ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900

osgi>  install file:bundles/FirstBundle-1.0.0.jar
Bundle id is 1

//Try starting 
osgi> start 1
org.osgi.framework.BundleException: The bundle could not be resolved. 
  Reason: Missing Constraint: Import-Package: com.so.samples.osgi.second; 
                                              version="0.0.0"
 at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker
    (BundleHost.java:305)

You can diagnostic the issue:

osgi> diag 1
file:bundles/FirstBundle-1.0.0.jar [1]
  Direct constraints which are unresolved:
    Missing imported package com.so.samples.osgi.second_0.0.0.

And install the missing dependency, provided you know where to fetch the jar
(which might very well be the crux of your question, and for which I have no exact answer, except to convert a legacy jar in an OSGi bundle, like a wrap protocol or an extension of an OSGi framework):

osgi> install file:bundles/SecondBundle-1.0.0.jar
Bundle id is 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文