Quartz的spring集成
1.实现Job接口,定义Quartz的Job
java
public class MailJob implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("你的任务执行代码");
}
}
2.quartz.properties
建议参考持久化Quartz的内容,进行编写
3.配置spring
3.1配置jobDetail
java
<bean name="mailJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="cn.com.ultrapower.atp.quartz.MailJob" />
<property name="jobDataMap">
<map>
<entry key="mailIndex" value="xxx" />
<entry key="username" value="xxx" />
<entry key="password" value="123456" />
</map>
</property>
<property name="durability" value="true" />
</bean>
其实上述的代码,与我们写的下面的代码很类似:
java
JobDetail jobDetail = JobBuilder.newJob(UltraJob.class)
.withIdentity(jobName, JOB_GROUP_NAME)
.usingJobData("mailIndex", "xxx")
.usingJobData("username", "xxx")
.usingJobData("password", "123456")
.build()
3.2 配置Trigger
java
<bean id="mailSimpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="name" value="mailTrigger"></property>
<property name="group" value="mailTrigger"></property>
<property name="jobDetail">
<ref bean="mailJobDetail" />
</property>
<property name="startDelay" value="20000"/>
<property name="repeatInterval" value="5000"/>
</bean>
上述代码以SimpleTrigger做示范,稍后会做CronTrigger的
上述代码类似于我们下面的java代码:
java
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, TRIGGER_GROUP_NAME).startNow()
.withSchedule(cronBuilder)
.build()
3.3 配置Scheduler
java
<bean id="ultraSchedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="autoStartup" value="true"></property>
<property name="schedulerName" value="ultraScheduler"></property>
<property name="configLocation" value="/WEB-INF/config/quartz.properties" />
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<property name="triggers">
<list>
<ref bean="mailSimpleTrigger" />
</list>
</property>
</bean>
现在我们已经将所有的spring配置做完了。稍后会写复杂点(持久化)的spring配置。