使 FileAlterationListenerAdaptor.onFileCreate() 始终是单线程,apache.commons.io.monitor

Make FileAlterationListenerAdaptor.onFileCreate() always single thread, apache.commons.io.monitor

我正在覆盖 org.apache.commons.io.monitor.FileAlterationListener 接口的 onFileCreate() 方法。

该方法有效,但我发现有时它会产生两个线程,我不完全理解是什么触发了这种行为。

WatchServiceClass

@Component
@Slf4j(topic="watchService")
public class WatchService {

    private static RestTemplate restTemplate;
    private static Environment env;

    @Autowired
    public WatchService(RestTemplate restTemplate, Environment env) {
        WatchService.restTemplate = restTemplate;
        WatchService.env = env;
    }


    //When a new file is created inside a folder, the file content is sent to kafka
    @Bean
    public static void startFolderPolling() throws Exception {
        FileAlterationObserver observer = new FileAlterationObserver(env.getRequiredProperty("folder"));
        FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
        log.info("setup completed");
        FileAlterationListener listener = new FileAlterationListenerAdaptor() {
            @Override
            public void onFileCreate(File file) {
                log.info("are you single thread ?");
                try {
                    String data = FileUtils.readFileToString(file, "UTF-8");
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.APPLICATION_JSON);

                    HttpEntity<String> entity = new HttpEntity<String>(data,headers);
                    log.info("Calling Kakfa microservice");
                    String answer = restTemplate.postForObject("http://kafka/api/messages/receiveSapOrder", entity, String.class);
                    log.info("sending SAP Order result:" + answer);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        observer.addListener(listener);
        monitor.addObserver(observer);
        monitor.start();
    }
}

主要方法

@SpringBootApplication
@EnableEurekaClient
public class DirectoryListenerApplication {

   public static void main(String[] args) throws Exception {
       SpringApplication.run(DirectoryListenerApplication.class, args);
       startFolderPolling();
   }
}

如果在文件夹中创建相同的文件,有时该方法会在两个单独的线程中记录两次调用,有时该方法只会在单个线程中记录一次调用。

2022-05-10 09:46:42.382  INFO 88330 --- [           main] watchService                             : setup completed
2022-05-10 09:46:42.397  INFO 88330 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SAP-LISTENER/192.168.2.63:sap-listener:8095 - registration status: 204
2022-05-10 09:46:57.394  INFO 88330 --- [       Thread-4] watchService                             : are you single thread ?
2022-05-10 09:46:57.423  INFO 88330 --- [       Thread-4] watchService                             : Calling Kakfa microservice
2022-05-10 09:46:58.788  INFO 88330 --- [       Thread-4] watchService                             : sending SAP Order result:{"message":"Uploaded the file successfully"}
2022-05-10 09:47:00.108  INFO 88330 --- [       Thread-2] watchService                             : are you single thread ?
2022-05-10 09:47:00.112  INFO 88330 --- [       Thread-2] watchService                             : Calling Kakfa microservice
2022-05-10 09:47:00.197  INFO 88330 --- [       Thread-2] watchService                             : sending SAP Order result:{"message":"Uploaded the file successfully"}

是否可以强制单线程行为?

我删除了 startFolderPolling 方法上的 SprigBoot @Bean 符号,现在只创建了一个线程。