当前位置 博文首页 > 文章内容

    SpringBoot 与任务

    作者: 栏目:未分类 时间:2020-09-13 11:41:19

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    异步任务

    @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