Просмотр исходного кода

新增包装机服务文件监听

赵冬冬 3 лет назад
Родитель
Сommit
286be39f08

+ 5 - 0
pom.xml

@@ -24,6 +24,11 @@
     </properties>
 
     <dependencies>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.8.0</version>
+        </dependency>
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>easyexcel</artifactId>

+ 9 - 0
src/main/java/com/hw/admin/PackerServiceApplication.java

@@ -1,8 +1,11 @@
 package com.hw.admin;
 
+import com.hw.admin.system.listener.FileMonitor;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
  * 系统模块
@@ -12,8 +15,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
 @MapperScan("com.hw.admin.**.mapper")
+@EnableScheduling
 public class PackerServiceApplication {
     public static void main(String[] args) {
         SpringApplication.run(PackerServiceApplication.class, args);
     }
+
+    @Bean
+    public FileMonitor fileMonitor() {
+        return new FileMonitor("D:\\project\\delivery_project\\远程制药\\后台\\ycpharmacy_excel_service\\excel\\");
+    }
 }

+ 1 - 10
src/main/java/com/hw/admin/model/controller/WorkPackerController.java

@@ -1,13 +1,10 @@
 package com.hw.admin.model.controller;
 
 
-import com.alibaba.excel.EasyExcel;
 import com.hw.admin.model.domain.AjaxResultVo;
 import com.hw.admin.model.domain.WorkPacker;
-import com.hw.admin.model.domain.WorkPackerExcel;
 import com.hw.admin.model.mapper.WorkPackerMapper;
 import com.hw.admin.model.service.WorkPackerService;
-import com.hw.admin.system.listener.WorkPackerListener;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
@@ -27,13 +24,7 @@ public class WorkPackerController {
     @PostMapping
     @ResponseBody
     public AjaxResultVo add(@RequestBody WorkPacker workPacker) {
-        // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
-        // 写法3:
-        String fileName = "D:\\project\\delivery_project\\远程制药\\后台\\ycpharmacy_excel_service\\excel\\包装机数据测试表.xlsx";
-        // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
-        EasyExcel.read(fileName, WorkPackerExcel.class, new WorkPackerListener(workPackerMapper)).sheet().doRead();
-        //workPackerService.add(workPacker);
-        return AjaxResultVo.success();
+        return AjaxResultVo.success(workPackerService.add(workPacker));
     }
 
 

+ 2 - 0
src/main/java/com/hw/admin/model/service/WorkPackerService.java

@@ -20,4 +20,6 @@ public interface WorkPackerService {
 
     public int inserList(List<WorkPacker> workPacker);
 
+    public void task(String time);
+
 }

+ 11 - 0
src/main/java/com/hw/admin/model/service/impl/WorkPackerServiceImpl.java

@@ -3,8 +3,10 @@ package com.hw.admin.model.service.impl;
 import com.alibaba.excel.EasyExcel;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.hw.admin.model.domain.WorkPacker;
+import com.hw.admin.model.domain.WorkPackerExcel;
 import com.hw.admin.model.mapper.WorkPackerMapper;
 import com.hw.admin.model.service.WorkPackerService;
+import com.hw.admin.system.listener.WorkPackerListener;
 import com.hw.admin.system.utils.Sequence;
 import javafx.scene.input.InputMethodTextRun;
 import org.springframework.beans.BeanUtils;
@@ -37,6 +39,15 @@ public class WorkPackerServiceImpl implements WorkPackerService {
         return workPackerMapper.inserList(list);
     }
 
+    @Override
+    public void task(String time) {
+        // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
+        // 写法3:
+        String fileName = "D:\\project\\delivery_project\\远程制药\\后台\\ycpharmacy_excel_service\\excel\\包装机数据测试表.xlsx";
+        // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
+        EasyExcel.read(fileName, WorkPackerExcel.class, new WorkPackerListener(workPackerMapper)).sheet().doRead();
+    }
+
     @Override
     public List<WorkPacker> list(WorkPacker workPacker) {
         QueryWrapper<WorkPacker> queryWrapper = new QueryWrapper<WorkPacker>();

+ 25 - 0
src/main/java/com/hw/admin/system/listener/ContextFileListener.java

@@ -0,0 +1,25 @@
+package com.hw.admin.system.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+public class ContextFileListener implements ApplicationListener<ContextRefreshedEvent> {
+    @Autowired
+    private FileMonitor fileMonitor;
+    @Autowired
+    private FileListener fileListener;
+
+    @Override
+    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
+        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
+            fileMonitor.setFileListener(fileListener);
+            fileMonitor.start();
+        }
+    }
+}
+

+ 63 - 0
src/main/java/com/hw/admin/system/listener/FileListener.java

@@ -0,0 +1,63 @@
+package com.hw.admin.system.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.monitor.FileAlterationListener;
+import org.apache.commons.io.monitor.FileAlterationObserver;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+
+@Slf4j
+@Component
+public class FileListener implements FileAlterationListener {
+    @Override
+    public void onStart(FileAlterationObserver fileAlterationObserver) {
+        File directory = fileAlterationObserver.getDirectory();
+        log.info("开始监听..." + directory);
+    }
+
+    @Override
+    public void onDirectoryCreate(File file) {
+        String name = file.getName();
+        log.info("目录被创建了..." + name);
+    }
+
+    @Override
+    public void onDirectoryChange(File file) {
+        String name = file.getName();
+        log.info("目录被修改了..." + name);
+    }
+
+    @Override
+    public void onDirectoryDelete(File file) {
+        String name = file.getName();
+        log.info("目录被删除了..." + name);
+    }
+
+    @Override
+    public void onFileCreate(File file) {
+        String name = file.getName();
+        String path = file.getPath();
+        log.info("文件被创建了..." + name);
+        log.info("文件是..." + path);
+    }
+
+    @Override
+    public void onFileChange(File file) {
+        String name = file.getName();
+        log.info("文件被修改了..." + name);
+    }
+
+    @Override
+    public void onFileDelete(File file) {
+        String name = file.getName();
+        log.info("文件被删除了..." + name);
+    }
+
+    @Override
+    public void onStop(FileAlterationObserver fileAlterationObserver) {
+        File file = fileAlterationObserver.getDirectory();
+        String name = file.getName();
+        log.info("结束监听..." + name);
+    }
+}

+ 46 - 0
src/main/java/com/hw/admin/system/listener/FileMonitor.java

@@ -0,0 +1,46 @@
+package com.hw.admin.system.listener;
+
+import org.apache.commons.io.monitor.FileAlterationMonitor;
+import org.apache.commons.io.monitor.FileAlterationObserver;
+
+public class FileMonitor {
+    private FileListener fileListener;  //监听事件
+    private String path;    //监听的文件路径
+    private FileAlterationMonitor monitor;
+    private long defaultInterval = 10000L;  //默认监听的时间间隔
+
+    public FileMonitor() {
+    }
+
+    public FileMonitor(String path) {
+        this.path = path;
+        this.monitor = new FileAlterationMonitor(defaultInterval);
+    }
+
+    public FileMonitor(String path, long interval) {
+        this.path = path;
+        this.monitor = new FileAlterationMonitor(interval);
+    }
+
+    public void setFileListener(FileListener fileListener) {
+        this.fileListener = fileListener;
+    }
+
+    public void start() {
+        if (monitor == null) {
+            throw new IllegalStateException("Listener must not be null");
+        }
+        if (path == null) {
+            throw new IllegalStateException("Listen path must not be null");
+        }
+
+        FileAlterationObserver observer = new FileAlterationObserver(path);
+        observer.addListener(fileListener);
+        monitor.addObserver(observer);
+        try {
+            monitor.start();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 26 - 0
src/main/java/com/hw/admin/system/task/WorkPackerTask.java

@@ -0,0 +1,26 @@
+package com.hw.admin.system.task;
+
+import com.hw.admin.model.service.WorkPackerService;
+import com.hw.admin.system.utils.DateExUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Component
+public class WorkPackerTask {
+    private static final Logger LOGGER = LoggerFactory.getLogger(WorkPackerTask.class);
+
+
+    @Autowired
+    private WorkPackerService workPackerService;
+
+    @Scheduled(cron = "0/60 * * * * ?")
+    public void executeScheduledTask2() {
+        String time = DateExUtils.commonDatePathStr();
+        LOGGER.info("包装机excel数据读取开始:" + time);
+        workPackerService.task(time);
+        LOGGER.info("包装机excel数据读取结束:" + time);
+    }
+}