views:

205

answers:

4
+1  Q: 

java scheduler

Which is the best way to run a process in scheduler. I can either do it crontab or Spring-Batch. Any other better option?

+5  A: 

Quartz

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

skaffman
+1  A: 

You can also look at Quartz if you want to schedule tasks in one VM.

If you want to do something periodically as a separate process, I'd go for crontab.

alamar
A: 

I thing that crontab is reasonable. It's mature program used for commercial purposes also.

Other sheduler with GUI task creation is MAESTRO. You could also generate some config files for automation.

bua
A: 

Spring + Quartz is much easier to setup then Spring Batch. But it depends on you application.

http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html

Using Spring + Quartz you can define a MethodInvokingJobDetailFactoryBean that schedules a method call on a bean (that exists in your application context).

In this example orderService.cancelNotPaidOrders() will be called every 30 minutes:

<bean id="cancelExpiredOrders" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     <property name="targetBeanName" value="orderService"/>
     <property name="targetMethod" value="cancelNotPaidOrders"/>
     <property name="concurrent" value="false" />
</bean>

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

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