SpringBoot 与任务

异步任务

@Service public class AsyncService { @Async //标识着这是一个异步任务 public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("数据处理中 .... "); } } @EnableAsync //开启异步任务 @SpringBootApplication public class TaskMain { public static void main(String[] args) { SpringApplication.run(TaskMain.class,args); } }

定时任务

public class ScheduleService { @Scheduled(cron = "0 * * * * MON-SAT") public void schedule(){ System.out.println("定时任务。。。"); } } @EnableScheduling //开启基于注解的定时任务 @EnableAsync @SpringBootApplication public class TaskMain { public static void main(String[] args) { SpringApplication.run(TaskMain.class,args); } }

41b50bab8d0447989358bca53850b479

clipboard

邮件任务

da5bd567e90e40bc9f601086141813eb

@SpringBootTest @RunWith(SpringRunner.class) public class TaskMainTest { @Autowired private JavaMailSenderImpl mailSender; @Test public void testMail(){ //发送简单邮件 SimpleMailMessage mailMessage = new SimpleMailMessage(); //邮件设置 mailMessage.setSubject("通知"); mailMessage.setText("今晚7:30开会"); mailMessage.setTo("2972891323@qq.com"); mailMessage.setFrom("1285653662@qq.com"); mailSender.send(mailMessage); } }

application.yml配置:

spring: mail: username: 1285653662@qq.com password: gkjrxbmyudswibjd host: smtp.qq.com properties: mail: smtp: ssl: enable: true

aba4ff65f5ed4c84a5cd20af9ed51784

发送复杂邮件:

/* 发送复杂邮件 */ @Test public void testComplexMail() throws MessagingException { //创建复杂邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("通知"); helper.setText("<h2 style='color:blue'>今晚7:30开会<h2>",true); // 添加附件 helper.addAttachment("image.jpg", new File("D:\GoogleDownload\background\image.jpg")); helper.setTo("2972891323@qq.com"); helper.setFrom("1285653662@qq.com"); mailSender.send(mimeMessage); }

clipboard

原文地址:https://www.cnblogs.com/houchen/p/13660769.html