spring整合Quartz实现定时发送邮件配置学习总结

文章目录

    • Quartz依赖
    • spring配置
    • job工具类

Quartz依赖

<dependency>
	<groupId>org.quartz-schedulergroupId>
	<artifactId>quartzartifactId>
	<version>2.2.3version>
dependency>

spring配置


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="mailJob" class="pdsu.xps.erp.job.MailJob">
		<property name="storedetailBiz" ref="storedetailBiz">property>
	bean>
	
	<bean id="sendStoreAlertMailJobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFac
toryBean">
		<property name="targetObject" ref="mailJob">property>
		<property name="targetMethod" value="sendStoreAlertMail">property>
	bean>
	
	<bean id="mailTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="sendStoreAlertMailJobDetail">property>
		
		<property name="cronExpression" value="0 17 12,16 * * ?">property>
	bean>
	
	<bean id="startQuartz"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="mailTrigger" />
			list>
		property>
	bean>
beans>

job工具类

/**
 *	 邮件相关任务
 *	 后台定时检测库存预警
 *	 如果存在库存预警,则发预警邮件给相关工作人员
 * @author Administrator
 */
public class MailJob {
	// 商品库存业务
	private IStoredetailBiz storedetailBiz;

	public void setStoredetailBiz(IStoredetailBiz storedetailBiz) {
		this.storedetailBiz = storedetailBiz;
	}

	/**
	 * 发送库存报警邮件
	 */
	public void sendStoreAlertMail() {
		try {
			storedetailBiz.sendStoreAlertMail();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(Quartz)