This bundle plugin is an example of how to Schedule Quartz Jobs using an OSGI bundle plugin.
How to create a bundle plugin for Schedule Quartz Jobs
Let’s review the file organization on the Quartz Job (com.dotcms.job)example:
META-INF/MANIFEST.MF: The manifest file is very important for the deployment, it lists the Bundle Name, Version and Packages.
In this MANIFEST you must specify (see template plugin):
- Bundle-Name: The name of your bundle
- Bundle-SymbolicName: A short an unique name for the bundle
- Bundle-Activator: Package and name of your Activator class (example: com.dotmarketing.osgi.job.Activator)
Dynamically add required imports the plugin may need without add them explicitly.
- Import-Package:This is a comma separated list of package's name.List the packages that you are using insidethe bundle plugin and that are exported by dotCMS.
In order to work inside the Apache Felix OSGI runtime, the importand export direc/dA/tive must be bidirectional.
dotCMS must declare the set of packages that will be available to the OSGI plugins by changing the file: dotCMS/WEB-INF/felix/osgi-extra.conf.
This is possible also using the dotCMS UI (System-> Dynamic Plugins -> Exported Packages).
Only after that exported packages are defined in this list,a plugin can Import the packages to use them inside the OSGI blundle.
Under the src folder you will find all the Java classes needed to create a dynamic Quartz Job in dotCMS:
$ ls src/com/dotmarketing/osgi/job/
Activator.java CustomJob.java TestClass.java
Simple Job class that implements the regular Quartz Job interface
This bundle activator extends from com.dotmarketing.osgi.GenericBundleActivator and implements BundleActivator.start().
Will manually register aCronScheduledTaskmaking use of the methodscheduleQuartzJob.
* PLEASE note theunregisterServices()call on thestopmethod, this call is MANDATORY (!) as it will allow us to stop andremove the register Quartz Job when the plugin is undeploy.
There are limitations on the hot deploy functionality for the OSGI Quartz Job plugin, once you upload this plugin you are limitedon what code you can modify for the Quartz Job classes in order to see those changes the next time you upload the plugin.
This will apply only for the OSGI Quartz plugins, exactly to the Quartz Job class you implement and the classes use it by it, becausein order to integrate this plugin with the dotCMS/Quartz code we are using our plugin code outside the OSGI and the plugin context,trying to let know to dotCMS/Quartz that there is a Job outside its classpath trying to be use it and instantiate by them.
In order to support the use of the Quartz Jobs inside ours OSGI plugins we use the java hot swapping, it allows to redefine classes, unfortunately, this redefinition is limited only to changing method bodies:
The redefinition may change method bodies, the constant pool and attributes. The redefinition must not add, remove or rename fields or methods, change the signatures of methods, or change inheritance.
As long as you don’t add, remove or change methods (ONLY the methods bodies) for your Job code you will have an OSGI plugin thatwill reflect you changes when a redeploy is done. If you need to change the signature of the Job classes a restart of the dotCMS app will be require.