Announcement Announcement Module
Collapse
No announcement yet.
Usage example for BatchResourceFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Usage example for BatchResourceFactoryBean

    Hi,

    I am very interested in this project. I am watching results for a while. Now , I am trying to use spring-batch for new project in my company.

    I have successfuly install it, and run samples.

    But, I can not make BatchResourceFactoryBean to work. Can you provide configuration for fixedLengthImportJob?

    I tried with:

    <bean id="fileLocator"
    class="org.springframework.batch.execution.resourc e.BatchResourceFactoryBean"
    scope="step">
    <property name="rootDirectory" value="file://c/gsm2/data/inct" />
    <property name="filePattern" value="%BATCH_ROOT%/%JOB_NAME%" />
    <aop:scoped-proxy />
    </bean>

    The error I receive is:

    Error creating bean with name 'scopedTarget.fileLocator': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope

    Thanks in advance

  • #2
    What version of spring are you using?

    Comment


    • #3
      Spring 2.5.1

      Comment


      • #4
        This usually occurs when you try to use a "step" scoped bean from within a singleton. Think about it this way. You have a singleton, so every time it is referenced the same object is returned. Initialization of that object (including resolution of its references) occurs only once, the first time it is needed. This means that there is no lifecycle "hook" at the beginning and end of the step lifecycle from the Spring framework's perspective. In order to resolve this, you need to define your bean as a scoped proxy using the <aop:scoped-proxy /> tag. This causes your bean to be defined as a Proxy whose invocation handler redirects all calls to the appropriate instance of the class for the step. This allows Spring to keep track of and update the bean according to the rules defined in the framework, and manage the bean's lifecycle appropriately (calling init-method, destroy-method, afterPropertiesSet() and destroy() at the appropriate times).

        I tried to make this explanation clear, potentially at the expense of being 100% technically accurate (someone else please feel free give a better technical explanation), but nonetheless using the scoped proxy tag should fix the issue for you. If it doesn't, post again and let me know.

        Comment


        • #5
          Apologies! I didn't see you already had the tag in your configuration. That's what I get for posting at 3:30am.

          Anyway, could you please post more of your configuration for context?

          Thanks

          Comment


          • #6
            This might not be the same issue, but I noticed recently that you get this error if spring-aop.jar is not on your classpath. The error message is confusing so I raised an issue against Spring Core and it is fixed in 2.5.2.

            Comment


            • #7
              I have spring.jar in my classpath (ver 2.5.1). I have make a comparison od content of spring-jar and spring-aop.jar and it seems (I am not 100% sure) that all content of spring-aop.jar is in spring.jar.

              To reproduce error just replace:

              Code:
              <bean id="fileLocator"
              	class="org.springframework.core.io.ClassPathResource">
              	<constructor-arg type="java.lang.String"
              		value="data/fixedLengthImportJob/input/20070122.teststream.ImportTradeDataStep.txt" />
              </bean>
              whit this:

              Code:
              <bean id="fileLocator"
              	class="org.springframework.batch.execution.resource.BatchResourceFactoryBean"
              	scope="step">
              	<property name="rootDirectory" value="file://c/gsm2/data/inct" />
              	<property name="filePattern" value="%BATCH_ROOT%/%JOB_NAME%" />
              	<aop:scoped-proxy />
              </bean>
              in fixedLengthImportJob.xml, and run that job through BatchCommandLineLauncher

              Comment


              • #8
                What is your result if you use your configuration without the <aop:scoped-proxy /> tag. It seems to me you shouldn't need that bean to be a proxy since it is referenced from a "step"-scoped scoped proxy bean already. That is, assuming your job configuration as a whole is similar in that way to the fixedLengthImportJob.
                Last edited by dkaminsky; Jan 28th, 2008, 03:59 AM. Reason: added caveat

                Comment


                • #9
                  It is the same with <aop:scoped-proxy /> and tithout it.

                  Could you reproduce error according to my instructions?

                  Comment


                  • #10
                    Any suggestions? I am working hard to resolve this issue, but no progress...

                    Comment


                    • #11
                      I was able to reproduce the error as well, thanks for the detail. I'll try and figure out what's going wrong.

                      Comment


                      • #12
                        I made the change to the fixedlengthimportjob to use a BatchResourceFactoryBean, and I did get the same exception. The problem is that the unit test has the locator and input source wired in via autowiring. I pulled all of this out by creating them by hand in the test and everything worked, with the exception of a bug in milestone 4 that was introduced last week, that shouldn't be an issue in milestone 3.

                        So, if you just modified the fixedLengthImportJob to add in your classes, and the unit test is still the same, you should be able to fix it the same way I did.

                        Comment


                        • #13
                          Hi Lucas,

                          I was off a few days.

                          I tried to follow your instructions, but with no success.
                          Can you please post sample code and configuration.

                          Thanks in advance

                          Comment


                          • #14
                            I did have an example in the Samples, but it got rolled back for other reasons. Just to make sure it's the actual problem, how exactly are you launching your job? Did you just modify the FixedLengthImportJob with your own classes and reuse the same unit test that launches it?

                            Comment


                            • #15
                              I am working with m3 version of spring-batch (zip download).
                              I have modified fileLocator to :

                              Code:
                              <bean id="fileLocator"
                              	class="org.springframework.batch.execution.resource.BatchResourceFactoryBean"
                              	scope="step">
                              	<property name="rootDirectory" value="file://c/gsm2/data/inct" />
                              	<property name="filePattern" value="%BATCH_ROOT%/%JOB_NAME%" />
                              	<aop:scoped-proxy />
                              </bean>
                              and than run job with BatchCommandLineLauncher.

                              Comment

                              Working...
                              X