Springboot中任务整合的方法有哪些

发布时间:2021-06-11 16:09:07 作者:Leah
来源:亿速云 阅读:164

这篇文章给大家介绍Springboot中任务整合的方法有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一 异步任务

启动类

@MapperScan("com.topcheer.*.*.dao")
@SpringBootApplication
@EnableCaching
@EnableRabbit
@EnableAsync
public class Oss6Application {
  public static void main(String[] args) {
    SpringApplication.run(Oss6Application.class, args);
  }
}

Controller层

/**
 * @author WGR
 * @create 2019/10/12 -- 21:53
 */
@RestController
public class AsynController {

  @Autowired
  AsynService asyncService;

  @GetMapping("/hello")
  public String hello(){
    asyncService.hello();
    return "success";
  }
}

Service层

/**
 * @author WGR
 * @create 2019/10/12 -- 21:52
 */
@Service
public class AsynService {

  //告诉Spring这是一个异步方法
  @Async
  public void hello() {
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("处理数据中...");
  }

}

测试结果:

页面直接显示success,控制台过3秒显示处理数据中...

二 定时任务

此处的定时,标注在方法上+注解,假如想修改生成环境的时间,不是很灵活,后面补充Quartz+boot,采用数据库配置和反射的原理。

注:java的cron表达式和Linux的不太一样,请注意,java为6位,linux为5位。

启动类

@SpringBootApplication
@EnableScheduling
public class Oss6Application {
  public static void main(String[] args) {
    SpringApplication.run(Oss6Application.class, args);
  }
}

服务类

@Service
public class ScheduledService {

  /**
   * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
   * 0 * * * * MON-FRI
   * 【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
   * 【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
   * 【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
   * 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
   * 【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
   */
  // @Scheduled(cron = "0 * * * * MON-SAT")
  //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
  // @Scheduled(cron = "0-4 * * * * MON-SAT")
  @Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次
  public void hello(){
    System.out.println("hello ... ");
  }
}

三 邮件任务

pom.xml

 <dependency>       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-mail</artifactId>
       <scope>test</scope>
     </dependency>

配置文件

spring: 
 mail:
  username: ***********
  password: *********  (这是qq邮箱的授权码)
  host: smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true

测试类

@Autowired(required = false)
  JavaMailSenderImpl mailSender;

  @Test
  public void contextLoads() {
    SimpleMailMessage message = new SimpleMailMessage();
    //邮件设置
    message.setSubject("通知-今晚开会");
    message.setText("今晚7:30开会");

    message.setTo("**************");
    message.setFrom("**************");

    mailSender.send(message);
  }

  @Test
  public void test02() throws Exception{
    //1、创建一个复杂的消息邮件
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

    //邮件设置
    helper.setSubject("测试");
    helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);

    helper.setTo("***************");
    helper.setFrom("**************");

    //上传文件
    helper.addAttachment("nginx.md",new File("C:\\Users\\asus\\Desktop\\nginx.md"));

    mailSender.send(mimeMessage);

  }

结果:

Springboot中任务整合的方法有哪些

关于Springboot中任务整合的方法有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. SpringBoot整合SpringTask如何实现定时任务功能?
  2. SpringBoot整合Swagger框架的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:SpringBoot中怎么激活profiles

下一篇:怎么在Spring中为IOC容器注入Bean

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》