How to get resource in itemreader using spring batch? - spring

I am using spring batch for batch processing.
I am using MultiResourceItemReader to initialize the reader object. I did set the resources.
In FlatFileItemReader, i wanted to get the current filename so that i can manipulate data based on filename.

MultiResourceItemReader#getCurrentResource()
This returns the currentResource being read, and Resource has getFileName method which you can use to get the name.

Related

How to process property file values during server start up in Spring?

I have this entry in application.properties of my spring boot app:
myapp.urls = url1,url2,url3
In a method in my component, I am creating an array like below:
String myArray[] = properties.getmyAppUrls().split(",");
I want this array creation logic execute only once. I know we can achieve this using post construct. Is there any other way we could achieve this like during server start up?
I want this array constructed reading from a properties file during server start up and i want to use this in my component.
You can use Spring EL to do the job:
#Value("#{'${myapp.urls}'.split(',')}")
private List<String> myAppUrls;
I'd recommend to move this to a Configuration class, then you can autowire it everywhere you need it.

How to replace flatFileItemReader with openCSV in spring batch

I am using a multiResourceItemReader in spring batch. I found about openCSV that it automatically binds csv columns to java object. But how to replace the FlatFileItemReader/multiResourceItemReader, with openCSV using CsvToBeanBuilder.
The line to object mapping logic in Spring Batch is implemented in a LineMapper, which is used by the FlatFileItemReader to map read lines to domain objects.
So the question "How to replace flatFileItemReader with openCSV in spring batch" is incorrect, those are not at the same level. What you can do is create a LineMapper implementation based on OpenCSV and use it with the FlatFileItemReader.

spring-batch Read a file that contains file names to be ingested

I have the following case and I am trying to solve it with Spring-Boot and Spring-Batch. I have to read a flat file (sidecar file) in which every line is a name of a file to be ingested in a database.
I have configured a job to read the sidecar file, but I am having a problem to decide what is the accepted method in spring-batch to process the contained files. I also have configured steps that can read each file and insert the records in a data base.
Any ideas how to configure the sidecar job with the steps I have written for the individual files.
I can provide actual configuration from my implementation if needed.
FactoryBean is your friend. In this case, you can create a FactoryBean that reads the file of file names and creates a list of Resource objects for it. That can be injected into the MultiResourceItemReader which will iterate over that list.

Returning an object from a Spring Batch job / processor

I have a Spring Batch job that reads in a very large fixed length file and maps it just fine to an object. Validated all of the data in the associated processing task.
Being rather new to Spring and Spring Batch I am wondering if it is possible to get out of the job, a fully populated object to be used in a particular case when I am running the job as part of another process ( that I would like to have access to the data).
I realize that I could do the above without Batch, and it seems to be designed with scope limitations for its purpose.
I could serialize the objects in the processor and go that route but for my immediate satisfaction I am hoping there is a way to get around this.
Thanks
In my #configuration class for the batch processing, I created a class variable (it is a list of the object I want to get back) and instantiated with the no arg constructor.
My Step, ItemReader, LineMapper are setup to use a list for input. The custom FieldSetMapper takes that list instantiated from the constructor as a parameter and adds to the list as the file is read and mapped. Similarly my custom ItemProcessor takes the list as input and returns it.
Finally I created a ReturnObjectList bean that returns the populated list.
In my main I cast the AnnotationConfigApplicationContext getbean to the list of that object type. I am now able to use the list of objects generated from the fixed file in the scope of my main application.
Not sure if this is a healthy work around in terms of how Spring Java config is supposed to work, but it does give me what I need.

How to write a custom Flat File Item Reader

I am new to Spring batch. I wanted to ask how can I write a custom flat file item reader in Spring batch.
I know there is one generic FlatFileItemReader available in Spring batch, but we want to add some business logic while reading then how to write a custom flat file reader.
Thanks in advance
I have done same thing for MultiResourceItemReader, you can extend FlatFileReader and copy complete code of FlatFileReader. Then you can add your own method.
If the in-build logging provided in the FlatFileItemReader is not sufficient for your needs,
Grab the code for the FlatFileItemReader - https://github.com/spring-projects/spring-batch/blob/master/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java
Rename the class to you class-name/package name
Add loggers as needed and use
I don't recommend this - in more cases you would be better off debugging your code with a debugger.

Resources