Ipotiziamo ad avere il seguente Job
1 2 3 4 5 6 7 8 9 10 | package sample; public class ExampleJob { // properties and collaborators public void doIt() { // do the actual work } } |
Di cui vogliamo eseguire il metodo doIt, ogni tot secondi.
Basta configurare il nostro xml di spring nel seguente modo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!-- definiamo il nostro bean --> <bean id="myJob" class="sample.ExampleJob" /> <!-- dichiariamo spring che vogliamo far eseguire allo scheduler il metodo doIt di myJob --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myJob"/> <property name="targetMethod" value="doIt"/> </bean> <!-- definiamo il trigger che dichiara a spring ogni quanto vogliamo eseguire il nostro metodo --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="myJobDetail"/> <!-- 10 seconds --> <property name="startDelay" value="10000"/> <!-- repeat every 15 seconds --> <property name="repeatInterval" value="15000"/> </bean> <!-- dichiariamo a spring il trigger che abbiamo appena definito --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> <property name="applicationContextSchedulerContextKey"> <value>applicationContext</value> </property> </bean> |
Fletto i muscoli e sono nel vuoto.