异步任务
@SpringBootApplication
@EnableAsync
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
@Service
public class AsyncService {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理...");
}
}
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();
return "ok";
}
}
邮件任务
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring.mail.username=1823149784@qq.com
spring.mail.password=uakbcuqbfgqpeicd
spring.mail.host=smtp.qq.com
# 开启加密验证(只有qq有)
spring.mail.properties.mail.smtp.ssl.enable=true
@Autowired
JavaMailSenderImpl javaMailSender;
public void sendMil(Boolean multipleFile,String text,String sender,String receiver,String subject) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,multipleFile);
helper.setSubject(subject);
helper.setText(text,true);
helper.addAttachment("1.jpg",new File("C:\Users\18231\Desktop\1.jpg"));
helper.addAttachment("2.jpg",new File("C:\Users\18231\Desktop\1.jpg"));
helper.setTo(receiver);
helper.setFrom(sender);
javaMailSender.send(mimeMessage);
}
定时任务
TaskScheduler 任务调度者
TaskExecutor 任务执行者
@EnableScheduling
@Schedule
Cron表达式 生成网站(https:
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
@Service
public class ScheduledService {
@Scheduled(cron = "30 53 19 * * ?")
public void hello(){
System.out.println("hello,你被执行了");
}
}