SpringBoot使用线程池监测某文件夹内新增文件
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Component
public class TestDemo implements ApplicationRunner {
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
getFile();
}
private static String path = "F:\\JavaDemo";
public static void getFile() throws FileNotFoundException, IOException {
ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(2);
scheduled.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
WatchKey key;
try {
WatchService watchService = FileSystems.getDefault