开发者

com.sun.management.OperatingSystemMXBean use in an OSGi bundle

开发者 https://www.devze.com 2022-12-27 05:39 出处:网络
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

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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消