SpringBoot - 任务

异步任务

@Service
public class AsyncService {

    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("hello");
    }
}

@RestController
public class AsyncController {

    @Autowired
    AsyncService asyncService;

    @GetMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "success";
    }
}

浏览器访问localhost:8080/hello,因为线程休眠3秒,所以会转3秒才会显示success,此时在service方法上添加@Async注解表明该方法是异步方法,并在启动类上添加@EnableAsync注解开启异步任务注解,此时再次访问无需等待直接显示success

定时任务

项目中会存在某一时刻或某一时间段执行某些操作的需求,此时即可使用@Scheduled注解添加在方法上定时执行,需要在启动类上添加@EnableScheduling开启定时任务注解,如

@Service
public class ScheduledService {

    @Scheduled(cron = "0-10 * * * * *")
    public void hello(){
        System.out.println("hello world");
    }
}

其中cron表达式有两种语法格式:

  1. Seconds Minutes Hours DayofMonth Month DayofWeek Year
  2. Seconds Minutes Hours DayofMonth Month DayofWeek

每一个域可用的字符如下:

  • Seconds:", - * /"四个字符,有效范围为0-59的整数
  • Minutes:", - * /"四个字符,有效范围为0-59的整数
  • Hours:", - * /"四个字符,有效范围为0-23的整数
  • DayofMonth:", - * / ? L W C"八个字符,有效范围为0-31的整数
  • Month:", - * /"四个字符,有效范围为1-12的整数或JAN-DEC
  • DayofWeek:", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 以此类推
  • Year:", - * /"四个字符,有效范围为1970-2099年

每一个域都可以使用数字,也可以使用特殊字符,它们的含义是:

  1. *****:表示匹配该域的任何值,如在Seconds中使用*,表示每秒钟都会触发事件
  2. ?:只能用在DayofMonth和DayofWeek两个域,它也可以匹配域的任何值,但DayofMonth和DayofWeek会相互影响,如想在每月的20日触发事件,则DayofWeek只能使用?,而不能使用*****,如果使用*表示不管星期几都会触发
  3. -:表示范围,如在Seconds中使用0-5,表示在0-5秒内每秒钟都触发事件
  4. /:表示从起始事件开始,每隔固定时间触发事件,如在Seconds中使用0/5,表示从0秒开始,每隔5秒触发一次
  5. ,:表示枚举,如在在Seconds中使用0,1,2,3,4,5,表示在0,1,2,3,4,5秒每秒触发一次
  6. L:表示最后,只能出现在DayofWeek和DayofMonth域,如在DayofWeek域使用5L,表示在最后的一个星期四触发事件(5表示星期四)
  7. W:表示工作日(周一到周五),只能出现在DayofMonth域,在离指定日期的最近的工作日触发事件。如在 DayofMonth使用5W,如果5日是星期六,则在最近的工作日4日星期五触发;如果5日是星期天,则在6日星期一触发;如果5日在星期一到星期五中的一天,则就在5日触发。注意,W的最近工作日寻找不会跨过月份
  8. LW:两个字符可以连用,表示某月最后一个工作日,即星期五
  9. #:用于确定每个月第几个星期几,只能出现在DayofMonth域。如4#2,表示某月的第二个星期三(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点期间,每个整点都执行一次

邮件任务

环境准备

邮件任务需要引入依赖如下:

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

配置application.properties

[email protected]
spring.mail.password=zjxfovnjnbmmijae #授权码
spring.mail.host=smtp.qq.com #QQ邮箱的SMTP服务器
spring.mail.properties.mail.smtp.ssl.enable=true #开启ssl

需要在QQ邮箱设置 - 帐户中开启服务,并生成授权码(并填入application.properties中)

测试发送邮件

简单邮件发送

@Autowired
    JavaMailSenderImpl mailSender;

    /**
     * 简单邮件发送
     */
    @Test
    public void test01() {
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件设置
        message.setSubject("在干嘛");
        message.setText("哈哈");
        message.setTo("[email protected]");
        message.setFrom("[email protected]");
        mailSender.send(message);
    }

复杂邮件发送(带附件)

@Test
    public void test02() throws Exception{
        MimeMessage mimeMailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage, true);
        helper.setSubject("在干嘛");
        //true指定为html元素,默认为false
        helper.setText("<strong style='color:red'>哈哈</strong>",true);
        helper.setTo("[email protected]");
        helper.setFrom("[email protected]");
        // 上传文件
        helper.addAttachment("图片", new File("D:\\背景图\\cb990076cde55365ca679384d3e7198e.jpg"));
        mailSender.send(mimeMailMessage);
    }

猜你喜欢

转载自www.cnblogs.com/codeDD/p/12699471.html