Spring批处理 jobParameters的值为null

huangapple go评论82阅读模式
英文:

Spring batch jobParameters value null

问题

在一个Spring批处理中,我试图传递jobParameters并在我的JdbcCursorItemReader中使用它们。下面是相关的一部分代码:

@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{

    private String demoVar;

    public String getDemoVar() {
        return demoVar;
    }

    public void setDemoVar(@Value("#{jobParameters['demoVarValue']}") String demoVar) {
        this.demoVar = demoVar;
    }

    @Bean
    @StepScope
    public JdbcCursorItemReader<User> reader1() {
        String sqlQuery = String.format("select * FROM TABLE_NAME where demoVar=? ");
        System.out.println(demoVar);
        JdbcCursorItemReader<User> cursorItemReader = new JdbcCursorItemReader<>();
        cursorItemReader.setDataSource(dataSource);
        cursorItemReader.setSql(sqlQuery);
        cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setString(1, demoVar);
            }
        });
        cursorItemReader.setRowMapper(new DbRowMapper());
        return cursorItemReader;
    }
...

控制器类(包含作业启动器):

@RestController
@RequestMapping("/load")
public class LoadController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @GetMapping("/users")
    public BatchStatus load() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        
        JobParameters jobParameters = new JobParametersBuilder()
                                    .addString("demoVarValue", "001")
                                    .toJobParameters();
        
        JobExecution jobExecution = jobLauncher.run(job, jobParameters);

        System.out.println("JobExecution: " + jobExecution.getStatus());

        return jobExecution.getStatus();
    }
}

但是在readerbean中,从jobParameters解析出的demoVar值传递为null,而不是预期的001

英文:

In a spring batch, I'm trying to pass jobParameters and use them in my JdbcCursorItemReader. A snippet from the same is:

@Configuration
public class SpringBatchConfig extends DefaultBatchConfigurer{

    private String demoVar;

	public String getDemoVar() {
		return demoVar;
	}

	public void setDemoVar(@Value(&quot;#{jobParameters[&#39;demoVarValue&#39;]}&quot;) String demoVar) {
		this.demoVar = demoVar;
	}

    @Bean
	@StepScope
	public JdbcCursorItemReader&lt;User&gt; reader1() {
		String sqlQuery = String.format(&quot;select * FROM TABLE_NAME where demoVar=? &quot;);
		System.out.println(demoVar);
		JdbcCursorItemReader&lt;User&gt; cursorItemReader = new JdbcCursorItemReader&lt;&gt;();
		cursorItemReader.setDataSource(dataSource);
		cursorItemReader.setSql(sqlQuery);
		cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
		   public void setValues(PreparedStatement preparedStatement) throws SQLException {
		      preparedStatement.setString(1, demoVar);
		   }
		});
		cursorItemReader.setRowMapper(new DbRowMapper());
		return cursorItemReader;
	}
...

Controller class(containing job launcher):

@RestController
@RequestMapping(&quot;/load&quot;)
public class LoadController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @GetMapping(&quot;/users&quot;)
    public BatchStatus load() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        
        JobParameters jobParameters = new JobParametersBuilder()
        							.addString(&quot;demoVarValue&quot;, &quot;001&quot;)
        							.toJobParameters();
        
        JobExecution jobExecution = jobLauncher.run(job, jobParameters);

        System.out.println(&quot;JobExecution: &quot; + jobExecution.getStatus());

        return jobExecution.getStatus();
    }
}

But in the reader bean, the value of the demoVar parsed from jobParameters is being passed as null instead of the expected 001.

答案1

得分: 3

除非在其他地方调用了getter方法,否则您不需要`demoVar`字段。您可以将其作为reader的bean定义方法的参数传递:

@Bean
@StepScope
public JdbcCursorItemReader<User> reader1(@Value("#{jobParameters['demoVarValue']}") String demoVar) {
    String sqlQuery = String.format("select * FROM TABLE_NAME where demoVar=? ");
    System.out.println(demoVar);
    JdbcCursorItemReader<User> cursorItemReader = new JdbcCursorItemReader<>();
    cursorItemReader.setDataSource(dataSource);
    cursorItemReader.setSql(sqlQuery);
    cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
       public void setValues(PreparedStatement preparedStatement) throws SQLException {
          preparedStatement.setString(1, demoVar);
       }
    });
    cursorItemReader.setRowMapper(new DbRowMapper());
    return cursorItemReader;
}

作业参数的延迟绑定在参考文档的[作业和步骤属性的延迟绑定](https://docs.spring.io/spring-batch/docs/4.2.x/reference/html/step.html#late-binding)部分进行了解释。
英文:

You don't need the demoVar field (unless you are calling the getter somewhere else). You can pass it as a parameter of reader's bean definition method:

@Bean
@StepScope
public JdbcCursorItemReader&lt;User&gt; reader1(@Value(&quot;#{jobParameters[&#39;demoVarValue&#39;]}&quot;) String demoVar) {
    String sqlQuery = String.format(&quot;select * FROM TABLE_NAME where demoVar=? &quot;);
    System.out.println(demoVar);
    JdbcCursorItemReader&lt;User&gt; cursorItemReader = new JdbcCursorItemReader&lt;&gt;();
    cursorItemReader.setDataSource(dataSource);
    cursorItemReader.setSql(sqlQuery);
    cursorItemReader.setPreparedStatementSetter(new PreparedStatementSetter() {
       public void setValues(PreparedStatement preparedStatement) throws SQLException {
          preparedStatement.setString(1, demoVar);
       }
    });
    cursorItemReader.setRowMapper(new DbRowMapper());
    return cursorItemReader;
}

Late binding of job parameters is explained in the Late Binding of Job and Step Attributes section of the reference documentation.

huangapple
  • 本文由 发表于 2020年10月5日 19:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64207827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定