Java Reference
In-Depth Information
if (fwk != null) {
fwk.stop();
fwk.waitForStop(0);
}
} catch (Exception ex) {
System.err.println("Error stopping framework: " + ex);
}
}
});
...
The JVM shutdown hook mechanism requires a Thread object to perform necessary
actions during process exit; you supply a thread to cleanly stop the framework. When
the shutdown thread executes, you verify that a framework instance was created B
and, if so, you stop it. Because shutting down the framework happens asynchronously,
the call to fwk.stop() returns immediately. You call fwk.waitForStop() to make the
thread wait for the framework to completely stop. It's necessary to have your thread
wait; otherwise, there's a race condition between the JVM process exiting and your
framework stopping.
Using a shutdown hook isn't strictly necessary. The process is in an awkward state
during shutdown, and not all JVM services are guaranteed to be available. There's also
the potential for deadlock and hanging the process. In short, it's a good idea to try to
cleanly shut down the framework, but be aware of the potential pitfalls and do as little
work as possible in the shutdown hook.
Checks if
framework exists
B
13.2.3
Configuring, creating, and starting the framework
In section 13.2.1, you determined which bundles you want to install; all you need now
is a framework instance. The following snippet shows how you create it:
...
Bundle mainBundle = null;
try {
List bundleList = new ArrayList();
Map m = new HashMap();
m.putAll(System.getProperties());
m.put(Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
fwk = getFrameworkFactory().newFramework(m);
fwk.start();
...
You begin by creating a variable to hold a reference to your main bundle, which is a
bundle with a Main-Class entry in its manifest file; we'll come back to this concept
in a couple of sections. After that, you create a list to hold the bundles you success-
fully install.
In the setup for the framework instance, you create a configuration map for it. For
the generic launcher, you copy the system properties in the configuration map as a
convenience and only set one configuration, which cleans the bundle cache on first
initialization. In most cases, you likely won't want to do this; but for the purposes of
Search WWH ::




Custom Search