Springboot之异步任务、邮件任务、定时执行任务

异步任务

步骤:

  1. 在要使用异步任务的方法上开启异步任务的注解,告诉Spring这是一个异步任务
  • Service层
@Service
public class AsyncService {
    //告诉Spring这是一个异步的方法
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理......");
    }
}
  • Controller层
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "OK";
    }
}
  1. 在主方法上开启异步任务
@EnableAsync //开启异步注解的功能
@SpringBootApplication
public class SpringbootTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestApplication.class, args);
    }
}

邮件任务

步骤:

  1. 首先引入依赖的包
<!--javax.mail:配置-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  1. 在配置文件中进行配置
spring.mail.username=xxxxxxxxx@qq.com
spring.mail.password=trarlbnxohpoggda
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
  1. 自动装配相关的类,进行测试程序的书写
@SpringBootTest
class SpringbootTestApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {
        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("甜瓜,你好呀!");
        mailMessage.setText("我就是一个西瓜");
        mailMessage.setTo("11237698@qq.com");
        mailMessage.setFrom("1123698@qq.com");
        mailSender.send(mailMessage);
    }
    @Test
    void contextLoads2() throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        //正文
        helper.setSubject("melon,hello");
        helper.setText("<p style='color:red'>thanks</p>",true);
        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\z144\\Desktop\\1.jpg"));
        helper.setTo("11237698@qq.com");
        helper.setFrom("1123698@qq.com");
        mailSender.send(mimeMessage);
    }
    //也可以对发送邮件的代码进行简单的封装,工具类
    public void sendmail(Boolean html,String subject,String text) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,html);
        //正文
        helper.setSubject(subject);
        helper.setText(text,true);
        //附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\zq144\\Desktop\\1.jpg"));
        helper.setTo("1123725698@qq.com");
        helper.setFrom("1123725698@qq.com");
        mailSender.send(mimeMessage);
    }
}

定时执行任务

  • 用到的关键类和注解
TaskScheduler   任务调度者
TaskExecutor     任务执行者
@EnableScheduling        //开启定时功能的注解
@Scheduled(cron="00 38 10 * * ?")     //什么时候执行
  • 步骤:
  1. 在主类main方法上开启定时功能的注解
@EnableScheduling //开启定时功能的注解
@SpringBootApplication
public class SpringbootTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestApplication.class, args);
    }
}
  1. 编写定时功能类,并加上注解,注意cron表达式的理解,如下。
@Service
public class ScheduledService {
    //在一个特定的时间执行这个方法  Timer
    //cron表达式(可以自行百度)
    //秒 分 时 日 月 周几
    /*
    * 0 5 10,18 * * ?    每天的10点5分和18点5分执行
    * 0/2 * * * * ?     每2秒执行一次任务
    * */
    @Scheduled(cron="00 38 10 * * ?")
    public void hello(){
        System.out.println("hello,你被执行了!!!");
    }
}
  1. 结果就是在对应的时间执行相关的代码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西瓜程序设计

您的打赏将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值