Announcement Announcement Module
No announcement yet.
Is Batch for me? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is Batch for me?

    I am very new to Spring and it's proving difficult for me to determine if the Spring batch mechanism would be something I can utilize for a project requirement I have.

    Requirement: Poll a file system location for appearance of file (which in reality a new file would arrive each day - name is unique every time) and then parse the file (it is tab-delimitted) into a POJO structure (some fields may map directly to a parent POJO while other fields may map into child POJO instances which are members of the parent POJO). And then finally this object structure gets passed to a very business logic specific module for processing.

    Now, my initial low level Java gut reaction is, I create my own Thread that polls that file system, using a database to keep track of the files, and when a new file is found mark it on the database, read and parse it into my object structure, and pass it to my business logic module.

    However it looks like I may be able to use Spring batch or integration to really streamline this process but I really dont know where to begin.

    With using Spring batch, can I have it poll a file system location for files? Will it be able to ignore a header line? Will it be able to map the fields to a object tree structure instead of a single POJO?

    Here's a tough question - will I be able to configure it so that it can call my business logic module to process each parsed object tree line-by-line? In other words, for every record it reads off the file it needs to call my business logic module prior to continuing to the next record.

    So is batch for me or do I have to go back to low level coding?

    Can you give me any quick configuration example how this cane be accomplished?

  • #2

    Does this help?


    • #3
      Originally posted by DaveyS View Post
      Hello, Thanks for the reply!

      Actually I already came across that post doing searches, but it doesn't really help me.

      I setup my own test as best I can using the snippets of code they posted however I have no idea how the method should look like which is invoked by the service activator - i.e. how do I get the data being read from the file?

      Furthermore, there is no indication of how I can utilize Spring's Delimited Line Tokenizer classes to have each line of my input file parsed for me, or even mapped to an object.

      Here's all I was able to do with the example in that thread:

      	<file:inbound-channel-adapter id="input" directory="file:D:/input">
            <int:interval-trigger interval="250"/>
      	<int:service-activator input-channel="input" ref="demoReader" method="handleLine"/>
      	<bean id="demoReader" class="com.test.DemoReader" />
      And my class:

      package com.test;
      public class DemoReader {
      	public void handleLine () {
      		System.out.println("handleLine() called");
      And it works but it completely falls short of any of my requirements...

      for example, I pop a file into the D:\input folder and the process picks it up and I see output "handleLine() called". But it just prints it once, and my file I put in there has multiple lines of data. I expect to have my class called on each line.

      Also, how do I even get at the data parsed from the file? Does my class need to implement some interface or include some kind of object parameter in its handleLine() method? If so, which one?

      And ideally, the Spring batch framework can be configured so that I can supply it with a class structure, mapping and delimitter so that it automaticlaly parses each line and loads a POJO with the data parsed for me, which seems to be possible from whatever info I can find.

      But I just cannot find any sample of how to actually do this.


      • #4
        Doing some more research I think I am starting to understand more about this.

        So the service activator that I had used is just there to notify my processing class that the file is ready, right? At this point I could use Spring Batch in the processor's method to process the file. Such as using DelimitedLineTokenizer in my class.

        See I was thinking that I could set it up in the configuration file so that Spring picks up the file and starts parsing it into objects for me, which my processor then gets called on to act on each object parsed.

        Is there a way to still set it up like that?

        Basically I'd like to have as much of this process governed by the configuration file as possible, with only my processor class having some business logic on handling each parsed object.


        • #5
          the service activator should only trigger the Spring Batch job, using Spring Batch's JobLauncher. The service activator would look like this one: