jdk监听文件系统目录

package com.ecoman.tlog;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;

import org.apache.log4j.Logger;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.ecoman.weixin.entity.EcoFile;

import java.nio.file.WatchKey;
import java.nio.file.WatchService;

import javax.annotation.PostConstruct;
@Service
public class TlogWatchService {

	private static Logger logger = Logger.getLogger(TlogWatchService.class);
	
	private WatchService watchService;
	
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	@Autowired
	private SystemService systemService;

	public TlogWatchService() throws IOException {
		Path path = Paths.get(ResourceUtil.getConfigByName("demoUpload"));
		this.watchService = FileSystems.getDefault().newWatchService();
		path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
	}
	
	public void setThreadPoolTaskExecutor(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
		this.threadPoolTaskExecutor = threadPoolTaskExecutor;
	}

	@PostConstruct
	public void handleEvents() throws InterruptedException{
		logger.info("开启文件监听服务..."+this);
		threadPoolTaskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					while(true){
						WatchKey key = watchService.take();
						for(WatchEvent<?> event : key.pollEvents()){
							Kind<?> kind = event.kind();
							if(kind == StandardWatchEventKinds.OVERFLOW){
								continue;
							}
							@SuppressWarnings("unchecked")
							WatchEvent<Path> e = (WatchEvent<Path>) event;
							Path fileName = e.context();
							String name = String.valueOf(fileName);
							logger.info("监听到新增文件,文件名:" + name);
							if(name.contains(".tlog")){
								methodInvoke(name);
							}
						}
						if(!key.reset()){
							break;
						}
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 解析地防文件tlog为tx文件,并更新文件路径和pause_type状态
	 * @param fileName
	 */
	public void methodInvoke(String fileName){
		AnalysisTlog.analysisLocation(fileName);
		String txFilePath = ResourceUtil.getConfigByName("tlogUpload") + File.separator + fileName.replace("tlog", "tx");
		EcoFile ecoFile = systemService.get(EcoFile.class, fileName.substring(0, fileName.indexOf(".")));
		ecoFile.setPath(txFilePath);
		ecoFile.setPauseType("1");
		systemService.saveOrUpdate(ecoFile);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值