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.
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.