Integrating Spring and Obsidian Scheduler
Join the DZone community and get the full member experience.
Join For FreeThe use of Spring framework has woven its way into a large percentage of Java projects of all sorts. It has grown well beyond a dependency injection framework to a large interconnected technology stack providing “plumbing” in a large number of areas. Since it’s so often a part of our lives, we’ll discuss Obsidian’s support of Spring and how to quickly integrate your use of Spring into Obsidian Scheduler.
Version Indifferent
First, Obsidian does not include any Spring libraries and is not dependent on Spring to run. Given that Spring is a very active project and has frequent releases, and at times projects have constraints that require using older versions of certain libraries, we felt it would be best if Obsidian just worked with whatever version of Spring you’re using. Obsidian supports versions 2.5 and later of Spring.
Configuring Job Components
The first step in integrating Obsidian Scheduler is often already in place in your project. Obsidian expects to find @Component
annotations or any Spring or custom extensions of those annotations (such as @Service
and @Repository
) on your job classes. If you’re using these annotations to wire your classes, you likely don’t have to do anything else to complete this step. If you’re not using these annotations to wire your classes, you will need to add the annotation to serve as a Marker to Obsidian. If you happen to have multiple instances of a given bean class in your Spring context, Obsidian will need to know how to uniquely identify the job within the Spring context. In these cases, use value=""
in the annotation to provide the bean name. For example:
@Component("mySpringComponent") public class SpringComponent implements SchedulableJob {
Providing the Spring Context to Obsidian
The next step is to let Obsidian know about your Spring context. Included with Obsidian is an implementation of ApplicationContextAware
. If you’re using classpath scanning in Spring, you can just add the Obsidian package to your configuration. Here is a sample:
<context:component-scan base-package="com.my.package, com.carfey.ops.job.di"/>
If you’re wiring explicitly in a configuration XML file, here is a sample configuration:
<bean id="obsidianAware" class="com.carfey.ops.job.di.SpringContextAware"/>
Startup & Shutdown Options
The third step, which is optional and depends on your desired usage, relates to starting and stopping Obsidian. If you’re configuring Spring integration for use in the Obsidian standalone Web Administration application (with no scheduler running), you would not include this configuration. In all other cases, such as embedded scheduler or Web Administration application with bundled scheduler, you can use this sample:
<bean id="obsidianStarter" class="com.carfey.ops.job.di.SpringSchedulerStarter"/>
This bean should wired as late as possible in the Spring context startup, using depends-on or other appropriate strategies. This class does not possess a @Component
annotation since it cannot be auto-wired via Spring classpath scanning.
Now your Add/Edit Job list in the Web Administration UI, will include all implementations ofcom.carfey.ops.job.SchedulableJob
that you’ve wired through Spring. If you’re using Spring 3.0 or later, even jobs that use Obsidian’s annotationscom.carfey.ops.job.SchedulableJob.Schedulable
andcom.carfey.ops.job.SchedulableJob.ScheduledRun
are included. See our wiki for more information on implementing jobs using the interface or the annotations.
Good to Go!
You now have complete Spring integration with Obsidian. All job instances that loaded via Spring annotations will be loaded from the Spring container when performing validation and execution. Note that these could be either prototypes or singletons according to your needs, though we recommend prototypes if you have any kind of internal state within the job.
Of course Spring is not the only dependency injection framework available in Java. On the roadmap for Obsidian is similar integration with Google Guice. Stay tuned! Get in touch with us if you have any questions on Spring integration or any other Obsidian Scheduler features.
Published at DZone with permission of Craig Flichel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments