Come impostare Time Server per ScheduledTimerTask in primavera?
-
26-10-2019 - |
Domanda
Stiamo usando ScheduledTimerTask per la gestione dei posti di lavoro (automatico) nel nostro application.We stanno utilizzando il seguente codice:
<bean id="SampleTask"
class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject" ref="sampleScheduler" />
<property name="targetMethod" value="runMethod" />
</bean>
<bean id="sampleScheduler" class="com.sample.SampleScheduler" />
<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>
Utilizzando il codice di cui sopra runMethod
sta lavorando bene per ognuno minute.but vorremmo cambiare l'esecuzione in base ad esempio tempo.Per server, se il tempo di server è 6:00 allora abbiamo bisogno di invocare il function.How a raggiungerlo?
Aiuto sarebbe apprezzato.
Soluzione
È possibile utilizzare Task grilletto della primavera per invocare in un periodo di tempo certian, è possibile utilizzare CronTrigger
.
Altri suggerimenti
Se si deve usare solo ScheduledTimerTask, quindi richiamare il compito timer di una volta al minuto. Ora dal all'interno del task timer, controllare se il tempo è 06:00 ed eseguire le funzionalità necessarie. ritorno Else.
Con: Questo compito, che è NO-OP otterrà eseguito per ogni minuto (tranne che per una volta ha a che fare un po 'di elaborazione.
o
Richiamare l'TimeTask solo una volta dalla primavera. Quando si esegue l'operazione, è possibile ottenere l'ora corrente e può calcolare la differenza tra il tempo e il tempo per eseguire l'attività reale attuale. (Lasciate che questa differenza essere chiamato RITARDO).
Ora da con in questo compito timer, iniziare una nuova esecutore Executors.newScheduledThreadPool (1) e scheduleAtFixedRate con ritardo intial = DELAY e impostare il periodo 1 giorno
Spring quarzo SchedulerFactoryBean
applicationContext.xml
<!-- quartz -->
<bean id="emial" class="quartzcore.QuartzEmail"/>
<bean id="myTask" class="quartzcore.MyTask" >
<property name="edao" ref="empdao"/>
<property name="email" ref="emial"/>
</bean>
<!--
<bean id="schedulerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
<property name="targetObject" ref="myTask" />
<property name="targetMethod" value="sayHello" />
</bean>
<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask" ref="schedulerTask" />
<property name="delay" value="5000" />
<property name="period" value="5000" />
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="timerTask" />
</list>
</property>
</bean>
-->
<bean name="quartzJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myTask" />
<property name="targetMethod" value="sayHello" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="quartzJob" />
<property name="repeatInterval" value="1000" />
<property name="startDelay" value="1000" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="quartzJob" />
<property name="cronExpression" value="0/15 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="quartzJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
Il metodo di classe perticular da eseguire come configurato in XML.
public class MyTask {
private EmpDao edao;
private QuartzEmail email;
public static int size = 10;
public void sayHello() {
System.out.println("Hello !!! ");
int currSize = 0;
if ((currSize = edao.emp_count()) != size) {
size = currSize;
System.out.println("####### Records Changed in DB : "+size);
email.sendMail();
}else {
System.out.println("Records not added/removed. "+currSize);
}
(or base on dates)
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String today = dateFormat.format(new Date());
System.out.println("Current Day : "+today);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date()); // Current date
int numOfDays = cal.get(Calendar.DAY_OF_YEAR);
System.out.println("Day of Year : "+numOfDays);
Date edate = dateFormat.parse("27/10/2015");
System.out.println("End Date : "+edate);
cal.setTime(edate); // Future Date
int numOfpdays = cal.get(Calendar.DAY_OF_YEAR);
final int fireTrigger = numOfpdays - numOfDays;
System.out.println("Shedlue : "+fireTrigger);
//if(fireTrigger > 0 && fireTrigger < 10 ){ //send a mail.. }
//...
}
-
Se il tempo di attivazione è molto meno si può non eseguire il metodo completamente, perché prima di ottenere la risposta del trigger successivo può sollevare.
-
richiesta di passare alla DBS, ma non geeting indietro e di esecuzione se {} codice. può uno può risolvere il problema.