首页 > 代码库 > 定时发送邮件(利用quart)

定时发送邮件(利用quart)

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目

思路:1.依赖 2.任务 3.配置文件 4.测试

1.依赖

	<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.3</version>
		</dependency>
	</dependencies>

  

2.任务类 

class MailJob{

private IStoredetailBiz storedetailBiz;
	private MailUtil mailUtil;//邮件工具
	private String to;//收件人
	private String subject;//邮件标题
	private String text;//邮件内容
	
	/**
	 * 发送预警邮件任务的方法
	 */
	public void doJob(){
		//获取预警的库存列表
		List<Storealert> list = storedetailBiz.getStorealertList();
		if(null != list && list.size() > 0){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				//发送邮件
				mailUtil.sendMail(to, subject.replace("[time]", df.format(new Date())), 
						text.replace("[count]", list.size() + ""));
				System.out.println("邮件发送成功!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

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

	public void setMailUtil(MailUtil mailUtil) {
		this.mailUtil = mailUtil;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setText(String text) {
		this.text = text;
	}
}
}

 

3.配置文件

//3配置文件
<!-- 定义一个任务类 -->
    <bean id="mailJob" class="cn.itcast.erp.job.MailJob">
        <property name="mailUtil" ref="mailUtil"></property>
        <property name="storedetailBiz" ref="storedetailBiz"></property>
        <property name="to" value="erik2010163@163.com"></property>
        <property name="subject" value="【Auto-Mail】库存预警_时间:[time]"></property>
        <property name="text" value="亲!有[count]种商品已经库存不足,请登陆ERP系统查看"></property>
    </bean>
    <!-- 任务类描述 -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="mailJob"></property>
        <property name="targetMethod" value="doJob"></property>
        <!-- 去掉并发执行 -->
        <property name="concurrent" value="false"/>
    </bean>
    <!-- 触发器  -->
    <bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <!-- 七子表达式: -->
        <property name="cronExpression" value="0/30 * * * * ? *"></property>
    </bean>
    <!-- 任务调度管理容器 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        <property name="triggers">
            <list>
                <ref bean="jobTrigger"/>
            </list>
        </property>
        <!-- 跳过 新版本的检查 -->
        <property name="quartzProperties"> 
            <props>
                <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop> 
            </props>
        </property>
    </bean>
    
</beans>

 

 

定时发送邮件(利用quart)