Java Reference
In-Depth Information
public void stop(BundleContext ctx) {
MultiThreadedHttpConnectionManager.shutdownAll();
}
}
You have to tell
OSG
i about this activator by adding metadata to the manifest:
Bundle-Activator: org.apache.commons.httpclient.internal.Activator
You can see this in action by building and running the following example:
$
cd chapter06/HttpClient-example
$
ant dist
$
java -jar launcher.jar bundles
You should see it start and attempt to connect to the internet (ignore log4j warnings):
GET http://www.google.com/
GOT 5500 bytes
->
If you use
jstack
to see what threads are running in the
JVM
, one of them should be
"MultiThreadedHttpConnectionManager cleanup" daemon
Stop the HttpClient bundle, which should clean up the thread pool, and check again:
->
stop 5
The
MultiThreadedHttpConnectionManager
thread should now be gone. Unfortu-
nately, this isn't a complete solution, because if you stop and restart the test bundle,
the thread pool manager reappears—even though the HttpClient bundle is still
stopped! Restricting use of the HttpClient library to the bundle active state would
require all calls to go through some sort of delegating proxy or, ideally, the
OSG
i ser-
vice registry. Thankfully, the 4.0 release of the HttpClient library makes it much easier
to manage connection threads inside a container such as
OSG
i and removes the need
for this single static shutdown method.
Bundle activators are mostly harmless because they don't interfere with non-
OSG
i
users of the
JAR
file. They're only referenced via the bundle metadata and aren't con-
sidered part of the public
API
. They sit there unnoticed and unused in classic Java
applications until the bundle is loaded into an
OSG
i framework and started. When-
ever you have a
JAR
file with implicit state or background resources, consider adding
an activator to help
OSG
i users.
We've now covered most aspects of turning a
JAR
file into a bundle: identity, exports,
imports, embedding, and lifecycle management. How many best practices can you
remember? Wouldn't it be great to have them summarized as a one-page cheat sheet?
Look no further than the following section.
6.1.6
JAR file to bundle cheat sheet
Figure 6.5 presents a cheat sheet that gives you a handy summary of converting
JAR
files into bundles.