<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<!--
  - Scheduler context for Quartz. Schedules "list images" and "check images" jobs.
  - Note that Quartz Jobs are throwaway objects, in contrast to shared TimerTasks.
  -->
<beans>

	<!-- Quartz Scheduler, with pre-registered triggers -->
	<!-- Will automatically start scheduling on context startup -->
	<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="listImagesTrigger"/>
				<ref local="checkImagesTrigger"/>
			</list>
		</property>
	</bean>

	<!-- Job definition for ListImagesQuartzJob -->
	<!-- Lists the images in the image database and sends a corresponding mail -->
	<bean id="listImagesJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="org.springframework.samples.imagedb.scheduling.ListImagesQuartzJob"/>
		<property name="jobDataAsMap">
			<map>
				<entry key="imageDatabase" value-ref="imageDatabase"/>
				<entry key="mailSender" value-ref="mailSender"/>
				<entry key="mailFrom" value="${mail.from}"/>
				<entry key="mailTo" value="${mail.to}"/>
			</map>
		</property>
	</bean>

	<!-- Trigger for the job defined above -->
	<!-- Registered by the 'scheduler' bean -->
	<bean id="listImagesTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="listImagesJobDetail"/>
		<property name="startDelay" value="10000"/>
		<property name="repeatInterval" value="10000"/>
	</bean>

	<!-- Job definition that delegates to the specified 'imageDatabase' method -->
	<!-- Convenient way of invoking existing business logic instead of writing a job -->
	<bean id="checkImagesJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="imageDatabase"/>
		<property name="targetMethod" value="checkImages"/>
	</bean>

	<!-- Trigger for the job defined above -->
	<!-- Registered by the 'scheduler' bean -->
	<bean id="checkImagesTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="checkImagesJobDetail"/>
		<property name="cronExpression" value="0/5 * * * * ?"/>
	</bean>

</beans>
