Spring Quartz 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>
method of perticular class to be executed as configured 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.. }
//...
}
If trigger time is very less it may not execute method completely, because before getting the response next trigger may raise.
request passing to DBS but not geeting back and executing if{} code. can any one can fix this.
using annotations.