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

I'm overriding the onFileCreate() method of the org.apache.commons.io.monitor.FileAlterationListener interface.

The method works, but I found that sometimes it spawns two threads and I don't fully understand what is triggering this behaviour.

WatchService Class

@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();
    }
}

Main Method

@SpringBootApplication
@EnableEurekaClient
public class DirectoryListenerApplication {

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

With the same file created in the folder sometimes the method logs two calls in two separated threads, sometimes the method log only one call in a single thread.

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"}

Is it possible to force the single thread behaviour ?



Solution 1:[1]

I removed the SprigBoot @Bean notation over my startFolderPolling method and now only one thread is created.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pickeroll