3

I want to read from my application.properties , the input and output paths for my Spring Batch application, and set them to the jobParametersBuilder, so I can access them throughout the job execution (to assign them to readers and writers).

I was able to read from the application.properties in other configuration classes, but I can't seem to achieve doing it inside my main class. I need to do it here in order to be able to assign the value to the Job Parameters, before executing the job.

My Main Class:

@SpringBootApplication
public class GleBatchApplication {


 private static final Logger logger = 
 LogManager.getLogger(FormateadorJobConfig.class);

    @Value("${file.input}")
    private static String inputPath;

    @Value("${file.output}")
    private static String outputPath;


public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {


     ApplicationContext ctx = SpringApplication.run(GleBatchApplication.class, args);

     JobLauncher lanzadorJob = ctx.getBean(JobLauncher.class);
     Job jobFormateador = ctx.getBean("jobFormateador", Job.class);
     JobParameters jobParameters = new JobParametersBuilder().
             addLong("Time in miliseconds: ", System.currentTimeMillis())
             .addString("inputPath", inputPath)
             .addString("outputPath", outputPath)
             .toJobParameters();

        System.out.println("Valor leido del properties: " + inputPath);
        System.out.println("Valor leido del properties: " + outputPath);


     JobExecution jobExecution = lanzadorJob.run(jobFormateador, jobParameters);
     logger.info("=================================================");
     logger.info("START TIME: " + jobExecution.getCreateTime());
     logger.info("FINISH TIME: " + jobExecution.getEndTime());
     logger.info("=================================================");

My application.properties file:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url = 
jdbc:mysql://localhost:3306/curso_batch_multiplefilewriting_2? 
autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=mysql
spring.datasource.continueOnError=false

spring.batch.job.enabled=false


file.input = /inputFiles/GLEO-MN170100-PROCESO01-SUBDFACT-000001.txt
file.output = outputFiles/GLEO-MN1701-PROCESO001-SUBDFACT-FORMATDO-000001.txt

Also I have tried doing a separate configuration class, for the input and output, but i dont understand how to call it from my main class:

InOutConfiguration:

@Configuration
@PropertySource("classpath:application.properties")
public class InOutConfiguration {

@Value("${file.input}")
 private  String inputPath;

@Value("${file.output}")
 private  String outputPath;


@Bean
  public static PropertySourcesPlaceholderConfigurer   propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
  }

public  String getInputPath() {
    return inputPath;
}

public  void setInputPath(String inputPath) {
    this.inputPath = inputPath;
}

public  String getOutputPath() {
    return outputPath;
}

public  void setOutputPath(String outputPath) {
    this.outputPath = outputPath;
}

}

I'm getting inputPath = null , and outputPath = null.

Draken
  • 3,134
  • 13
  • 34
  • 54
Fede Lopez
  • 85
  • 2
  • 8

1 Answers1

2

The below code works (I believe you have your application.properties file in src/main/resources):

            Properties properties = new Properties();
            InputStream file = null;
            try {
                file = GleBatchApplication.class.getClassLoader().getResourceAsStream("application.properties");
                properties.load(file);
            }catch(Exception e) {
                //exception handling
            }

Place it in your main method and you can read the values from the "properties" variable. Like: properties.getProperty("spring.datasource.driverClassName");

Or you can place the above code in a different class and call the method to get properties. SO that you can use it where ever you want.

The Guest
  • 698
  • 11
  • 27
  • Hello The Guest . It makes sense, I will try it out today and tell you how it went. Thank you so much for answering – Fede Lopez Mar 26 '18 at 15:42
  • It worked like a charm. Now i would like to pass the input and output paths, through the CommandLineJobRunner and store them in file.input and file.output of the application.properties. How can I achieve that? thank you so much in advance!!! – Fede Lopez Mar 26 '18 at 17:23
  • @FedeLopez I don't know why you like to override application.properties. Because, again you need to read the file. Instead of that read the command line args in your code and set the args as system properties like System.setProperty("name", value); and to get it where ever you want use System.getProperty("name"); To parse the command line args see examples for org.apache.commons.cli.CommandLineParser – The Guest Mar 26 '18 at 18:14
  • Yes, your are right. I mix up the options i was thinking of. One of them, was to pass the paths through commandline into jobParameters, and use them throught the application. Thank you for the help! – Fede Lopez Mar 27 '18 at 13:39