SpringBoot中的异步任务、定时任务和邮件任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zxd1435513775/article/details/85048436

一、SpringBoot中的异步任务

在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的,但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。

1、模拟长时间服务调用
//Service类
@Service
public class AsyncService {
   
    public void hello(){

        try {
        	//让线程休眠3秒,模拟长时间无响应
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("业务正在处理中……");

    }
}
//Controller类
@RestController
public class AsyncController {

    @Autowired
    AsyncService asyncService;

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

}
//启动类
@SpringBootApplication
public class SpringbootTaskApplication {

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

}
2、运行结果:(输入请求地址后,会等待3秒,才会有输出)

在这里插入图片描述

3、采用异步方式:

用@Async 注解,修饰Service中需要执行异步的方法

@Service
public class AsyncService {

    @Async //该方法是异步的
    public void hello(){

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("业务正在处理中……");

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

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }
}
4、测试结果:打开浏览器,重新发送请求,页面及时响应,无须等待3秒
5、小节

两个注解:
@Aysnc:该注解修饰需要执行异步的方法;
@EnableAysnc:该注解是开启异步注解的功能;别忘记打开

二、SpringBoot中的定时任务

项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式。

1、需要定时执行的Service服务
@Service
public class ScheduledService {
	//定时执行表达式
    @Scheduled(cron = "0 * * * * MON-SAT")
    public void hello(){

        System.out.println("hello……");

    }
}

需要开启@EnableScheduling 注解

@EnableScheduling //开启定时任务功能
@SpringBootApplication
public class SpringbootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }
}
3、测试结果:(每分钟的0秒,打印hello……)

在这里插入图片描述

4、cron 表达式

在这里插入图片描述

在这里插入图片描述

5、小节

两个注解:
@EnableScheduling:开启定时任务注解
@Scheduled:标注方法执行定时任务

三、SpringBoot中的邮件任务

1、添加mail依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、编写测试代码:
RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootTaskApplicationTests {

    @Autowired
    JavaMailSenderImpl javaMailSender;


    @Test
    public void contextLoads() {
		//普通的文本邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        simpleMailMessage.setSubject("放假通知");
        simpleMailMessage.setText("下周放假");
        simpleMailMessage.setTo("*******.com");
        simpleMailMessage.setFrom("[email protected]");

        javaMailSender.send(simpleMailMessage);

    }

    @Test
    public void test02() throws MessagingException {
		//带附件的的邮件
        MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage,true);

        helper.setSubject("通知-放假通知");
        helper.setText("<b style='color:red'>下周放假</b>",true);
        helper.setTo("**********.com");
        helper.setFrom("[email protected]");

        helper.addAttachment("1.jpg",new File("C:\\Users\\Think\\Downloads\\scorpios.group_cert.jpg"));
        helper.addAttachment("2.jpg",new File("C:\\Users\\Think\\Downloads\\scorpios.xin_cert.jpg"));

        javaMailSender.send(mimeMailMessage);

    }
 }
3、配置文件
//发送者的邮件
[email protected]
//不是邮箱的密码,是授权码
spring.mail.password=XXXXXXXX(查看自己的授权码)
spring.mail.host=smtp.qq.com
4、配置QQ邮箱,获取授权码

第一步:
在这里插入图片描述
第二步:
在这里插入图片描述
第三步:
在这里插入图片描述
第四步:
在这里插入图片描述

5、测试结果

在这里插入图片描述
在这里插入图片描述

四、小结

SpringBoot提供了异步任务、定时任务、邮件任务的功能。
异步任务:

@Aysnc:该注解修饰需要执行异步的方法;
@EnableAysnc:该注解是开启异步注解的功能;

定时任务:

@EnableScheduling:开启定时任务注解
@Scheduled:标注方法执行定时任务

邮件任务:

引入依赖
配置邮件账号
使用JavaMailSenderImpl 发送邮件

猜你喜欢

转载自blog.csdn.net/zxd1435513775/article/details/85048436
今日推荐