To run some code when your module is loaded, and basically every time the IDE starts and your module is enabled, simply create a subclass of org.openide.modules.ModuleInstall and override the restored() method. Be sure to call the superclass, and then you can execute your code. Bear in mind that this is being executing during the time the IDE/platform is starting up. You should limit the work you do here to that which is absolutely necessary.
Once the class is created, you must declare it in your module's manifest.mf file, using the name OpenIDE-Module-Install like so:
(from editor/manifest.mf in the NetBeans source tree)
Likewise, to execute code when the IDE is shutting down, you can override the closed() method. This method of ModuleInstall is called when the IDE is shutting down, contrary to the closing() method, which is called to alert the module that the IDE is about to shut down. However, another module may veto the shutdown by returning false from the closing() method, so the closed() method is best for performing any cleanup work for your module.
Source: NetBeans FAQ