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

  • 相关阅读:
    记录——framework探测定位程序集与core探测定位程序集
    C# 特定框架适用特定代码
    python读取excel代码
    时间比较
    ORA 01791错误
    MongoDB.1什么是MongoDB
    Mayatis 异常之result maps collection already contains value...
    怎样做好黄焖鸡
    关于foreach
    C#之out,ref关键字
  • 原文地址:https://www.cnblogs.com/houchen/p/13660769.html
Copyright © 2011-2022 走看看