简体   繁体   中英

How to read the number of text files from folder using standalone jar

I have a spring boot project which reads a textfile from an input folder and generates a pdf report.

public void generateReport(String inputTextFileWIthPath, String compiledJasperFile, String outFilePath) {
        try {
            
            String[] columnNames = ["column1","column2","column3"] ;
            

            File jasperFile = new File(compiledJasperFile);

            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFile);
            // Map<String, Object> parameters = new HashMap<String, Object>();
            JRCsvDataSource dataSource = new JRCsvDataSource(inputTextFileWIthPath);
            dataSource.setFieldDelimiter('|');
            dataSource.setColumnNames(columnNames);
            //Map data with fields
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("_dd-MM-yyyy_HH-mm-ss");
            LocalDateTime now = LocalDateTime.now();
                
        
            String reportFileName = "report" + dtf.format(now) + ".pdf";

          

            JasperExportManager.exportReportToPdfFile(jasperPrint, outFilePath + reportFileName);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • I am able to generate the report with the above code.

But the scenario is

  • Read all the text files from the input folder and generate the
    report files for each input text file and store them in the output folder.

  • If any report generate fails, store the failure message in db table.

  • Also, The application should run as a stand alone jar. How to define entry point so that program starts reading the files from input
    folder when we run the jar(using scheduler).

    Trying to figure out what is the better approach to implement this. Can use Executor service to read all the files from input folder or is there a better approach to achieve this.

Java executable jar excluding embedded tomcat

So if you need a executable Jar without spring boot, you just need to remove this in you main method:

public static void main(String[] args) {
    //SpringApplication.run(Application.class, args); <---- remove this line
    add in your business logic here!
}

This would be your entry point already.

The next is, based on your build tools, either ant, maven or gradle, run the command accordingly, you will get your final jar file.

Then you can execute java -jar xxx.jar to run your program.

If you take out spring boot, you can't use the spring boot scheduler, you need to handle it separately.

As for the Executor service , you don't really need it unless you want to control the maximum threads (in thread pool) in your app, you just need to for loop to take in all the input and call your method generateReport .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM