Let's begin by stating that you probably do not need to know when other modules are loaded or unloaded. The module system takes care of dependency management for you, so your module should never be loaded unless all of its stated dependencies are loaded too.
The normal means of communicating between modules about available services (which you could consider indirect dependencies) is using the Lookup API. If what you really wanted was to know when a service became available, you do not need to listen to module load or unload events as such; which module hosts the service is not of direct interest. Instead:
If you simply want to run some code when your module loads or unloads, registering an instance of ModuleInstall will do the trick. There is even a wizard in the IDE for setting this up.
But in the very rare case that you want to be notified when other modules are loaded or unloaded, it is possible because:
By using these two facts together, it is possible to listen to changes in the installed modules by running code like this at some point in the application's lifecycle:
final Lookup.Result<ModuleInfo> result = Lookup.getDefault().lookupResult(ModuleInfo.class);
result.addLookupListener(new LookupListener() {
public void resultChanged(LookupEvent event) {
// it seems a module was installed or removed
}
});
Once you detect that a module has been created you may also want to register a PropertyChangeListener and listen to ModuleInfo.PROP_ENABLED. (A module present in the installation will provide a ModuleInfo but isEnabled might be false if it is not currently loaded.)