Spring Boot与任务

    科技2022-07-13  137

    一、异步任务

    两个注解: @EnableAysns、@Aysnc

    代码示例:

    @EnableAsync // 开启异步注解功能 @SpringBootApplication public class SpringBoot04TaskApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot04TaskApplication.class, args); } }

    【AsyncService.java】

    @Service public class AsyncService { // 告诉spring这是一个异步方法 @Async public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("处理数据中...."); } }

    【AsyncController.java】

    @RestController public class AsyncController { @Autowired private AsyncService asyncService; @GetMapping(value = "/hello") public String hello(){ asyncService.hello(); return "success"; } }

    二、定时任务

    两个注解: @EnableScheduling、 @Scheduled

    cron表达式:

    开启基于注解的定任务

    @EnableScheduling // 开启基于注解的定时任务 @SpringBootApplication public class SpringBoot04TaskApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot04TaskApplication.class, args); } }

    【ScheduledService.java】

    second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几)

    对应:0 * * * * MON-FR

    【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 public class ScheduledService { //@Scheduled(cron = "0 * * * * MON-SAT") // 整点执行一次 // @Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") // 0,1,2,3,4 各执行一次 // @Scheduled(cron = "0-4 * * * * MON-SAT") //0-4执行 @Scheduled(cron = "0/4 * * * * MON-SAT") // 每4秒执行一次 public void hello(){ System.out.println("hello..."); } }

    三、邮件任务

    ① 导入pom 依赖

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>

    ② 配置 application.properties

    spring.mail.username=// 自己的邮箱 spring.mail.password= spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true

    spring.mail.password 怎么获取:(以 QQ 邮箱为例)

    登入QQ邮箱

    ③ 测试

    基本格式:

    @Autowired JavaMailSenderImpl javaMailSender; @Test void contextLoads() { SimpleMailMessage message = new SimpleMailMessage(); // 邮件设置 message.setSubject("通知-今晚开会"); message.setText("今晚7:30开会"); // 邮件接收 message.setTo("2640379231@qq.com"); // 邮件发送 message.setFrom("2097291754@qq.com"); javaMailSender.send(message); }

    复杂格式

    @Autowired JavaMailSenderImpl javaMailSender; @Test public void test02() throws MessagingException { // 1.创建一个复杂的消息邮件 MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // 邮件设置 helper.setSubject("通知-今晚开会"); // 开启html 渲染,默认是false helper.setText("<b style='color:red'>今天 7:30 开会</b>", true); helper.setTo("2640379231@qq.com"); helper.setFrom("2097291754@qq.com"); // 上传文件 helper.addAttachment("1.jpg",new File("C:\\Users\\hp\\Desktop\\OY\\图片\\1.jpg")); helper.addAttachment("2.jpg",new File("C:\\Users\\hp\\Desktop\\OY\\图片\\2.jpg")); javaMailSender.send(mimeMessage); }

    Processed: 0.017, SQL: 8