Spring batch - java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource

Rahul Raj

I have the below definition for FlatFileItemReader in s simple spring batch job:

@Bean
  public FlatFileItemReader<BookingInfo> bookingInfoReader() {
    FlatFileItemReader<BookingInfo> itemReader = new FlatFileItemReader<>();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    BlobClient blobClient =
        blobContainerClient.getBlobClient("path/file.csv");
    blobClient.downloadStream(outputStream);
    final byte[] bytes = outputStream.toByteArray();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    InputStreamResource resource = new InputStreamResource(inputStream);
    itemReader.setResource(resource);
    itemReader.setName("csvReader");
    itemReader.setLinesToSkip(1);
    itemReader.setLineMapper(lineMapper());
    return itemReader;
  }

But I get the error: java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times

Now, I changed the above code to use ByteArrayResource as shown below:

@Bean
  public FlatFileItemReader<BookingInfo> bookingInfoReader {

    FlatFileItemReader<BookingInfo> itemReader = new FlatFileItemReader<>();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    BlobClient blobClient =
        blobContainerClient.getBlobClient("path/file.csv");
    blobClient.downloadStream(outputStream);
    final byte[] bytes = outputStream.toByteArray();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    
    ByteArrayResource byteArrayResource = new ByteArrayResource(inputStream.readAllBytes());
    itemReader.setResource(byteArrayResource); 

    itemReader.setName("csvReader");
    itemReader.setLinesToSkip(1);
    itemReader.setLineMapper(lineMapper());
    return itemReader;
  }

Now the mentioned error goes away. However, there is a strange issue. Even if I update the file content, it is not fetching dynamically when the job is triggered. Instead, it is using the same old content. It takes modified content only after pod restart. I believe it is due to the fact that reader bean is created once and using it everytime.

So, I need to go back to InputStreamResource implementation by fixing the earlier mentioned issue. What is going wrong here?

Mahmoud Ben Hassine

I believe it is due to the fact that reader bean is created once and using it everytime.

Yes, the scope of the reader bean definition that you shared is singleton. Therefore, the same instance will be used for entire app lifetime and the file content will be the same.

You should make the reader step-scoped by adding @StepScope on its bean definition method to create a new instance on each run.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I use try-with-resources with an InputStream that has already been created?

java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times

java.lang.IllegalStateException: FragmentManager has been destroyed

java.lang.IllegalStateException: AssetManager has been finalized

ChildFragmentManager java.lang.IllegalStateException: Activity has been destroyed

Spring java.lang.IllegalStateException: Cannot create a session after the response has been committed

Java 8 Stream IllegalStateException: Stream has already been operated on or closed

java.lang.IllegalStateException: getReader() has already been called for this request

java.lang.IllegalStateException: FragmentManager has not been attached to a host

GlassFish issue with filter chain: java.lang.IllegalStateException: PWC3990: getWriter() has already been called for this response

Spring boot : java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

java.lang.IllegalStateException: Cannot call getInputStream() after getReader() has already been called for the current request

Spring Batch StoredProcedureItemReader for PostgreSQL - java.lang.IllegalStateException

I am getting an exception: java.lang.IllegalStateException: getOutputStream() has already been called for this response

Servlet - java.lang.IllegalStateException: getWriter() has already been called for this response

Android camera2: java.lang.IllegalStateException: maxImages (1) has already been acquired, call #close before acquiring more

java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted?

Caused by: java.lang.IllegalStateException: No value has been specified for this provider

java.lang.IllegalStateException: ViewPager has not been bound (Using JakeWharton ViewPager Indicator)

Trouble with Spring async request processing in Controller - getting java.lang.IllegalStateException: Cannot forward after response has been committed

Handling "Activity has been destroyed" (java.lang.IllegalStateException) exception?

java.lang.IllegalStateException: Request Data has already been read CQ5,AEM

java.lang.IllegalStateException: ServletConfig has not been initialized

REST - java.lang.IllegalStateException: getOutputStream() has already been called for this response

Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed

Spring Data GemFire - Caused by: java.lang.IllegalStateException: The connection pool "DEFAULT" has not been created

java.lang.IllegalStateException: stream has already been operated upon or closed in Java while working with list

Spring Boot 2.x reports java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

java.lang.IllegalStateException: ServletConfig has not been initialized in Tests

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive