Announcement Announcement Module
No announcement yet.
Spring Batch validations Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Batch validations

    I am working on Spring Batch and input source is text file.

    I should read each line and should be able to configure the mapping elements(String or date or Integer) to check whether the input file format is valid or not.
    I have used the mappers to specify the length of each attribute, but i should know whether the field read belongs to my specific data type or not.


  • #2
    Generally speaking, the FieldSetMapper will throw type-related errors when it detects problems. For instance, the*() methods automatically do the checking and exception throwing for you.


    • #3
      Originally posted by DHGarrette View Post
      Generally speaking, the FieldSetMapper will throw type-related errors when it detects problems. For instance, the*() methods automatically do the checking and exception throwing for you.

      Thanks Garrette, so if any of my field type changes, then again i have to change my java code, is there any way to set the field data types dynamically, ie, somewhere like in jobs.xml. so that i should not change my java code if my requirement changes.

      please let me know your concerns.


      • #4
        Take a look at the BeanWrapperFieldSetMapper. It automatically maps the object based on the property names.


        • #5
          I tried using BeanWrapper, but using this i am unable to configure the each field type.
          H:2006092900000030000030100200000000000000000D0000 0000000400000D
          T:200609290000000300001ORGBAL000415441301101379500 000000000200000D000

          My input has this text, first line is header record and second one transaction record.
          header record has, date(0-7), batchNumber(8-12),items, etc... fields

          now when we read the line, we can assign to header POJO, and now date has a value of 2006092. but if i get some characters while reading the text file instead of numbers i should immediately throw an Exception(saying Data issue(because we were expecting a int and found character values)) .

          i am expecting to validate similar to struts validator framework, so that i can configure date field by giving Expression values( 0-9 or a-z).
          so if the input field does not contain values between 0-9, i can directly throw an Exception saying invalid input or any ...

          by doing this way, we can say that the input file format is correct or not and then process according to the business requirement.

          Please let me know your concerns.
          Last edited by ramesh.kasarla; Jul 9th, 2009, 02:24 AM.


          • #6
            You can do this sort of checking in the FieldSetMapper (wrapping BeanWrapperFieldSetMapper) or LineMapper (wrapping DefaultLineMapper).

            Take a look at the ValidatingItemProcessor for an idea for how you might want to approach the validation. The tradeJob.xml, from the samples project, uses this processor so you can look at that too.


            • #7
              Hi Dan,

              i have used ValangValidator,

              <beans:bean id="tradeValidator" class="org.springframework.batch.item.validator.Sp ringValidator">
              <beansroperty name="validator">
              <beans:bean class="org.springmodules.validation.valang.ValangV alidator">
              <beansroperty name="valang">
              <![CDATA[{ isin : length(?) < 13 : 'ISIN too long' : 'isin_length' : 12}]]>

              I think this approach works if we have only one type of record in the input text file.
              Currently i have two types of records, i want to differentiate the two objects by alias name. if i use alias name, it is not working.

              can you help me out .



              • #8
                Perhaps you can create a sort of "Composite Validator" that uses a PatternMatcher to distinguish between record types. Take a look at PatternMatchingCompositeLineTokenizer for an example.


                • #9
                  Thanks Garrrette,

                  can i have any piece of sample code... its getting complex day by day.

                  Don't we have any spring batch validation framework to validate a Batch.



                  • #10
                    Hi Dan,
                    i have referred multilineOrderJob.xml for validating each field.

                    { customer.registered : customer.businessCustomer = FALSE OR ? = TRUE : 'Business customer must be registered' : 'error.customer.registration'}
                    { customer.companyName : customer.businessCustomer = FALSE OR ? HAS TEXT : 'Company name for business customer is mandatory' : 'error.customer.companyname'}

                    In the above code, alias name customer is used to refer its corresponding POJO, but it is not configured any where. when i used in the similar fashion i am getting an Exception( means it is not identifying the alias name)

                    can you help me out.