/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

Quartz 定时邮件发送多个备份文件


项目代码

pom.xml 文件
Quartz 的包是整个项目不可缺少的

  1 <properties>
  2         <!-- Spring的版本 -->
  3         <springframework.version>4.0.6.RELEASE</springframework.version>
  4         <!-- Quartz的版本 -->
  5         <quartz.version>2.2.1</quartz.version>
  6     </properties>
  7 
  8     <dependencies>
  9         <dependency>
 10             <groupId>junit</groupId>
 11             <artifactId>junit</artifactId>
 12             <version>3.8.1</version>
 13             <scope>test</scope>
 14         </dependency>
 15 
 16         <dependency>
 17             <groupId>javax.servlet</groupId>
 18             <artifactId>javax.servlet-api</artifactId>
 19             <version>3.0.1</version>
 20         </dependency>
 21 
 22         <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
 23         <dependency>
 24             <groupId>javax.mail</groupId>
 25             <artifactId>mail</artifactId>
 26             <version>1.4</version>
 27         </dependency>
 28 
 29         <dependency>
 30             <groupId>org.quartz-scheduler</groupId>
 31             <artifactId>quartz</artifactId>
 32             <version>${quartz.version}</version>
 33         </dependency>
 34 
 35         <!-- spring -->
 36         <dependency>
 37             <groupId>org.springframework</groupId>
 38             <artifactId>spring-context-support</artifactId>
 39             <version>${springframework.version}</version>
 40         </dependency>
 41 
 42         <dependency>
 43             <groupId>org.springframework</groupId>
 44             <artifactId>spring-tx</artifactId>
 45             <version>${springframework.version}</version>
 46         </dependency>
 47 
 48         <dependency>
 49             <groupId>org.springframework</groupId>
 50             <artifactId>spring-web</artifactId>
 51             <version>${springframework.version}</version>
 52         </dependency>
 53 
 54         <dependency>
 55             <groupId>org.springframework</groupId>
 56             <artifactId>spring-webmvc</artifactId>
 57             <version>${springframework.version}</version>
 58         </dependency>
 59     </dependencies>

MyJob.java 继承 QuartzJobBean 然后 Override
有钻研精神的可以一点一点扒代码看 QuartzJobBean 。


  1 import org.quartz.JobExecutionContext;
  2 import org.quartz.JobExecutionException;
  3 import org.slf4j.Logger;
  4 import org.slf4j.LoggerFactory;
  5 import org.springframework.scheduling.quartz.QuartzJobBean;
  6 
  7 import java.util.Date;
  8 
  9 /**
 10  * kzyuan Job 参考
 11  * @description black husk
 12  * @description http://www.bhusk.com
 13  */
 14 public class MyJob extends QuartzJobBean {
 15 
 16     private static Logger logger = LoggerFactory.getLogger(DatabaseBackupJob.class);
 17 
 18     @Override
 19     protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
 20 
 21         System.out.println("执行时间:"+new Date());
 22     }
 23 }



application.xml 可以理解为 quartz 配置文件 注释很齐全

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"
  3        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 10 
 11     <!-- 定义任务bean -->
 12     <bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
 13         <!-- 指定具体的job类 -->
 14         <property name="jobClass" value="com.bhusk.quartz.Job.MyJob"/>
 15         <!-- 指定job的名称 -->
 16         <property name="name" value="myJob"/>
 17         <!-- 指定job的分组 -->
 18         <property name="group" value="jobs"/>
 19         <!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务  -->
 20         <property name="durability" value="true"/>
 21         <!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 -->
 22         <property name="applicationContextJobDataKey" value="applicationContext"/>
 23     </bean>
 24 
 25     <!-- 定义触发器 -->
 26     <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
 27         <property name="jobDetail" ref="myJobDetail"/>
 28         <!--执行时间 -->
 29         <property name="cronExpression" value="0 0 3 * * ?"/>
 30     </bean>
 31 
 32     <!-- 定义触发器 -->
 33      <!--演示:一个job可以有多个trigger; -->
 34     <!--<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">-->
 35     <!--<property name="jobDetail" ref="myJobDetail2" />-->
 36     <!--每一分钟执行一次-->
 37     <!--    <property name="cronExpression" value="0 0 3 * * ?"/> -->
 38      <!--</bean>-->
 39 
 40     <!-- 定义调度器 -->
 41     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
 42         <property name="triggers">
 43             <list>
 44                 <ref bean="cronTrigger"/>
 45      <!--          <ref bean="cronTrigger2"/> -->
 46             </list>
 47         </property>
 48     </bean>
 49 
 50 </beans>

JavaMail 发送邮件工具类
java 实现邮件的发送, 抄送及多附件
这个工具类来源网络,具体精准位置也不清楚了

  1         // 设置发件人
  2 //          mimeMsg.setFrom(new InternetAddress(from));
  3             mimeMsg.setFrom(new InternetAddress(from, nick));
  4             // 设置收件人
  5             if (to != null && to.length() > 0) {
  6                 mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  7             }
  8             // 设置抄送人
  9             if (cc != null && cc.length() > 0) {
 10                 mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
 11             }
 12             // 设置主题
 13             mimeMsg.setSubject(subject);
 14             // 设置正文
 15             BodyPart bp = new MimeBodyPart();
 16             bp.setContent(content, "text/html;charset=utf-8");
 17             mp.addBodyPart(bp);
 18             // 设置附件
 19             if (fileList != null && fileList.length > 0) {
 20                 for (int i = 0; i < fileList.length; i++) {
 21                     bp = new MimeBodyPart();
 22                     FileDataSource fds = new FileDataSource(fileList[i]);
 23                     bp.setDataHandler(new DataHandler(fds));
 24                     bp.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
 25                     mp.addBodyPart(bp);
 26                 }
 27             }
 28             mimeMsg.setContent(mp);
 29             mimeMsg.saveChanges();
 30             // 发送邮件
 31             if (props.get("mail.smtp.auth").equals("true")) {
 32                 Transport transport = session.getTransport("smtp");
 33                 transport.connect((String) props.get("mail.smtp.host"), (String) props.get("username"), (String) props.get("password"));
 34                 transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
 35                 transport.close();
 36             } else {
 37                 Transport.send(mimeMsg);
 38             }
 39             System.out.println("邮件发送成功");
 40         } catch (MessagingException e) {
 41             e.printStackTrace();
 42             success = false;
 43         } catch (UnsupportedEncodingException e) {
 44             e.printStackTrace();
 45             success = false;
 46         }
 47         return success;
 48     }
 49 
 50     public String getMailList(String[] mailArray) {
 51         StringBuffer toList = new StringBuffer();
 52         int length = mailArray.length;
 53         if (mailArray != null && length < 2) {
 54             toList.append(mailArray[0]);
 55         } else {
 56             for (int i = 0; i < length; i++) {
 57                 toList.append(mailArray[i]);
 58                 if (i != (length - 1)) {
 59                     toList.append(",");
 60                 }
 61 
 62             }
 63         }
 64         return toList.toString();
 65     }
 66 
 67     public static void main(String[] args) {
 68         String from = username;
 69         String[] to = {"keshu@bhusk.com", "1520812121@qq.com"};
 70         String[] copyto = {"lu12121@qq.com"};
 71         String subject = "黑壳网数据库备份";
 72         String content = "没有数据就没有一切,数据库备份就是一种防范灾难于未然的强力手段,没有了数据,应用再花哨也是镜中花水中月。";
 73         String[] fileList = new String[2];
 74         fileList[0] = "~/solo_h2/db.mv.db";
 75         fileList[1] = "~/solo_h2/db.trace.db";
 76 
 77         EmailManager.getInstance().sendMail(from, to, copyto, subject, content, fileList);
 78     }
 79 }


参考项目 GitHub:https://github.com/ykz200/Quartz_DatabaseBackup




本文出自于:ykz200

posted @ 2017-09-04 14:51  一品堂.技术学习笔记  阅读(298)  评论(0编辑  收藏  举报