SpringBoot 应用篇笔记

https://youtube.com/playlist?list=PLD3Xyx6ef38xcNhCaCV_fUqB9vjTDA7hX

文件上传与下载

文件上传

  • Spring Boot 实现文件上传与下载功能本质还是依靠 Spring MVC
  • 需要注意的是,获取存放上传与下载文件的目录的方式不同
@RequestMapping("/uploadDocument")
public Result uploadDocument(MultipartFile uploadFile) throws IOException {
    // 获取上传的文件的文件名
    String fileName = uploadFile.getOriginalFilename();
    // 处理文件重名问题
    String suffixName = fileName.substring(fileName.lastIndexOf("."));
    String prefixName = fileName.substring(0, fileName.lastIndexOf("."));
    fileName = prefixName + "-" + UUID.randomUUID() + suffixName;
    // 获取服务器中 Document 目录的路径
    File documentPath = new File(ResourceUtils.getURL("classpath:static/uploadPath/document").getPath());
    if (!documentPath.exists()) {
        documentPath.mkdir();
    }
    String finalPath = documentPath + File.separator + fileName;
    // 实现上传功能
    uploadFile.transferTo(new File(finalPath));
    return Result.OK("/uploadPath/document/" + fileName);
}

文件下载

@GetMapping("/downloadDocument/{fileName}")
public ResponseEntity<byte[]> downloadDocument(@PathVariable("fileName") String fileName) throws IOException {
    // 获取服务器中文件的真实路径
    String realPath = ResourceUtils.getURL("classpath:static/uploadPath/document/").getPath() + fileName;
    // 创建输入流
    //InputStream is = new FileInputStream(realPath);
    InputStream is = Files.newInputStream(Paths.get(realPath));
    // 创建字节数组
    byte[] bytes = new byte[is.available()];
    // 将流读到字节数组中
    is.read(bytes);
    // 创建HttpHeaders对象设置响应头信息
    MultiValueMap<String, String> headers = new HttpHeaders();
    // 对文件名重新编码,防止中文乱码
    fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
    // 设置要下载方式以及下载文件的名字
    headers.add("Content-Disposition", "attachment;filename=" + fileName);
    // 设置响应状态码
    HttpStatus statusCode = HttpStatus.OK;
    // 创建ResponseEntity对象
    ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
    // 关闭输入流
    is.close();
    return responseEntity;
}

5-整合第三方技术

5-2 定时任务

定时任务是企业级开发中必不可少的组成部分,诸如长周期业务数据的计算,例如年度报表,诸如系统脏数据的处理,再比如系统性能监控报告,还有抢购类活动的商品上架,这些都离不开定时任务。本节将介绍两种不同的定时任务技术。

Quartz

Quartz技术是一个比较成熟的定时任务框架,怎么说呢?有点繁琐,用过的都知道,配置略微复杂。springboot对其进行整合后,简化了一系列的配置,将很多配置采用默认设置,这样开发阶段就简化了很多。在

学习springboot整合Quartz前先普及几个Quartz的概念。

  • 工作(Job):用于定义具体执行的工作
  • 工作明细(JobDetail):用于描述定时工作相关的信息
  • 触发器(Trigger):描述了工作明细与调度器的对应关系
  • 调度器(Scheduler):用于描述触发工作的执行规则,通常使用cron表达式定义规则

步骤①:导入springboot整合Quartz的starte

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

步骤②:定义任务Bean,按照Quartz的开发规范制作,需要继承QuartzJobBean

  • 注意,任务Bean不需要标注为组件交给Spring容器管理。
public class MyQuartz extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("quartz task run...");
    }
}

步骤③:创建Quartz配置类,定义工作明细(JobDetail)与触发器的(Trigger)bean

@Configuration
public class QuartzConfig {
  
  	/**
     * 工作明细
     * @return
     */
    @Bean
    public JobDetail printJobDetail(){
        //绑定具体的工作
        return JobBuilder.newJob(MyQuartz.class).storeDurably().build();
    }
  
  	/**
     * 触发器
     * @return
     */
    @Bean
    public Trigger printJobTrigger(){
        ScheduleBuilder schedBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        //绑定对应的工作明细与执行时间(调度器)
        return  TriggerBuilder.newTrigger().forJob(printJobDetail()).withSchedule(schedBuilder).build();
    }
}
  • 工作明细中要设置对应的具体工作
  • 使用newJob()操作传入对应的工作任务类型即可。
  • 触发器需要绑定任务,使用forJob()操作传入绑定的工作明细对象,
  • 此处可以为工作明细设置名称,然后使用名称绑定,
  • 也可以直接调用对应方法绑定;
  • 触发器中最核心的规则是执行时间,此处使用调度器定义执行时间,
  • 执行时间描述方式使用的是cron表达式。有关cron表达式的规则,各位小伙伴可以去参看相关课程学习,略微复杂,而且格式不能乱设置,不是写个格式就能用的,写不好就会出现冲突问题。

总结

  1. springboot整合Quartz就是将Quartz对应的两个核心对象交给spring容器管理,包含两个对象,JobDetail和Trigger对象
  2. JobDetail对象描述的是工作的执行信息,需要绑定一个QuartzJobBean类型的对象
  3. Trigger对象定义了一个触发器,需要为其指定绑定的JobDetail是哪个,同时要设置执行周期调度器

Spring Task

上面的操作看上去不多,但是Quartz将其中的对象划分粒度过细,导致开发的时候有点繁琐,Spring针对上述规则进行了简化,开发了自己的任务管理组件——Task,

Spring根据定时任务的特征,将定时任务的开发简化到了极致。怎么说呢?要做定时任务总要告诉容器有这功能吧,然后定时执行什么任务直接告诉对应的bean什么时间执行就行了,就这么简单,一起来看怎么做

步骤①:开启定时任务功能,使用注解 @EnableScheduling

@SpringBootApplication
//开启定时任务功能
@EnableScheduling
public class Springboot22TaskApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot22TaskApplication.class, args);
    }
}

步骤②:定义Bean,在对应要定时执行的操作上方,使用注解@Scheduled定义执行的时间,执行时间的描述方式还是cron表达式

@Component
public class MyTask {
    @Scheduled(cron = "0/1 * * * * ?")
    public void print(){
        System.out.println(Thread.currentThread().getName()+" :spring task run...");
    }
}

完事,这就完成了定时任务的配置。总体感觉其实什么东西都没少,只不过没有将所有的信息都抽取成bean,而是直接使用注解绑定定时执行任务的事情而已。

  • 如果想对定时任务进行相关配置,可以通过配置文件进行
spring:
  task:
   	scheduling:
      pool:
       	size: 1							# 任务调度线程池大小 默认 1
      thread-name-prefix: spring_task_      	# 调度线程名称前缀 默认 scheduling-      
        shutdown:
          await-termination: false		# 线程池关闭时等待所有任务完成
          await-termination-period: 10s	# 调度线程关闭前最大等待时间,确保最后一定关闭

总结

  1. Spring Task需要使用注解 @EnableScheduling 开启定时任务功能

  2. 为定时执行的的任务设置执行周期,描述方式cron表达式

赞赏