Search code examples
javaspringspring-bootelasticsearchjobs

Create a simple job spring boot


I created a spring boot project. I use spring data with elastic search. The whole pipeline: controller -> service -> repository is ready.

I now have a file that represents country objects (name and isoCode) and I want to create a job to insert them all in elastic search. I read the spring documentation and I find that there's too much configuration for such a simple job. So I'm trying to do a simple main "job" that reads a csv, creates objects and insert them in elastic search.

But I have a bit of trouble to understand how injection would work in this case:

@Component
public class InsertCountriesJob {

private static final String file = "D:path\\to\\countries.dat";
private static final Logger LOG = LoggerFactory.getLogger(InsertCountriesJob.class);

@Autowired
public CountryService service;

public static void main(String[] args) {
    LOG.info("Starting insert countries job");
    try {
        saveCountries();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static void saveCountries() throws Exception {
    try (CSVReader csvReader = new CSVReader(new FileReader(file))) {
        String[] values = null;
        while ((values = csvReader.readNext()) != null) {
            String name = values[0];
            String iso = values[1].equals("N") ? values[2] : values[1];
            Country country = new Country(iso, name);
            LOG.info("info: country: {}", country);
            //write in db;
            //service.save(country); <= can't do this because of the injection
        }
    }
}
}

Solution

  • based on Simon's comment. Here's how I resolved my problem. Might help people that are getting into spring, and that are trying not to get lost. Basically, to inject anything in Spring, you'll need a SpringBootApplication

    public class InsertCountriesJob implements CommandLineRunner{
    
    private static final String file = "D:path\\to\\countries.dat";
    private static final Logger LOG = LoggerFactory.getLogger(InsertCountriesJob.class);
    
    @Autowired
    public CountryService service;
    
    public static void main(String[] args) {
        LOG.info("STARTING THE APPLICATION");
        SpringApplication.run(InsertCountriesJob.class, args);
        LOG.info("APPLICATION FINISHED");
    }
    
    @Override
    public void run(String... args) throws Exception {
        LOG.info("Starting insert countries job");
        try {
            saveCountry();
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOG.info("job over");
    }
    
    public void saveCountry() throws Exception {
        try (CSVReader csvReader = new CSVReader(new FileReader(file))) {
            String[] values = null;
            while ((values = csvReader.readNext()) != null) {
                String name = values[0];
                String iso = values[1].equals("N") ? values[2] : values[1];
                Country country = new Country(iso, name);
                LOG.info("info: country: {}", country);
                //write in db;
                service.save(country);
            }
        }
    }
    
    
    }