Feeds:
Posts
Comments

Archive for the ‘Quartz’ Category

In one of our current integration projects, where we were using Spring, we had a Spring bean which was constantly running every 20 to 30 seconds pooling the database and fetching new status of records. These statuses would be constantly sent to our client as and when new status came up. This Spring bean is a simple Quartz(From opensymphony) bean of the class MethodInvokingJobDetailFactoryBean.

<bean id="jobBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetObject" ref="queryDatabaseServiceTX" />
     <property name="targetMethod" value="getStatus" />
</bean>

Here the queryDatabaseServiceTX(Spring bean) is the business service with the method getStatus that actually retrieves the status and performs some updates also.

After this we define two more beans in the Spring context

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="simpleTrigger"/>
        </list>
    </property>
</bean>

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail" ref=" jobBean " />
    <property name="startDelay" value="10000" />
    <property name="repeatInterval" value="30000" />
</bean>

Recently we started noticing that multiple threads were instantiated and the business service was called multiple times. This eventually was causing multiple updates to occur. Our design was such that the scheduler should run sequentially, repeating itself every 30 seconds. However sometimes when the database is under too much load the query execution itself takes more than 30 seconds and the next thread is spawned causing dead lock kind of situations.

To overcome this we found a silver bullet. Following was the modification done.


<bean id="jobBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
   <property name="targetObject" ref="queryDatabaseServiceTX" />
   <property name="targetMethod" value="getStatus" />
   <property name="concurrent" value="false" />
</bean>

Setting the attribute concurrent to false ensures that no more threads are spawned until the first one is successfully completed.

Advertisements

Read Full Post »