zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    反射-基础方法-java
    排序-插入-java
    排序-选择-java
    决策树
    python基础2 -画图
    python基础1
    如何实现用户的历史记录功能(最多n条)
    如何让字典保持有序
    如何快速找到多个字典中的公共键(key)
    如何根据字典中值的大小, 对字典中的项排序
  • 原文地址:https://www.cnblogs.com/houchen/p/13660769.html
Copyright © 2011-2022 走看看