Spring-quartz定时任务

1、在Maven项目加入quartz的jar包
2、新建spring-quartz.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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-4.0.xsd">  
    <!-- default-autowire="byName" default-lazy-init="false"此两个值可以不配置 -->  
    <description>Quartz Job Setting</description>  
  <!-- A.配置调度的任务对应bean的id和自定义class-->  
  <!-- 每天凌晨4点跑,打包数据成2张图片和一个TXT文件 -->
  <bean id="SendMailQuartz" class="cn.cloudwalk.syeb.core.common.quartz.SendMailQuartz" />  
  <!-- B.配置调度任务对应的bean的id和执行的方法,作业不并发调度-->  
  <bean id="SendMailQuartzMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
    <property name="targetObject" ref="SendMailQuartz" />  
    <property name="targetMethod" value="sendMail" />  
    <property name="concurrent" value="false" />  
  </bean>  
  <!-- C.配置调度任务执行的触发的时间-->  
  <bean id="sendMailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  <property name="jobDetail" ref="SendMailQuartzMethod" />  
     <property name="cronExpression">  
     <!-- 每15分钟执行任务调度 -->  
     <!--  <value>0 0/15 * * * ?</value> -->
       <!-- 每天凌晨3点执行任务调度 -->  
    <value>0 0 9 * * ?</value>
    </property>  
  </bean>  
  <!-- D.Quartz的调度工厂,调度工厂只能有一个,多个调度任务在list中添加 -->  
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    <property name="triggers">  
      <list>  
         <!-- 所有的调度列表-->  
        <ref bean="sendMailTrigger" />  
      </list>  
    </property>  
  </bean>  
</beans>

3、把spring-quartz.xml引入web.xml

<init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml,classpath:spring/spring-quartz.xml</param-value>
    </init-param>

4、附录邮件发送代码

@RequestMapping(value = "/send")
    public void send(String[] to, String[] cc, String subject, String content) throws MessagingException {
        // 配置发送邮件的环境属性
        final Properties props = new Properties();
        /*
         * 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host /
         * mail.user / mail.from
         */
        // 表示SMTP发送邮件,需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", mailSmtpHost);
        // 发件人的账号
        props.put("mail.user", mailUser);
        // 访问SMTP服务时需要提供的密码
        props.put("mail.password", mailPassword);
        //收件人
        props.put("mail.sendTo", sendTo);


        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(
                props.getProperty("mail.user"));
        message.setFrom(form);

        // 设置收件人
        String toStr = props.getProperty("mail.sendTo");
        to = toStr.split(",");
        InternetAddress[] sendTo = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {  
            sendTo[i] = new InternetAddress(to[i]);  
        }        
        message.setRecipients(RecipientType.TO, sendTo);

        // 设置抄送
        if(cc!=null && !"".equals(cc)) {
            InternetAddress[] sendCc = new InternetAddress[cc.length];
            for (int i = 0; i < cc.length; i++) {  
                sendCc[i] = new InternetAddress(cc[i]);  
            }        
            message.setRecipients(RecipientType.CC, sendCc);
        }

        // 设置邮件标题
        if(subject!=null && !"".equals(subject)) {
            message.setSubject(subject);
        }

        // 设置邮件的内容体
        if(content!=null && !"".equals(content)) {
            message.setContent(content, "text/html;charset=UTF-8");
        }

        // 发送邮件
        Transport.send(message);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值