美文网首页spring bootspring boot
SpringBoot与异步任务、定时任务、邮件任务

SpringBoot与异步任务、定时任务、邮件任务

作者: MaLizhi | 来源:发表于2018-12-17 16:45 被阅读119次

    环境: IDEA版本2017.3.1 x64, JDK1.8, SpringBoot2.1.1

    异步任务

    • 在需要开启异步的服务加上注解:@Async
    @Service
    public class AsyncService {
    
        //告诉SpringBoot这是一个异步任务,SpringBoot会自动开启一个线程去执行
        @Async
        public void testAsyncService(){
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("执行异步成功");
        }
    }
    
    • 在主配置类上添加开启异步注解功能:@EnableAsync
    @EnableAsync   //开启异步注解功能
    public class SpringbootMybatisApplication {
    

    定时任务

    • 在需要开启定时任务的服务上添加注解@Scheduled(cron = "0 * * * * MON-SAT")
        /* {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
         *  cron的六个符号分别对应以上时间单位,空格隔开
         *  * 表示所有值;
         *  ? 表示未说明的值,即不关心它为何值;
         *  - 表示一个指定的范围;
         *  , 表示附加一个可能值;
         *   / 符号前表示开始时间,符号后表示每次递增的值;
         */
    @Service
    public class ScheduledService {
        @Scheduled(cron = "0 * * * * MON-SAT")
        public void testSchedule(){
            System.out.println("测试定时任务成功");
        }
    }
    
    image
    • 在主配置类上开启定时任务注解功能:@EnableScheduling

    邮件任务

    • 引入邮件依赖组件
    <!-- 引入邮件,如果发现注入失败,可以自行到maven官网下载jar放进对应文件夹 -->
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    

    可能会产生的错误:注入失败(可以自行到maven官网下载jar放进对应文件夹):

    image
    • 邮箱开启POP3/SMTP服务


      image
    • 在主配置文件(yml方式)中配置邮箱参数

    spring:
      mail:
        username: yourqq@qq.com
        password: xxxxxx  //授权码,在服务选项中获取
        host: smtp.qq.com   //qq邮箱服务器
        properties:
          mail:
            smtp:
              ssl:
                enable: true //开启安全连接
    
    • 测试邮件发送
    @Autowired
    JavaMailSenderImpl mailSender;
    
    /**
    * 创建简单消息邮件
    */
    @Test
        public void testMail(){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("这是主题");
            message.setText("这是内容");
            //收件人
            message.setTo("xxxxx@qq.com");
            //发送人
            message.setFrom("xxxxx@qq.com");
            mailSender.send(message);
    }
    
    /**
         * 创建复杂消息邮件
         */
        @Test
        public void testMail02() throws MessagingException {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("这是复杂消息邮件主题");
            helper.setText("<b style='color:red;'>这是复杂消息邮件内容</b>",true);
            //添加附件1
            helper.addAttachment("1.jpg",new File("E:\\desktop\\8234.jpg"));
            //添加附件2
            helper.addAttachment("2.docx",new File("E:\\desktop\\形势与政策课作业.docx"));
            //收件人
            helper.setTo("xxxx@qq.com");
            //发送人
            helper.setFrom("xxxxx@qq.com");
            mailSender.send(mimeMessage);
        }
    
    
    
    

    测试成功

    image

    更多Spring Boot整合可浏览此博客:malizhi.cn

    相关文章

      网友评论

        本文标题:SpringBoot与异步任务、定时任务、邮件任务

        本文链接:https://www.haomeiwen.com/subject/vteckqtx.html