Announcement Announcement Module
No announcement yet.
error while invoking programatically instead of by CommandLineJobRunner Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • error while invoking programatically instead of by CommandLineJobRunner

    Hi Friends,
    I am trying to load the bean-definition for spring-batch & spring-hadoop dynamically at run time not at the time of starting the application. The application load the bean-definition at any time and invoke the spring-batch job. Problem is while running it using CommandLineJobRunner it is working fine but while running it pragmatically it is throwing some error.
    When I run a spring-batch-job ( ie mapreduce jobs ) using mvn command line it works fine for me. -
    mvn exec:java -Dexec.args="context3.xml secondJob"

    But when I try to run progamatically it got the error -
    mvn exec:java -Dexec.mainClass="org.test.SecondExample"
    Error I got -
    12/07/04 13:55:07 INFO support.SimpleJobLauncher: Job: [FlowJob: [name=secondJob]] launched with the following parameters: [{programatic_attempt=AAAAA}]
    12/07/04 13:55:07 INFO job.SimpleStepHandler: Executing step: [firstJobStep]
    12/07/04 13:55:07 ERROR job.AbstractJob: Encountered fatal error executing job
    org.springframework.batch.core.JobExecutionException: Flow execution ended unexpectedly
            at org.springframework.batch.core.job.flow.FlowJob.doExecute(
            at org.springframework.batch.core.job.AbstractJob.execute(
            at org.springframework.core.task.SyncTaskExecutor.execute(
            at org.test.SecondExample.main(
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(
            at java.lang.reflect.Method.invoke(
            at org.codehaus.mojo.exec.ExecJavaMojo$
    Caused by: org.springframework.batch.core.job.flow.FlowExecutionException: Ended flow=secondJob at state=secondJob.firstJobStep with exception
            at org.springframework.batch.core.job.flow.FlowJob.doExecute(
            ... 11 more
    Caused by: java.lang.NullPointerException
            at org.springframework.batch.core.step.AbstractStep.execute(
            at org.springframework.batch.core.job.SimpleStepHandler.handleStep(
            at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(
            ... 13 more
    12/07/04 13:55:07 INFO support.SimpleJobLauncher: Job: [FlowJob: [name=secondJob]] completed with the following parameters: [{programatic_attempt=AAAAA}] and the following status: [FAILED]

    Java Code that dynamically load the bead-definition for spring-batch & spring-hadoop-
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        Resource resource = context.getResource("context3.xml");
        if(resource != null){System.out.println(" Resoruce is not null ");}
        GenericApplicationContext ctx = new GenericApplicationContext();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx);
        int i_resource = reader.loadBeanDefinitions(resource);JobLauncher launcher = (JobLauncher) ctx.getBean("jobLauncher");
        if( launcher != null ){
    		System.out.println("JobLauncher is not null");
             System.out.println("JobLauncher is null"); return;
        Job job = (Job) ctx.getBean("secondJob");
        if( job != null ){
             System.out.println("Job is not null");
             System.out.println("Job is null"); return;
        }, new JobParametersBuilder().addString("programatic_attempt", "AAAAA").toJobParameters());
    Any suggession what is going wrong
    Last edited by paragflume; Jul 4th, 2012, 05:57 AM.

  • #2
    Your programmatic code is a mess. You load an application context only to load another resource and then you load an empty application context and then you use the bean definition reader manually....
    There are numerous examples in our test suite, the docs or google on how to setup an application context.
    Just start with GenericXmlApplicationContext (use the constructor that accepts a string/resource) and you're good to go.