Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: More Hello world issues - TaskletStep, StepExecutionListener

  1. #1

    Default More Hello world issues - TaskletStep, StepExecutionListener

    Hello,

    I have also tried my luck with the Hello World example, and made it run in its basic configuration. I then tried to expand it, so that I can save details about the jobExecution and the stepExecution in the executionContext.

    I did it by implementing the StepExecutionListener interface in the Tasklet class. That gives me access to the stepExecution (without changing the configuration of the job ??!), but the Id (of the stepExecution) is null.

    Can any of you guide me in the right direction ? My goal is to get access to the stepExecutionId and the jobExecutionId from the Tasklet class.

    helloWorldJob.xml
    Code:
    <beans xmlns=...>  
                                       
      <import resource="helloWorldApplicationContext.xml"/>  
    
      <bean id="printtasklet" class="dk.bec.helloworld.PrintTasklet"/>  
             
      <bean id="hello" parent="printtasklet">
          <property name="message" value="Hello"/>  
      </bean>  
           
      <bean id="space" parent="printtasklet">
          <property name="message" value=" "/>  
      </bean>  
          
      <bean id="world" parent="printtasklet">
          <property name="message" value="World!"/>  
      </bean>  
      
      <bean id="taskletStep" abstract="true" class="org.springframework.batch.core.step.tasklet.TaskletStep">  
          <property name="jobRepository" ref="jobRepository"/>  
    <!--  The below configuration line gives "No property 'listeners' found" -->
    <!--  <property name="listeners" ref="printtasklet"/> -->
      </bean>  
           
      <bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob">  
          <property name="name" value="simpleJob" />  
          <property name="steps">  
              <list>  
                  <bean parent="taskletStep">  
                      <property name="tasklet" ref="hello"/>  
                  </bean>  
                  <bean parent="taskletStep">  
                      <property name="tasklet" ref="space"/>  
                  </bean>  
                  <bean parent="taskletStep">  
                      <property name="tasklet" ref="world"/>  
                  </bean>  
              </list>  
          </property>  
          <property name="jobRepository" ref="jobRepository"/>  
      </bean>  
    </beans>
    PrintTasklet:
    Code:
    package dk.bec.helloworld;
    
    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.StepExecutionListener;
    import org.springframework.batch.core.step.tasklet.Tasklet;
    import org.springframework.batch.repeat.ExitStatus;
    
    public class PrintTasklet implements Tasklet, StepExecutionListener{   
    	  
    	  private String message;
    	  StepExecution stepExecution;
    	  
    	  public void setMessage(String message) {
    	      this.message = message;
    	  }   
    
    	  public ExitStatus execute() throws Exception {   
    	      System.out.print(message);   
    	      stepExecution.getExecutionContext().putString("Message", message);
    	      stepExecution.getExecutionContext().putDouble("Time", System.currentTimeMillis());
    	      stepExecution.getExecutionContext().putLong("JobExecutionId", stepExecution.getJobExecutionId().longValue());
    
    //		  The .getId() gives java.lang.NullPointerException:	      
    //	      stepExecution.getExecutionContext().putLong("Id", stepExecution.getId().longValue());
    
    	      return ExitStatus.FINISHED;   
    	  }
    
    	public ExitStatus afterStep(StepExecution stepExecution) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	public void beforeStep(StepExecution stepExecution) {
    		this.stepExecution = stepExecution;
    	}
    
    	public ExitStatus onErrorInStep(StepExecution stepExecution, Throwable e) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    }

  2. #2

    Default

    The id is null because StepExecution is not saved yet. Do you really need the value for some reason?

  3. #3

    Default

    Quote Originally Posted by robert.kasanicky View Post
    Do you really need the value for some reason?
    Well, maybe I don't need it. I was just pussled, that it was null.

    And then I still can't figure out why the stepExecution works, just because I implemented the StepExecutionListener interface in the Tasklet class. I would have thought that it would take some change to the job configuration xml to make it work.

  4. #4

    Default

    And then I still can't figure out why the stepExecution works, just because I implemented the StepExecutionListener interface in the Tasklet class
    TaskletStep checks whether the Tasklet implements StepExecutionListener, you only need to register nested listeners explicitly.

  5. #5

    Default

    Hi Robert,

    OK. That makes sense. By the way - thank you for all your good work with this framework.

    Best regards
    Preben

  6. #6
    Join Date
    Nov 2006
    Location
    Dallas, TX
    Posts
    15

    Default How do i save into StepExecutioncontext

    I need to save a list of keys into executioncontext so i can retrieve them during a next step.

    Is there a way to do that while running the current taskletstep?

  7. #7
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    Just implement StepExecutionListener so that you can get access to the ExecutionContext.

  8. #8
    Join Date
    Nov 2008
    Posts
    5

    Default

    Hi @all,

    I am not that far to execute the first step. I recently used Hivemind as IOC (Unfortunately - but it was company policy in those days) and want to move to Spring now, so there might be some lacks concerning Spring as well.

    I did everything as explained in the example, but somehow the PlatformTransactionManager is null, which results in the following stacktrace.
    Can anyone tell me if - any how - I have to define the TransactionManager myself in this example.v

    Code:
    2008-11-07 11:51:39,012  INFO org.springframework.context.support.ClassPathXmlApplicationContext:412 - Refreshing org.springframework.context.support.
    ClassPathXmlApplicationContext@cade31: display name [org.springframework.context.support.ClassPathXmlApplicationContext@cade31]; startup date [Fri Nov
     07 11:51:39 CET 2008]; root of context hierarchy
    2008-11-07 11:51:39,079  INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader:323 - Loading XML bean definitions from class path resourc
    e [simpleJob.xml]
    2008-11-07 11:51:39,290  INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader:323 - Loading XML bean definitions from class path resourc
    e [applicationContext.xml]
    2008-11-07 11:51:39,348  INFO org.springframework.context.support.ClassPathXmlApplicationContext:427 - Bean factory for application context [org.sprin
    gframework.context.support.ClassPathXmlApplicationContext@cade31]: org.springframework.beans.factory.support.DefaultListableBeanFactory@14ed577
    2008-11-07 11:51:39,368  INFO org.springframework.beans.factory.support.DefaultListableBeanFactory:414 - Pre-instantiating singletons in org.springfra
    mework.beans.factory.support.DefaultListableBeanFactory@14ed577: defining beans [jobLauncher,jobRepository,hello,space,world,taskletStep,simpleJob]; r
    oot of factory hierarchy
    2008-11-07 11:51:39,454  INFO org.springframework.batch.core.launch.support.SimpleJobLauncher:161 - No TaskExecutor has been set, defaulting to synchr
    onous executor.
    2008-11-07 11:51:39,537  INFO org.springframework.batch.core.launch.support.SimpleJobLauncher:114 - Job: [SimpleJob: [name=simpleJob]] launched with t
    he following parameters: [{}]
    2008-11-07 11:51:39,575 ERROR org.springframework.batch.core.step.AbstractStep:220 - Encountered an error executing the step: class java.lang.NullPoin
    terException: null
    java.lang.NullPointerException
            at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInStepContext(TaskletStep.java:247)
            at org.springframework.batch.core.scope.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:67)
            at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:352)
            at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:212)
            at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143)
            at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:230)
            at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
            at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:330)
            at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:78)
            at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:228)
            at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:115)
            at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
            at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:110)
            at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:204)
            at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:251)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:271)
            at java.lang.Thread.run(Thread.java:619)
    2008-11-07 11:51:39,599  INFO org.springframework.batch.core.launch.support.SimpleJobLauncher:116 - Job: [SimpleJob: [name=simpleJob]] completed with
    the following parameters: [{}] and the following status: [FAILED]
    2008-11-07 11:51:39,601  INFO org.springframework.context.support.ClassPathXmlApplicationContext:816 - Closing org.springframework.context.support.Cla
    ssPathXmlApplicationContext@cade31: display name [org.springframework.context.support.ClassPathXmlApplicationContext@cade31]; startup date [Fri Nov 07
     11:51:39 CET 2008]; root of context hierarchy
    2008-11-07 11:51:39,603  INFO org.springframework.beans.factory.support.DefaultListableBeanFactory:399 - Destroying singletons in org.springframework.
    beans.factory.support.DefaultListableBeanFactory@14ed577: defining beans [jobLauncher,jobRepository,hello,space,world,taskletStep,simpleJob]; root of
    factory hierarchy
    Thanks for your help.

    Thomas

  9. #9
    Join Date
    Jun 2005
    Posts
    4,231

    Default

    You need a transaction manager (just follow the examples). It looks like you probably aren't using the *StepFactoryBeans that the framework provides to make configuring steps easier. I would recommend you do that (as in the samples) so that you get an early warning about this kind of problem.

  10. #10
    Join Date
    Nov 2008
    Posts
    5

    Default

    Mmh.. as far as I can see no StepFactoryBeans are used in the example, but instead TaskletStep. At least not in Step1.
    As far as I can read from the documentation, the PlatformTransactionManager has only be defined in the StepHandler but not when using Tasklets.
    Is this true or is it only not mentioned especially in the documentation?

    Thomas

Posting Permissions

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