user970500
user970500

Reputation: 581

Spring Scheduler for every 24 hrs

I am trying to read CVS file and insert it into database in every 24 hrs.I am using Spring Schedular and its working as expected, but my application will be deployed in 3 different JVM's and all JVM will be running at the same time, so is there any way schedular will only run once for all the three JVM,so that it will insert the data only once per day.

<bean id="runMeTask" 
       class="com.fifththird.ebusiness.ivr.core.service.RunMeTask" >
          <property name="dataSource" ref="DataSource"/>
   </bean>

   <bean id="schedulerTask" 
      class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
        <property name="targetObject" ref="runMeTask" />
        <property name="targetMethod" value="printMe" />
   </bean>

   <bean id="timerTask"
      class="org.springframework.scheduling.timer.ScheduledTimerTask">
        <property name="timerTask" ref="schedulerTask" />
        <property name="delay" value="1000" />
        <property name="period" value="60000" />
  </bean>

 <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="timerTask" />
        </list>
    </property>
</bean>

Adding to above question" If same apllication deployed in three different JVMs will make the call to spring schedular , then code will try to insert the data three times/day."

Upvotes: 3

Views: 702

Answers (3)

user970500
user970500

Reputation: 581

Thanks for your response.I have achived it by using one more table in database which will check the status/day (whether the insert has been done or not ) ,and then insert the values of csv in another table.(This site is rele very awesom).Bcoz from the lock suggestion mentioned above I got this idea.

Upvotes: 0

Daniel Kec
Daniel Kec

Reputation: 549

If I understand this correctly (multiple cvs on multiple machines and one db?) it looks like a use case for JMS Queue we are using Java EE but there are Spring implementations http://static.springsource.org/spring/docs/2.5.3/reference/jms.html

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

You can use a common file as a lock. Only one JVM can aquire lock, others will skip the step. To be on the safe side you need to delete / rename the file after loading finished

    FileOutputStream out = new FileOutputStream("lock");
    try {
        FileLock lock = out.getChannel().tryLock();
        if (lock != null) {
            try {
                // load csv
            } finally {
                lock.release();
            }
        }
    } finally {
        out.close();
    }

Upvotes: 1

Related Questions