异步任务
- AsyncService
@Service
public class AsyncService {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理.....");
}
}
- AsyncController
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello();
return "ok";
}
}
- Springboot09TestApplication
开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
邮件任务
- application.properties
spring.mail.username=592939739@qq.com
spring.mail.password=evvzcockcwfvbbed
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtl.ssl.enable=true
- Springboot09TestApplicationTests
@SpringBootTest
class Springboot09TestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("山东菏泽~");
mailMessage.setText("666");
mailMessage.setTo("592939739@qq.com");
mailMessage.setFrom("592939739@qq.com");
mailSender.send(mailMessage);
}
}
定时执行任务
TaskScheduler 任务调度者
TaskExecutor 任务执行者
//开启定时功能的注解
@EnableScheduling
@Scheduled //什么时候执行
Cron表达式
- 开启定时功能注解
@EnableScheduling
- ScheduleService
@Service
public class ScheduleService {
@Scheduled(cron = "0 13 1 * * ?")
public void hello() {
System.out.println("hello,你被执行了");
}
}
|