Quartz scheduler and OSGI
I have an OSGI scheduler bundle that has the Quartz Scheduler Jar in it. This bundle exposes just an application interface to other bundles and, when a new job is registered, it is wrapped into a temporaryJob (that implements StatefulJob) and scheduled using the scheduler.
In this way I don't have to expose Quartz Scheduler jar (that it is not so much osgi compliant). The problem with this approach is that, since StatefulJob avoids to execute job in parallel and I have only one actual job (the temporaryJob), all my real jobs run one at a time.
Unfortunately it seems that the marker interface is the only way to say that the job is a stateful one. The only solution I could find is to make the daemon exposing the StatefulJobInterface (removing the fake job), but doing so, I am having a lot of classpath problems. Is there a simpler solution to开发者_开发知识库 this?
Use a real Quartz OSGi bundle, such as the one available here:
http://ebr.springsource.com/repository/app/bundle/detail?name=com.springsource.org.quartz
The latest Quartz version available there is 1.6.2. If you need a newer version, building your own bundle is pretty easy with bnd or bundlor.
Then you can expose StatefulJob as a service anywhere in your OSGi environment, and have your scheduler bundle register and deregister those jobs with Quartz. Even better, have the scheduler bundle listen for any services that are wrappers around your trigger and job information, such as the Spring CronTriggerBean or SimpleTriggerBean. This way,
1) your internal API / OSGi services are not Quartz specific -- only the scheduling bundle has a dependency on the Quartz bundle, and
2) your application bundles can determine the job's schedule instead of the scheduling bundle trying to figure that out.
Update: Newer Quartz OSGi bundles are available from the ServiceMix project: http://repo1.maven.org/maven2/org/apache/servicemix/bundles/org.apache.servicemix.bundles.quartz/
Here you can find newer Quartz OSGI bundles (up to version 2.2.0 at the time of writing):
http://mvnrepository.com/artifact/org.apache.servicemix.bundles/org.apache.servicemix.bundles.quartz
You can install these bundles
bundle:install wrap:mvn:c3p0/c3p0/0.9.1.2
bundle:install mvn:org.quartz-scheduler/quartz/2.2.2
bundle:install wrap:mvn:org.quartz-scheduler/quartz-jobs/2.2.2
You will need these dependency
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.quartz</artifactId>
<version>2.2.2_1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
精彩评论