realqmaster
12/4/2018 - 10:18 PM

CsvParser.java

package it.my.demo;

import java.io.FileNotFoundException;
import java.io.FileReader;
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.WatchKey;
import java.nio.file.WatchService;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;

@Service
public class CsvParser {

  @Autowired StudentRepo repo;

  private static final String path = "G:\\STS\\";

  public void sync(String filename) {
    try {

      CsvToBean<Student> csvToBean =
          new CsvToBeanBuilder<Student>(new FileReader(path + filename))
              .withType(Student.class)
              .withIgnoreLeadingWhiteSpace(true)
              .withSkipLines(1)
              .build();

      List<Student> students = csvToBean.parse();
      repo.saveAll(students);

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

  @Async(value = "threadPoolTaskExecutor")
  public void monitor() {
    try {
      WatchService watchService = FileSystems.getDefault().newWatchService();
      Path thePath = Paths.get(path);
      WatchKey watchKey =
          thePath.register(
              watchService,
              StandardWatchEventKinds.ENTRY_CREATE,
              StandardWatchEventKinds.ENTRY_MODIFY);

      while ((watchKey = watchService.take()) != null) {
        for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {

          String file = watchEvent.context().toString();
          if (file.startsWith("csv") && file.endsWith(".csv")) {

            System.out.println(
                "Event kind:"
                    + watchEvent.kind()
                    + ". File affected: "
                    + watchEvent.context()
                    + ".");
            
            sync(file);
          }
        }
        watchKey.reset();
      }

    } catch (IOException | InterruptedException e) {
      e.printStackTrace();
    }
  }
}
package it.my.demo;

import java.util.concurrent.Executor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@EnableAsync
public class PlaygroundApplication {

  @Autowired CsvParser parser;

  public static void main(String[] args) {
    SpringApplication.run(PlaygroundApplication.class, args);
  }

  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build();
  }

  @Bean
  public ApplicationRunner startDirectoryMonitorService() {
    return args -> parser.monitor();
  }

  @Bean(name = "threadPoolTaskExecutor")
  public Executor threadPoolTaskExecutor() {
    return new ThreadPoolTaskExecutor();
  }
}