SyntaxHighlighter

Friday, December 10, 2010

Spring Quartz Priority Job Queue

Following post describes how to build a Priority Quartz Job queue.

Basic and most important factor in this is to define (Spring's SchedulerFactory bean )Quartz scheduler with single worker thread.

This can be defined in spring configuration as follows
 <bean id="springScheduler"
                class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
                <property name="applicationContextSchedulerContextKey" value="applicationContext">property>
                <property name="quartzProperties">
                        <props>
                                <prop key="org.quartz.threadPool.threadCount"> 1 prop>
                        props>
                property>bean> 

As worker thread count is 1; this scheduler can run a one job at a time.

Now will write a Queue Job; which will be taking care of priority while executing individual jobs.

QueueJobBean.java is extension to spring's QuartzJobBean and internally maintains a TreeMap containing job names (spring bean names) with priorities.

<bean name="jobQueue" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="scheduler.QueueJobBean" />
        <property name="jobDataAsMap" ref="prioMap">property>
bean>

We can build a Priority Job map within spring bean configuration as below
<bean id="noOperation" class="scheduler.jobs.NoOpJob" >
    bean>
   
    <util:map id="prioMap" map-class="java.util.TreeMap">
        <entry key="1" value="noOperation"/>
        <entry key="2" value="noOperation"/>
        <entry key="3" value="noOperation"/>
    util:map>

Cheers,
Dhananjay

1 comment:

danidani2 said...

Nice article. Very usefull.

Did You know that, I also implemented an improvement of quartz ? here it is