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

  • Spring Batch Integration and FileToJobLaunchRequestAdapter

    Hi guys,

    I'm trying to start a job from a file read and I running into some issues.

    First of all, even tho I added the Spring Batch Integration jar to the pom file, my xml can't find the FileToJobLaunchRequestAdapter class when I try to run it:

    Code:
    Cannot find class [org.springframework.batch.admin.integration.FileToJobLaunchRequestAdapter]
    My job config looks like below.

    Which seems pretty bloated to me. Are all these steps necessary?

    Code:
    <int:channel id="fileNameForJob" />
    		
    <int:service-activator id="jobStarter" input-channel="fileNameForJob" output-channel="jobLaunchRequestGen">
    	<bean class="org.springframework.batch.admin.integration.FileToJobLaunchRequestAdapter">
    		<property name="job" ref="companyLoadJob" />
    	</bean>
    </int:service-activator>
    	
    <int:channel id="jobLaunchRequestGen" />
    	
    <int:service-activator input-channel="jobLaunchRequestGen">
    	<bean class="org.springframework.batch.integration.launch.JobLaunchingMessageHandler">
    	     <constructor-arg ref="jobLauncher" />
    	</bean>
    </int:service-activator>
    	
    <bean id="jobLauncher"
    	    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    	  <property name="jobRepository" ref="jobRepository" />
    </bean>
    	
    <batch:job id="companyLoadJob" job-repository="jobRepository" >
    	<batch:step id="companyLoad" >
    		<batch:tasklet>
    			<batch:chunk reader="companyFileItemReader" writer="companyWriter" commit-interval="5" />
    		</batch:tasklet>
    	</batch:step>
    </batch:job>
    Finally, the reader defined above (it's a org.springframework.batch.item.file.FlatFileItemRe ader) I got from the batch samples and takes a property called resource which at the moment is hardcoded. How can I make this the dynamically passed filename?

    Code:
    <property name="resource" value="C:\blahblah\company.txt" />
    Thanks for any help,
    Cheers,
    Tony

  • #2
    The class you need is in the spring-batch-admin-manager jar...

    For the property use a step scoped reader and use the property input.file (which is set by the FileToJobLaunchRequestAdapter) as the resource.

    SOmething like this.
    Code:
    <property name="resource" value="${input.file}" />
    For the configuration you need to configure Spring Batch and your job so you indeed need soem configuration. (Although I would seperate the job and batch-infrastructure into seperate files). I would also mo FileToJobLaunchRequestAdapter and JobLaunchingMessageHandler into a chain saves you some xml and channel definition(s).

    Comment


    • #3
      Hi Marten,

      Thanks for the quick reply! And for pointing out the correct jar. Good tip re the Chain implementation too, it doesn't save a whole lot of space but it does look more structured at least. Yeah I have the batch job running now, when I got passed the file config there was also a lot of DB config that I didn't realise I had to do but this page helped me:

      http://static.springsource.org/sprin.../html/apb.html

      Thanks again for your help,
      Tony

      Comment


      • #4
        Hi again, at the moment I am using a FlatFileItemReader as the reader but when I add

        Code:
        <property name="resource" value="${input.file}" />
        I get the following exception:

        Code:
        Input resource must exist (reader is in 'strict' mode): class path resource [${input.file}]
        Since I don't have access to add

        Code:
        @Scope("step")
        Should I just implement my own custom reader? Or is there a way for the FlatFileItemReader to access the job parameters?

        Cheers,
        Tony
        Last edited by AnthonyRyan; Oct 18th, 2012, 05:30 AM. Reason: Fixed typo

        Comment


        • #5
          Why would you need to at a @Scope? Simply add scope="step" in your xml configuration for the step...

          Comment


          • #6
            Hmmm... I added scope="step" to the reader and recieved the following error:

            Code:
            org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.companyFileItemReader' defined in file [C:TestTestTest\target\classes\META-INF\spring\integration\spring-integration-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'input' of bean class [org.springframework.batch.core.scope.context.StepContext]: Bean property 'input' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
            	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:526)
            	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
            	at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:331)
            My reader definition now looks like:

            Code:
            <bean id="companyFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader">
            	<property name="resource" value="#{jobParameters[input.file.name]}" />
            I also tried the suggest with the dollar sign above, ${input.file} but recieved the same "Input resource must exist" error as before.

            Does the job parameter (input.file.name) have to be explicitly set?

            Comment


            • #7
              Does the job parameter (input.file.name) have to be explicitly set?
              That is what the FileToJobLaunchRequestAdapter does... And it should be the one you used with the #{} however it must be input.file not input.file.name (looking at the source and docs for FileToJobLaunchRequestAdapter that is).

              Also your reader definition doesn't contain scope="step" so not sure where you put it but at least not in the right p lace.

              Code:
              <bean id="companyFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
              	<property name="resource" value="#{jobParameters[input.file]}" />

              Comment


              • #8
                Ah, you're right, input.file fixed it, not input.file.name.

                Cheers for your time and help, dude.
                Tony

                Comment

                Working...
                X