简体   繁体   English

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

[英]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.我正在覆盖org.apache.commons.io.monitor.FileAlterationListener接口的onFileCreate()方法。

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 WatchService 类

@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 ?是否可以强制单线程行为?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 org.apache.commons.io.monitor 表现多线程? - How to make org.apache.commons.io.monitor behave multi-threaded? Android上的Apache Commons IO - Apache Commons IO on Android Java和org.apache.commons.io.input.Tailer的日志监控器问题 - Log monitor problem with Java and org.apache.commons.io.input.Tailer Commons IO(Apache)copyURLToFile不起作用 - Commons IO (Apache) copyURLToFile Not Working Apache Commons IO Tailer 示例 - Apache Commons IO Tailer example 使用Spring和Apache Commons IO的Web应用程序和目录观察器的线程问题 - Thread Problems with Web Application and Directory Watcher using Spring and Apache Commons IO 线程“主”中的java异常java.lang.NoClassDefFoundError:org / apache / commons / io / FileUtils - java Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils 在命令提示符下面对“线程“ main”中的异常“ main” java.lang.NoClassDefFoundError:org / apache / commons / io” - Facing “Exception in thread ”main“ java.lang.NoClassDefFoundError: org/apache/commons/io” in command prompt 使用Java删除文件时出现问题(apache commons io) - Problems deleting a file with Java (apache commons io) 将Apache commons IO添加到我的项目中 - Adding Apache commons IO to my project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM