【springboot 任务 异步 定时 邮件】

异步任务

1.两个注解

//开启异步注解功能
@EnableAsync
//告诉spring这是一个异步的方法
@Async

@EnableAsync 需要在主类中使用

2.测试代码

服务类

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

视图层

@RestController
public class AsynController {
    @Autowired
    AsynService asynService;
    @GetMapping("/hello")
    public String hello()
    {
        asynService.hello();
        return "ok";
    }
}

主类

//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Springboot09TestApplication {

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

}

假设我们没有开启异步注解,那么跳入视图层将会等待三秒,若开启 就直接进入视图层不会等待3秒后打印信息

邮件任务

1.导包

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

2.在application.properties配置邮箱信息

spring.mail.username=
spring.mail.password=授权码
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtl.ssl.enable=true

3.发送

    //验证密码 授权码
    @Autowired
    JavaMailSenderImpl javaMailSender;
    @Test
    void contextLoads() {

//        一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        //邮件主题
        mailMessage.setSubject("springboot 发送邮件");
        //邮件信息
        mailMessage.setText("谢谢你让我成功!");
        //收件人
        mailMessage.setTo("958390434@qq.com");
        //发件人
        mailMessage.setFrom("958390434@qq.com");
        //发送
        javaMailSender.send(mailMessage);
    }
    @Test
    void contextLoads1() throws MessagingException {

//       一个复杂的邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        //邮件主题
        //组装
        MimeMessageHelper messageHelper = new MimeMessageHelper(message,true);
        //邮件信息
        messageHelper.setSubject("复杂邮件");
        messageHelper.setText("1d2sa12d31sa32d1sa");
        messageHelper.addAttachment("1.jpg",new File("E:\\ideacode\\springboot-09-test\\src\\main\\resources\\static\\image\\1.png"));
        //收件人
        messageHelper.setTo("958390434@qq.com");
        //发件人
        messageHelper.setFrom("958390434@qq.com");
        //发送
        javaMailSender.send(message);
    }

定时任务

TaskExecutor
TaskScheduler
@EnableScheduling //开启定时功能的注解
Scheduled //表示什么时候执行~

cron 表达式
我们看使用在线的cron生成器
corn在线生成器

1.代码实现

服务类
只需在这里添加@Scheduled和设置执行时间@corn就行

@Service
public class ScheduleService {

    //在一个特定的时间执行这个方法~ timer
    //cron 表达式
    // 秒 分 时 日 月 星期
    @Scheduled(cron = "0 25 13 * * ?")
    public void hello()
    {
        System.out.println("hello, 你被执行了!!");
    }
}

在启动类里添加 开启定时任务
@EnableScheduling //开启定时功能的注解

//开启异步注解功能
@EnableAsync
@SpringBootApplication
@EnableScheduling //开启定时功能的注解
public class Springboot09TestApplication {

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值