Announcement Announcement Module
No announcement yet.
Access to JobParameters in ChunkListener Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access to JobParameters in ChunkListener

    ChunkListener is a simple interface just providing two simple beforeChunk() and afterChunk() methods.
    How can I access JobParameters, ChunkContext, etc in my ChunkListener ?

    More specifically, I need to set some ThreadLocals (like the current user) for my jobs to use.
    Is ChunkListener the right place for this ?


  • #2
    Did you try this: Not sure how that helps with your thread locals though because you didn't really describe the use case in any detail. Is it a multi-threaded step? Where does the data come from that you want in the thread local?


    • #3
      Yes actually I saw that later on. But I think not having the ChunkContext as a paramter to the before and after methods in a ChunkListener seems strange. I cant think of any reason why it's been omitted.

      For the second question, I normally have some running jobs which go through the domain objects and do some complicated business logic with them. Often I set the current user & its permissions in a thread local so they can use it any place they want. Normally in a web application we put these in a Servlet Filter, just like transaction management, so they're 'set' and 'clear'ed before and after the request response.
      I was asking if the is a standard place to set/clear these threadlocals in batch job just like a web app ? Around a Step ? A Chunk ? Or maybe it differs from usecase to usecase ?



      • #4
        How about the Job's execution context?


        • #5
          Job execution context would make it available everywhere but it is also persisted in the repository, so you would want to make sure that was not secret security credentials.

          An instance variable in a step component (reader writer etc) is the easiest approach if the data is the same for the whole step or job execution. If the component is step scoped, then the value is available everywhere in the step, but not visible to other executions.


          • #6
            My question isn't how to implements security credentials in spring batch, as that has been implemented with ThreadLocal's and used aggressively throughout my domain objects. Now i have to add some batch jobs to my app, so I just want the domain objects to behave as usual, without changing inside their code.

            I'm mostly talking about the thread management in spring batch, and want to know
            - from where a thread starts (or is picked) to execute a step or job
            - where does it finish and get released
            - is the same thread used for a whole job or step, is there any guarantee for this
            - do we have a thread pool
            - and what if i restart/resume a job, where does the running thread get assigned to the job.

            even a pointer to the code where this is managed would help me figure it out. Knowing these would help me find the right place for setting & clearing my threadlocals

            Thanks in advance


            • #7
              As far as I know one thread is started when job is started and can be used to process pass thrue all steps. If you are using task executor in any step then new thread will be created for each chunk. Pooling of thherads in this scenario depends on your taskExecutor implementation (e.g. obtained by JNDI can be managed by AS). If you restart a job in fact the same approach will be used but context saved informations will be used to skip already processed steps/items.