2020:0705--18--任务和邮箱

231 阅读3分钟

主要内容

1.  异步任务
2.  定时任务
3.  邮件任务

1. 例子

    1.  Service
        @Service
        public class AsyncService {
        
            public void hello(){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("处理数据中");
            }
        
        }
    2.  Controller
        @RestController
        public class AsyncController {
        
            @Autowired
            AsyncService service;
        
            @GetMapping("/hello")
            public String hello(){
                service.hello();
                return "success";
            }
        }
    3.  发送一个请求:单线程的情况下,要等待3s才响应

    4.  异步实现
        开启异步注解功能

    5.  测试结果:
    
    controller调用service中的方法后,不再等待service中的方法执行完,
    直接返回success到页面

    service中方法的打印在3s后也执行了。

2. 定时任务

    项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。
    Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor、TaskScheduler接口。
    
    两个注解:@EnableScheduling、@Scheduled
    
    cron表达式:

    比如:
    s m h d M w 
    0 * * * * MON-FRI:
        周一到周五,任何月,日,时,分,每整秒(00秒,即每分钟启动一次)时启动一次
    * * * * * MON-FRI:
        周一到周五,任何月,日,时,分,每秒 都启动一次。
        
    枚举: ,
    0,1,2,3,4 * * * * MON-SAT:
        周一到周六,任何月,日,时,分,0.1.2.3.4秒都执行一次。
    区间:-    
    0-4 * * * * MON-SAT:
        周一到周六,任何月,日,时,分,0.1.2.3.4秒都执行一次。
    步长:/
    0/4 * * * * MON-SAT:
        周一到周六,任何月,日,时,分,每4秒执行一次。
        
    一些复杂例子:
    0 0/5 14,18 * * ?
        每天14点整和18点整,每隔5分钟执行一次
    0 15 10 ? * 1-6
        每个月的周一至周六10:15分执行一次
    0 0 2 ?* 6L
        每个月的最后一个周六凌晨2点执行一次
    0 0 2 LW * ?
        每个月的最后一个工作日的凌晨2点执行一次
    0 0 2-4 ?* 1#1
        每个月的第一个周一凌晨2点到4点之间,每整点执行一次

在service中写一个定时任务
    @Service
    public class ScheduledService {
        /**
         * s m h d M w
         * 0 * * * * MON-FRI :周一到周五,任何月,日,时,分,每整秒(00秒,即每分钟启动一次)时启动一次
         * * * * * * MON-FRI :周一到周五,任何月,日,时,分,每秒 都启动一次。
         */
        @Scheduled(cron = "0 * * * * MON-FRI")
        public void hello(){
            System.out.println("hello");
        }
    }

还要在主配置类上开启基于注解的定时任务
    @EnableAsync //开启异步注解功能
    @EnableScheduling //开启基于注解的定时任务
    @SpringBootApplication
    public class SpringbootAdvanced04TaskApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootAdvanced04TaskApplication.class, args);
        }
    
    }

3. 邮件任务

1.  邮件发送需要引入spring-boot-starter-mail
2.  Spring Boot 自动配置MailSenderAutoConfiguration
3.  定义MailProperties内容,配置在application.yml中
4.  自动装配JavaMailSender
5.  测试邮件发送

6.  两个邮箱发送消息的流程图:

1.  引入相关依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
2.  进行属性配置

    注意:邮箱密码要填写授权码

    注意:spring.mail.host=填写smtp服务地址

        spring.mail.username=1309984539@qq.com
        spring.mail.password=vbqmepysyyoijgdi
        spring.mail.host=smtp.qq.com
        # 额外的配置安全连接
        spring.mail.properties.mail.stmp.ssl.enable=true
3.  测试一个简答的邮件发送:简单邮件对象
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    @SpringBootTest
    class SpringbootAdvanced04MailApplicationTests {
    
        @Autowired
        JavaMailSenderImpl mailSender;
    
        @Test
        void contextLoads() {
    
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    
            //邮件设置
            simpleMailMessage.setSubject("通知-今晚开会");
            simpleMailMessage.setText("今晚7点开会");
            simpleMailMessage.setTo("gji062840840628406@163.com");
            simpleMailMessage.setFrom("1309984539@qq.com");
            
            mailSender.send(simpleMailMessage);
        }
    
    }
    发送成功

4.  测试一个复杂的邮件发送:简单复杂对象

    @Autowired
    JavaMailSenderImpl mailSender;
    
    @Test
    void test02() throws Exception{

        //创建一个复杂的消息对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        //SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        //邮件设置
        helper.setSubject("通知-今晚开会");
        helper.setText("<b style='color:red'>今天5:00开会</b>", true);
        //上传附件
        helper.addAttachment("1.png", new File("C:\\Users\\13099\\Desktop\\F1截图\\1.png"));
        helper.addAttachment("2.png", new File("C:\\Users\\13099\\Desktop\\F1截图\\2.png"));

        helper.setTo("gji062840840628406@163.com");
        helper.setFrom("1309984539@qq.com");

        mailSender.send(mimeMessage);
    }

    测试成功