异步任务,定时任务,邮件发送
一,异步任务
方式一:创建一个class文件,名为AsyncService
package com.sqt.service;
import org.springframework.stereotype.Service;
/**
* @author mypc
*/
@Service
public class AsyncService {
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("数据正在处理。。。。。。");
}
}
创建一个class文件,名为AsyncController
package com.sqt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author mypc
*/
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();//停止3s,转圈
return "OK";
}
}
然后在启动类调用
方式二(使用注解):创建一个class文件,名为AsyncService,使用Asyncz注解
package com.sqt.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @author mypc
*/
@Service
public class AsyncService {
//@Async告诉spring这是一个异步方法
@Async
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("数据正在处理。。。。。。");
}
}
然后,在启动类开启注解
@EnableAsync
package com.sqt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* @author mypc
*/
//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Spring09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Spring09TestApplication.class, args);
}
}
二,邮件任务
(1)导入依赖javax.mail依赖,需要进行配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
(2)配置application.properties文件
spring.mail.username=1738771095@qq.com
spring.mail.password=iupgkqczflncedea
spring.mail.host=smtp.qq.com
#??????
spring.mail.smtp.ssl.enable=true
(3)在测试启动类中编写(但是复杂邮件的图片找不到,文件确可以?