zoukankan      html  css  js  c++  java
  • 定时发送邮件(利用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>

     

  • 相关阅读:
    linux软件安装方式
    docker 安装 jenkins touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
    [ERR] Node goodsleep.vip:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
    Linux 常用命令 服务器间scp 用户 export 创建文件、软连接
    redis 安装 集群 主从 哨兵 docker
    WPF密码框中禁止复制、粘贴
    Application 统计在线人数
    【转义字符】HTML 字符实体&lt; &gt: &amp;等
    SQL语句统计每天的数据
    正则表达式计算代码数
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/7091623.html
Copyright © 2011-2022 走看看