0
点赞
收藏
分享

微信扫一扫

任务调度的是三种方式

求索大伟 2022-06-24 阅读 30
  • 异步任务
@Service
public class AsyncService {

@Async //告诉Spring,这是一个异步方法
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据正在处理......");
}
}
@SpringBootApplication
@EnableAsync//开启异步
public class SpringboortTaskApplication {

public static void main(String[] args) {
SpringApplication.run(SpringboortTaskApplication.class, args);
}
}
  • 定时任务
@EnableScheduling//开启定时功能的注解
@Schedule
@Service
public class ScheduledService {

/*在某一个特定的时间执行该方法~ Timer
cron
* 秒 分 时 日 月 星期
* "0 * * * * 0-7"
* 0 26 19 * * ? 每天19点26分 执行一次
* 30 0/10 4 10 * * ? 每天10点和4点,每隔10分钟执行一次
*/
@Scheduled(cron = "0/2 * * * * ?")
public void hello(){
System.out.println("你被逮捕了!lwh");
}
}
  • 邮件任务
  • application.yml
spring:
mail:
username: 2861184805@qq.com
host: smtp.qq.com
properties:
mail:
smtp:
ssl:
enable:true1
password: raaipltxzfqydcha测试类
  • 测试类
@Autowired
JavaMailSenderImpl javamail;

@Test
void contextLoads() {
//简单的邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("xiaobear,你好");
message.setText("你是最棒的!");
message.setTo("2861184805@qq.com");
message.setFrom("2861184805@qq.com");
javamail.send(message);
}
@Test
void contextLoads2() throws MessagingException {
//复杂的邮件
MimeMessage message = javamail.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setSubject("xiaobear,你好!加油哦");
helper.setText("<p>你是最棒的!</p>",true);
//附件
helper.addAttachment("升级锁的过程.png",new File("D:\\360MoveData\\Users\\Administrator\\Desktop\\升级锁的过程.png"));
helper.setTo("2861184805@qq.com");
helper.setFrom("2861184805@qq.com");
javamail.send(message);
}
//封装成一个方法
public void sendMail(Boolean html,String subject,......)throws MessagingException{
//复杂的邮件
MimeMessage message = javamail.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setSubject("xiaobear,你好!加油哦");
helper.setText("<p>你是最棒的!</p>",true);
//附件
helper.addAttachment("升级锁的过程.png",new File("D:\\360MoveData\\Users\\Administrator\\Desktop\\升级锁的过程.png"));
helper.setTo("2861184805@qq.com");
helper.setFrom("2861184805@qq.com");
javamail.send(message);
}



举报

相关推荐

0 条评论