java文件监听并实现业务需要

java文件监听并实现业务需要,第1张

java文件监听并实现业务需要 继承FileAlterationListenerAdaptor或实现FileAlterationListener接口
        ConditionalOnProperty注解可以根据yml配置文件中的配置项选择是否启用监听,prefix填写配置项,value填写启用监听的值,matchIfMissing会判断所填写的prefix对应的value是否为你填写的值,默认为false,即本段代码配置含义为:yml文件中filePath.enable值为1时,开启监听,其余值不启用监听,另外需要注意,由于继承了FileAlterationListenerAdaptor,所以无法使用@AutoWired注入业务类,本段代码使用构造器注入业务层
package com.zeei.ems.external.util;

import com.zeei.ems.external.service.OilMeterDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import java.io.File;


@Slf4j
@Component
@ConditionalOnProperty(prefix = "filePath.enable",value = "1",matchIfMissing = false)
public class FileListener extends FileAlterationListenerAdaptor {


    private OilMeterDataService omdService;

    
    public FileListener(OilMeterDataService omdService) {
        this.omdService = omdService;
    }


    
    public void onFileCreate(File file) {
        log.info("[新建]:" + file.getAbsolutePath());
        // 对应业务代码
        omdService.insertFileListen(file);
    }

    
    public void onFileChange(File file) {
        log.info("[修改]:" + file.getAbsolutePath());
    }

    
    public void onFileDelete(File file) {
        log.info("[删除]:" + file.getAbsolutePath());
    }

    
    public void onDirectoryCreate(File directory) {
        log.info("[新建]:" + directory.getAbsolutePath());
    }

    
    public void onDirectoryChange(File directory) {
        log.info("[修改]:" + directory.getAbsolutePath());
    }

    
    public void onDirectoryDelete(File directory) {
        log.info("[删除]:" + directory.getAbsolutePath());
    }

    public void onStart(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStart(observer);
    }

    public void onStop(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStop(observer);
    }

}

创建监听器

package com.zeei.ems.external.util;

import com.zeei.ems.external.dao.OilMeterDataDao;
import com.zeei.ems.external.service.OilMeterDataService;
import com.zeei.ems.external.vo.FileListenVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Component
public class FileListenUtil {

    // 设置监听路径
    @Value("${filePath.listenPath}")
    private String monitorDir;


    // 设置轮询间隔
    private final long interval = TimeUnit.SECONDS.toMillis(1);

    @Autowired
    private OilMeterDataService service;


    public FileAlterationMonitor getMonitor() {
        // 创建过滤器
        IOFileFilter directories = FileFilterUtils.and(
                FileFilterUtils.directoryFileFilter(),
                HiddenFileFilter.VISIBLE);
        IOFileFilter files       = FileFilterUtils.and(
                FileFilterUtils.fileFileFilter(),
                FileFilterUtils.suffixFileFilter(".docx"));
        IOFileFilter filter = FileFilterUtils.or(directories, files);
        // 使用过滤器
//        FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir), filter);
        //不使用过滤器,相当于监听所有类型文件
        FileAlterationObserver observer = new FileAlterationObserver(new File(monitorDir));
        observer.addListener(new FileListener(service));
        //创建文件变化监听器
        FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
        // 开始监控
        return monitor;
    }

}

创建springboot启动时执行任务,实现CommandLineRunner接口

package com.zeei.ems.external.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class FileListenerRunner implements CommandLineRunner {

    @Autowired
    private FileListenUtil fileListenUtil;

    @Override
    public void run(String... args) throws Exception {
        FileAlterationMonitor monitor = fileListenUtil.getMonitor();
        try {
            monitor.start();
        } catch (Exception e) {
            log.error(">>>>>>>>>>文件监听报错: " + e);
        }
    }
}

 

 至此,文件监听大功告成!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5678256.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存