1.异步任务
-
创建一个service包
-
创建一个类AsyncService
异步处理还是非常常用的,比如我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务。
编写方法:假装正在处理数据,使用线程设置一些延时,模拟同步等待的情况;
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中....");
}
}
-
编写controller包
-
编写AsyncController类
我们去写一个Controller测试一下
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
- 访问http://localhost:8080/hello进行测试,3秒后出现success,这是同步等待的情况。
问题:我们如果想让用户直接得到消息,就在后台使用多线程的方式进行处理即可,但是每次都需要自己手动去编写多线程的实现的话,太麻烦了,我们只需要用一个简单的办法,在我们的方法上加一个简单的注解即可,如下:
- 给service中的hello方法添加@Async注解:
//告诉Spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("业务进行中....");
}
- SpringBoot就会自己开一个线程池,进行调用!但是要让这个注解生效,我们还需要在主程序上添加一个注解@EnableAsync ,开启异步注解功能:
@EnableAsync //开启异步注解功能
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
- 重启测试,网页瞬间响应,后台代码依旧执行!
2. 邮件任务
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置自己的邮件xml信息
[email protected]
spring.mail.password=邮箱开启获取密码
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
- 配置简单复制邮件测试
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage=new SimpleMailMessage();
//主题
mailMessage.setSubject("邮箱测试1");
//正文
mailMessage.setText("你好钟情");
//收件人
mailMessage.setTo("[email protected]");
//发件人
mailMessage.setFrom("[email protected]");
mailSender.send(mailMessage);
}
@Test //发送复杂邮件
void contextLoads1() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//主题
helper.setSubject("复杂邮箱测试");
//正文
helper.setText("<p style='color:red'>下面是我为你发送的一个图片</p>",true);
//附件
helper.addAttachment("附件1",new File("C:\\Users\\钟情\\Pictures\\歌手图片\\2044.jpg"));
//收件人
helper.setTo("[email protected]");
//发件人
helper.setFrom("[email protected]");
mailSender.send(mimeMessage);
}
3. 定时任务
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
- 开启定时任务
在启动类上面加上@EnableScheduling注解即可开启定时任务。
@EnableScheduling
@SpringBootApplication
public class EmailApplication {
public static void main(String[] args) {
SpringApplication.run(EmailApplication.class, args);
}
}
- service中创建定时任务(头部格式cron)
@Service
public class ScheduledService {
@Scheduled(fixedRate = 5000)//什么时候执行
public void testTaskFirst() {
System.out.println("定时任务一:每五秒执行一次,当前时间:"+ LocalTime.now());
}
//秒 分 时 日 月 星期几
@Scheduled(cron = "0 42 10 ? * *") //什么时候执行
public void testTaskSecond() {
System.out.println("定时任务二:指定每天10:46执行,当前时间:"+LocalTime.now());
}
}