Announcement Announcement Module
No announcement yet.
Unable to load multiple independent job definitions in Spring MVC Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to load multiple independent job definitions in Spring MVC

    I have written a Spring Batch application which will load 4 different database tables reading from 4 different CSV files. In my application I have the launch-context importing the 4 module-context files successfully. I am able to access any jobs via a getBean("job name") with no issues. When I move the same code to a Spring MVC application I am not able to import the launch.context, which has the definitions for the jobLauncher, dataSource, transactionManager, and jdbc:initializer, with less than one module-context (has job/bean definition) or more than two.

    So the question is why can I not do the following code snippet in an MVC application where the launch.context is preloaded by the root-context.xml, and why can I not have multiple jobs imported by the launch.context in an MVC? I ask because in my Spring MVC in order for me to @Autowired JobLauncher and Job I have to preload the launch.context which defines the beans. But if I can not include all jobs in one shot I get a job repository issue with the database tables already defined. I am using the memory database configuration as I have some restrictions in a cloud base solution.

    Your insight is greatly appreciated.

    Non Spring MVC the following works fine but in a MVC this would not since the bean can't be loaded from a single instance of a launch-content.xml

             ApplicationContext launchcontext = 
            		new ClassPathXmlApplicationContext("launch-context1.xml");
             JobLauncher jobLauncher = launchcontext.getBean(JobLauncher.class);
             Job job = (Job) launchcontext.getBean("filereader");
             JobExecution jobExecution =,
                     new JobParameters());
             System.out.println("Job Status: " + jobExecution.getStatus().toString());
             job = (Job) launchcontext.getBean("secondjob");
             jobExecution =,
                     new JobParameters());
             System.out.println("Job Status: " + jobExecution.getStatus().toString());
    In a non-Spring importing multiple resource files works fine, but in a MVC application this causes the @Autowired Job in the Controller to fail.

     	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${batch.jdbc.driver}" />
    		<property name="url" value="${batch.jdbc.url}" />
    		<property name="username" value="${batch.jdbc.user}" />
    		<property name="password" value="${batch.jdbc.password}" />
    	<bean id="jobLauncher" class="">
    		<property name="jobRepository" ref="jobRepository" />
     	<bean id="transactionManager" 
      	<jdbc:initialize-database data-source="dataSource">
    		<jdbc:script location="${batch.schema.script}" />
    	<batch:job-repository id="jobRepository" />
     	<import resource="classpath:META-INF/spring/module-context1.xml" />
       	<import resource="classpath:META-INF/spring/module-context2.xml" />
     	<import resource="classpath:META-INF/spring/module-context3.xml" />
     	<import resource="classpath:META-INF/spring/module-context4.xml" />
    All module-context files look the same, but have different job and bean names.

    	<batch:job id="filereader">
    	<batch:step id="read">
    	<batch:tasklet transaction-manager="transactionManager">
    	<batch:chunk reader="fileReader"
    	                writer="fileWriter" commit-interval="10" />
    	<bean name="fileReader"
     	    <property name="resource" value="file:data/test.txt" /> 
    	    <property name="lineMapper" ref="fileLineMapper" />
    	<bean name="fileLineMapper"
            <property name="fieldSetMapper" ref="fileMapper" />
            <property name="lineTokenizer" ref="fileLineTokenizer" />
    	<bean name="fileLineTokenizer"
    	    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" />