Announcement Announcement Module
Collapse
No announcement yet.
How to read the resource File Name in Item processor/ItemWriter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to read the resource File Name in Item processor/ItemWriter

    Hello,

    I am doing a POC for one of my project with Spring batch. I am using flatFileItemReader to read the file and ceated my Custom Item Processor and ItemWriter. I have a requriement where I need to persist the resource file name with its full absoulte path in the database. I could not find any way to read the reader or its any of the attribute in ItemProcessor and/or ItemWriter.

    Please, help.

    Thanks in Advance.

  • #2
    Perhaps it makes sense to read the resource directly from your spring context by implementing ApplicationContextAware. Or you can inject the same resource into two places, in FlatFileItemReader and also the ItemProcessor.

    Comment


    • #3
      Thanks Robert for your reply. On which class you suggestes to impelment ApplicationContextAware as we have the resoruce property with flatFileItemReader or on ItemProcessor.

      If we inject the same resource with ItemProcessor it will be kind of hardcoidng which I am doing right now and which is very error prone.

      Comment


      • #4
        I mean you can declare the resource as a standalone bean and inject this resource bean in both FlatFileItemReader and ItemProcessor. So you end up with single shared resource definition which shouldn't be error-prone at all.

        Comment


        • #5
          Resource is a property in FlatFileItemReader, I mean its a name of the file, its not a class. So, how can we define a bean for it?

          Folloiwng will not work as SIT001K is not a class but a simple flat file.
          <bean id="inputResource" value="com.sopra.evolansa.cardpayment.job.context. SIT001K"/>

          Comment


          • #6
            Here's an example:
            <bean id="res" class="org.springframework.core.io.FileSystemResou rce">
            <constructor-arg>test.txt</constructor-arg>
            </bean>

            Comment


            • #7
              But I am sorry to tell you that FlatFileItemReader take reasource as a property not as a Object. So we can not refer this to FlatFileItemReader.

              Though, I put resource in a property file as a property and refered it as a property in FlatFileItemReader & ItemProcessor.

              But I think it is not a best way to do it.

              I wonder why there is no way to read ExecutionContext inside a step. There are only two method afterStep and beforeStep where we can have that. But I need that at many places in between step. Like I want to know my currentIndex of reader.

              Any clues?

              Comment


              • #8
                Robert's suggestion of using a Resource bean definition seems sensible to me, so I don't know why you think it won't work.

                Originally posted by goel_veenu View Post
                I wonder why there is no way to read ExecutionContext inside a step. There are only two method afterStep and beforeStep where we can have that. But I need that at many places in between step. Like I want to know my currentIndex of reader.
                You need an ItemStream?

                Comment

                Working...
                X