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

任务

  • 常用任务
    • 异步任务
    • 定时任务
    • 邮件任务
  • 异步任务
    service
@Service
public class AsyncService {

    @Async
    public void hellow(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理中......");
    }
}

controller

@Controller
public class AsyncController {

    @Autowired
    private AsyncService service;

    @ResponseBody
    @RequestMapping("/hellow")
    public String hellow(){
        service.hellow();
        return "hellow";
    }
}

启动类

@EnableAsync
@SpringBootApplication
public class Springboot10TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot10TestApplication.class, args);
    }

}

利用线程让方法睡3秒,会发现请求的时候,等三秒才能获取返回值,现在利用注解解决异步调用问题,在service方法加上@Async注解,然后在启动类上加上@EnableAsync(开启异步任务)即可,现在会发现执行请求,先获得返回值,然后方法后执行

注解@Enable***表示开启什么功能

  • 邮件任务
    1.导入依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2.以QQ邮箱为例


3.编写配置文件
application.properties

spring.mail.username=1419533877@qq.com
spring.mail.password=bjjucxhdiqtsjcbd //刚刚复制的密码
spring.mail.host=smtp.qq.com

# qq邮箱有一个特有的加密规则,网易就没有
spring.mail.properties.mail.smtp.ssl.enable=true

4.编写测试类测试

@SpringBootTest
class Springboot10TestApplicationTests {

    @Autowired
    JavaMailSenderImpl sender;

    @Test
    void contextLoads() {
        //一个简单邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("cdl");
        simpleMailMessage.setText("邮件任务调度");
        //发给谁
        simpleMailMessage.setTo("331772754@qq.com");
        simpleMailMessage.setFrom("1419533877@qq.com");
        sender.send(simpleMailMessage);
    }

}

5.成功收到

@Test
    void contextLoads2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = sender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("");
        helper.setText("<p style='color:red'>cdl</p>",true);

        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\Admin\\Desk\\1.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\Admin\\Desk\\1.jpg"));
        helper.setTo("1419533877@qq.com");
        helper.setFrom("1419533877@qq.com");
        sender.send(mimeMessage);
    }
  • 定时任务
    • TaskScheduler 任务调度程序
    • TaskExecutor 任务执行者
    • @EnableScheduling 开启定时功能
    • @Scheduled 什么时候执行
    • Cron表达式
      1.开启定时功能的注解
@SpringBootApplication
@EnableScheduling  //开启定时功能的注解
public class Springboot10TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot10TestApplication.class, args);
    }

}

service

@Service
public class ScheduledService {

    //在一个特定的时间执行这个方法
    //Cron表达式---------- 秒 分 时 日 月 星期几
    @Scheduled(cron = "0 12 20 * * ?")
    public void hellow(){
        System.out.println("hellow,你被执行了");
    }
}

常用Cron表达式

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

posted @ 2020-12-27 21:30  让你上瘾的三哥  阅读(102)  评论(0)    收藏  举报