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

1、异步任务

1、开启异步注解功能

在Spring的启动类上添加注解@EnableAsync,开启异步注解功能

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

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

}

2、标识异步方法

在异步方法上添加注解,告诉Spring这是一个异步任务

@Service
public class AsyncService {

    //告诉Spring这是一个异步的方法
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据处理完毕~");
    }
}

3、调用异步方法

在controller调用到异步任务时,异步任务会在另一个线程进行
AsyncService的hello方法是一个异步方法,它会在另一个线程执行

@RestController
public class AsyncController {

    @Autowired
    private AsyncService as;

    @RequestMapping("/hello")
    public String hello(){
        as.hello();
        return "OK";
    }
}

2、邮件

1、导入邮件相关的依赖

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

2、邮件相关的配置

username是你的邮箱
password是认证密码

spring.mail.username=xxxxxxxqq.com
spring.mail.password=xxxxxxxxx
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

3、发送一封简单的邮件

自动注入JavaMailSenderImpl

@SpringBootTest
class Springboot07TestApplicationTests {
	@Autowired
	JavaMailSenderImpl mailSender;

	@Test
	void contextLoads() {

		//一个简单的邮件
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		
		//设置邮件标题
		mailMessage.setSubject("XXX");
		//设置邮件正文
		mailMessage.setText("XXXXX");
		
		//设置收件人
		mailMessage.setTo("xxxxx@qq.com");
		//设置发件人
		mailMessage.setFrom("xxxxx@qq.com");
		mailSender.send(mailMessage);
	}
}

4、发送一封复杂的邮件

@SpringBootTest
class Springboot07TestApplicationTests {
	@Autowired
	JavaMailSenderImpl mailSender;
	
	@Test
	void contextLoads() throws MessagingException {

		//一个复杂的邮件
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

		//设置邮件标题
		helper.setSubject("XXX");

		//第一个参数是正文,第二个个参数是否开启html
		helper.setText("<h1>XXXXX</h1>",true);
		
		//设置收发邮件人
		helper.setTo("xxxxx@qq.com");
		helper.setFrom("xxxxx@qq.com");

		//添加附件
		//第一个参数是附件的名称,第二个参数是一个文件
		helper.addAttachment("1.jpg", new File("写自己的文件路径"));

		mailSender.send(mimeMessage);
	}
}

3、定时任务

1、开启任务调度

@EnableScheduling //开启调度
@SpringBootApplication
public class Springboot07TestApplication {

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

}

2、标识被调度的方法

给要调度的方法加上注解@Scheduled,并设置cron表达式
关于cron表达式,感兴趣的朋友可以去看看这个篇博客
博客链接在此

@Service
public class ScheduledService {

    //cron表达式 秒 分 时 日 月 周几
    /*
        0 34 11 * * ?   每天11点34分0秒执行
    */
    @Scheduled(cron = "0 34 11 * * ?")
    public void hello(){
        System.out.println("被调度了~~");
    }
}

3、测试定时任务

运行SpringBoot启动类,到指定时间点就自动执行该方法
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值