Results 1 to 3 of 3

Thread: Problems starting ApplicationContext - endless loop

  1. #1
    Join Date
    Feb 2012
    Posts
    12

    Default Problems starting ApplicationContext - endless loop

    Hello,

    I have a file 'simpleJob' with the spring batch job definition which imports a file 'applicationContext.xml' like:
    Code:
    <import resource="classpath:/com/bc/springbatch/spike/helloworld/applicationContext.xml"/>
    When I try to start the spring application context and then to retrieve the JobOperator bean, the construction of ClassPathXmlApplicationContext ends up in an endless loop (refresh) and never returns to the main method. Here is the code:

    Code:
    public class Main {
    
        public static void main(String[] args) throws Exception {
    
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"simpleJob.xml"}, Main.class); //never returns from this call
            
            JobOperator jobOperator = (JobOperator) context.getBean("jobOperator"); //never reach this line
            Set<String> jobNames = jobOperator.getJobNames();
            for (String jobName : jobNames) {
                System.out.println("jobName = " + jobName);
            }
           //do a lot more with jobOperator
        }
    }
    What is wrong?

    Kind regards,
    Kieran

  2. #2
    Join Date
    Feb 2012
    Posts
    12

    Default

    Hello,

    I changed:

    <bean class="org.springframework.batch.core.configuratio n.support.AutomaticJobRegistrar">
    <property name="applicationContextFactories">
    <bean class="org.springframework.batch.core.configuratio n.support.ClasspathXmlApplicationContextsFactoryBe an">
    <property name="resources" value="classpath*:com/bc/springbatch/spike/helloworld/simpleJob.xml"/>
    </bean>
    </property>
    <property name="jobLoader">
    <bean class="org.springframework.batch.core.configuratio n.support.DefaultJobLoader">
    <property name="jobRegistry" ref="jobRegistry"/>
    </bean>
    </property>
    </bean>


    with:

    <bean class="org.springframework.batch.core.configuratio n.support.JobRegistryBeanPostProcessor">
    <property name="jobRegistry" ref="jobRegistry"/>
    </bean>

    in my 'applicationContext.xml' and now it works fine.

    Still I do not understand, why. At the moment I would guess: Should the AutomaticJobRegistrar be used only with a spring container, which is instantiated via web.xml?

  3. #3

    Default

    Well, if your simpleJob.xml contins the automatic job registar, you will end up in a infinite loop since every time you load that file, you create a bean whose purpose is to load the very same file.

    Your automatic job registar definition should never be in the job definition itself. The purpose is to create a child application context for the need of your job. You would define your reader/writer/processor, etc. All your infrastructure would be defined in the main context (what you call applicationContext.xml I believe).

    HTH,
    S.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •