Announcement Announcement Module
Collapse
No announcement yet.
How to fill the JobRegistry when starting IoC container? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to fill the JobRegistry when starting IoC container?

    Hello

    I am trying to start "myJob" with the SimpleJobOperator implementation like this:

    Code:
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/spike/processing1/myContext.xml"));
    JobOperator jobOperator = (JobOperator) factory.getBean("jobOperator");
    
    final Long jobId = jobOperator.start("myJob", "time=out");
    The error message is:

    INFO: Checking status of job with name=simpleJob
    Exception in thread "main" org.springframework.batch.core.launch.NoSuchJobExc eption: No job configuration with the name [myJob] was registered
    When I debug it, I see that the JobRegistry stays empty.

    This is the "myContext.xml":
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
    
        <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
            <property name="jobRepository" ref="jobRepository"/>
        </bean>
    
        <bean id="jobRepositoryTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="transactionManager" ref="jobRepositoryTransactionManager"/>
            <property name="databaseType" value="mysql"/>
            <property name="tablePrefix" value="batch_"/>
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            [...]
        </bean>
    
        <bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>
    
        <bean id="jobRegistryBeanPostProcessor"  class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
            <property name="jobRegistry" ref="jobRegistry"/>
        </bean>
    
        <bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
              p:dataSource-ref="dataSource"/>
    
        <bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator"
              p:jobLauncher-ref="jobLauncher"
              p:jobExplorer-ref="jobExplorer"
              p:jobRepository-ref="jobRepository"
              p:jobRegistry-ref="jobRegistry"/>
    </beans>
    and the "myJob.xml" file:

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:batch="http://www.springframework.org/schema/batch"
           xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <import resource="classpath:/com/spike/processing1/myContext.xml"/>
    
        <batch:job id="baseJob" abstract="true" incrementer="incrementer">
            <batch:listeners>
                <batch:listener ref="sampleListener"/>
            </batch:listeners>
        </batch:job>
    
        <batch:job id="myJob" parent="baseJob">
            <batch:validator ref="parametersValidator"/>
            <batch:step id="hello-step" next="space-step">
                <batch:tasklet ref="hello" transaction-manager="jobRepositoryTransactionManager"/>
            </batch:step>
            <batch:step id="space-step" next="world-step">
                <batch:tasklet ref="space" transaction-manager="jobRepositoryTransactionManager"/>
            </batch:step>
            <batch:step id="world-step">
                <batch:tasklet ref="world" transaction-manager="jobRepositoryTransactionManager"/>
            </batch:step>   
        </batch:job>
        
        <bean id="hello" class="com.spike.testjobs.simplejob.PrintTasklet">
            <property name="message" value="Hello"/>
        </bean>
    
        <bean id="space" class="com.spike.testjobs.simplejob.PrintTasklet">
            <property name="message" value=" "/>
        </bean>
    
        <bean id="world" class="com.spike.testjobs.simplejob.PrintTasklet">
            <property name="message" value="World!"/>
        </bean>
    
        <bean id="sampleListener" class="com.bc.spike.testjobs.simplejob.SampleListener"/>
        <bean id="parametersValidator"  class="org.springframework.batch.core.job.DefaultJobParametersValidator"/>    
        <bean id="incrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>
    </beans>
    How could the JobRegistry be filled at container startup?

    Best regards,
    Kieran
    Last edited by kieran; Feb 24th, 2012, 07:28 AM.

  • #2
    You may want to look at AutomaticJobRegistar

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

    http://static.springsource.org/sprin...b.html#d0e2250

    HTH,
    S.

    Comment


    • #3
      Don't use a BeanFactory use an ApplicationContext.

      Comment

      Working...
      X