简体   繁体   中英

spring boot and spring batch throw NullPointerException in DAO of ItemProcessor batch

I am having problems in my spring boot and spring batch application, I share my situation have my layer Dao which is a interface is throwning NPE ( NullPointerException ) and it implemented, it had annotation @Autowired and I did test with Junit for check it and works but I don´t know that´s wrong in my configuration job or CustomItemProccesor, here is my code, I hope you can help me, please, thanks.

Config Job:

@ComponentScan({"com.company.batch.config","com.company.batch.dao","com.company.batch.mapper","com.company.batch.model","com.company.batch.particion","com.company.batch.procesos","com.company.batch.reader","com.company.batch.writers"})
public class ConfigJob 
{

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("sqlserverDataSource")
    private DataSource dataSource;


    @Bean(name = "demoPartitionStep")
    public Step step1Manager(Step slaveStep) {
        return stepBuilderFactory.get("step1.manager")
            .<String, String>partitioner("step1", demoPartitioner())
            .step(slaveStep)
            .gridSize(numerohilos())
            .taskExecutor(taskExecutor())
            .build();
    }

    @Bean(name = "demoPartitioner", destroyMethod = "")
    public Partitioner demoPartitioner() {
        RangePartitioner partitioner = new RangePartitioner();
        //partitioner.setDataSource(dataSource);
        // partitioner.partition(20);
        return partitioner;
    }

    // slave step
    @Bean
    public Step slaveStep(ItemReader<beangenerico> demoReader)
    {
        return stepBuilderFactory.get("slaveStep")
                .chunk(1)
                //.reader(pagingItemReader(null, null))
                .reader(demoReader)
                .processor(compositeProcessor())
                .writer(new ListDelegateWriter())
                .build();
    }


    @Bean
    public CompositeItemProcessor compositeProcessor() {
        List<ItemProcessor> delegates = new ArrayList<>(3);
        delegates.add(new CustomerItemProcessor());
        delegates.add(new AccountsItemProcessor());
        delegates.add(new beanDataItemProccesor());

        CompositeItemProcessor processor = new CompositeItemProcessor();

        processor.setDelegates(delegates);

        return processor;
    }


    @Bean(name = "demoWriter")
    @StepScope
    public ItemWriter< beangenerico> CustomItemWriter() {
        // TODO Auto-generated method stub
        CustomItemWriter wri = new CustomItemWriter();
        return wri;
    }


    @Bean(name = "demoReader")
    @StepScope
    public ItemReader<beangenerico> formiikreader(@Value("#{stepExecutionContext['fromId']}") int minValue,@Value("#{stepExecutionContext['toId']}") int maxValue){
        myReader fr = new myReader(minValue,maxValue);
        return fr;
    }

    @Bean
    public TaskExecutor taskExecutor() {
        return new SimpleAsyncTaskExecutor("spring_batch");
    }

    @Bean
    public Job job(@Qualifier("demoPartitionStep") Step demoPartitionStep) {
        return this.jobBuilderFactory.get("job")
                .start(demoPartitionStep)
                .build();
    }

}

This is the CustomerItemProcessor of spring batch

@Component
public class CustomerItemProcessor implements ItemProcessor<beangenerico,ThreadLocal<CopyOnWriteArrayList<beanCustomer>>> {
 {

    @Autowired
    private CustomerDAO customerDAO;

    private ThreadLocal<CopyOnWriteArrayList<beanCustomer>> listbean = new ThreadLocal<CopyOnWriteArrayList<beanCustomer>>();

    public ThreadLocal<CopyOnWriteArrayList<beanCustomer>> process(beangenerico rangos) throws Exception {
        System.out.println("entro a customitemprocessor");
            // TODO Auto-generated method stub

            listbean.set(new CopyOnWriteArrayList<beanCustomer>());

            System.out.println("rangos:"+rangos.getIni()+"-"+rangos.getFin()); //si trae datos
            listbean = customerDAO.getAccAgentes(rangos);

            if(listbean != null) {
                return listbean;
            } else {
                return null;
            }

    }

    @Autowired
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

}

Here is my interface Dao

public interface CustomerDAO {
    ThreadLocal<CopyOnWriteArrayList<beanCustomer>> getAccAgentes(beangenerico bean);
}

This is the DAO implementation:

@Repository("customerDAO")
public class CustomerDAOImpl  implements CustomerDAO{ 

    private String SP_SQL = "{call mysp(?, ?)}";

    @Autowired
    @Qualifier("sqlserverDataSource")
    DataSource dataSource;

    @Autowired
    JdbcTemplate jdbcTemplate;

    private ThreadLocal<CopyOnWriteArrayList<beanCustomer>> customerList2=new ThreadLocal<CopyOnWriteArrayList<beanCustomer>>();
    private beanCustomer b = null; 


    public  ThreadLocal<CopyOnWriteArrayList<beanCustomer>> getAccAgentes(beangenerico bean) {  
            // TODO Auto-generated method stub
            try {

                customerList2.set(new CopyOnWriteArrayList<beanCustomer>());
                System.out.println("entro a metodo");
                if(getJdbcTemplate().getDataSource()!=null) {
                    System.out.println("success con"); //with junit test is ok
                }else {
                    System.out.println("null conn");
                }
                return getJdbcTemplate().query(
                        SP_SQL,
                        new Object [] {bean.getIni(),bean.getFin()}, new ResultSetExtractor<ThreadLocal<CopyOnWriteArrayList<beanCustomer>>>() {    

                            @Override
                            public ThreadLocal<CopyOnWriteArrayList<beanCustomer>> extractData(ResultSet rs)
                                    throws SQLException, DataAccessException {
                                // TODO Auto-generated method stub
                                while(rs.next()){
                                b = new beanCustomer();
                                b.setIduser(rs.getString("iduser_co"));
                                b.setAccount(rs.getString("account_co"));
                                b.setTypeUser(rs.getString("type_idag"));
                                customerList2.get().add(b);
                                }
                                return customerList2;
                            }
                        });
            }catch(Exception e) {}

            System.out.println("size lista: "+customerList2.get().size());
            return customerList2;

    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public JdbcTemplate getJdbcTemplate() {
        if (null == jdbcTemplate) {
            jdbcTemplate = new JdbcTemplate(dataSource);
        }
        return jdbcTemplate;
    }   

}

Stack trace:

2020-04-28 11:15:03 - Repeat is complete according to policy and result value.
entro a customitemprocessor
rangos:71-80
2020-04-28 11:15:03 - Applying contribution: [StepContribution: read=1, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
2020-04-28 11:15:03 - Rollback for RuntimeException: java.lang.NullPointerException: null
2020-04-28 11:15:03 - Initiating transaction rollback on application exception
java.lang.NullPointerException: null
    at com.company.batch.procesos.CustomerItemProcessor.process(CustomerItemProcessor.java:69)
    at com.company.batch.procesos.CustomerItemProcessor.process(CustomerItemProcessor.java:1)
    at org.springframework.batch.item.support.CompositeItemProcessor.processItem(CompositeItemProcessor.java:63)
    at org.springframework.batch.item.support.CompositeItemProcessor.process(CompositeItemProcessor.java:52)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:134)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:319)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:210)
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:77)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208)
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:138)
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:135)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.lang.Thread.run(Thread.java:748)
2020-04-28 11:15:03 - Initiating transaction rollback
2020-04-28 11:15:03 - Rolling back resourceless transaction on [org.springframework.batch.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@1337e94]
2020-04-28 11:15:03 - Handling exception: java.lang.NullPointerException, caused by: java.lang.NullPointerException: null
2020-04-28 11:15:03 - Handling fatal exception explicitly (rethrowing first of 1): java.lang.NullPointerException: null
2020-04-28 11:15:03 - Encountered an error executing step slaveStep in job job
java.lang.NullPointerException: null
    at com.company.batch.procesos.CustomerItemProcessor.process(CustomerItemProcessor.java:69)
    at com.company.batch.procesos.CustomerItemProcessor.process(CustomerItemProcessor.java:1)
    at org.springframework.batch.item.support.CompositeItemProcessor.processItem(CompositeItemProcessor.java:63)

here

listbean = customerDAO.getAccAgentes(rangos);

Thanks in advance

When defining CompositeItemProcessor , you are creating CustomerItemProcessor by yourself which cause its dependent beans cannot be injected and resulting NPE. You should get the CustomerItemProcessor from the Spring context rather than creating it by yourself. Something like:

    @Bean
    public CompositeItemProcessor compositeProcessor(CustomerItemProcessor customerItemProcessor) {
        List<ItemProcessor> delegates = new ArrayList<>(3);
        delegates.add(customerItemProcessor);

        CompositeItemProcessor processor = new CompositeItemProcessor();

        processor.setDelegates(delegates);

        return processor;
    }

The same applies to other delegated beans of the CompositeItemProcessor such as AccountsItemProcessor etc.

Alright, I found the problem is that was not instancing the bean in the Config, here put my solution in the code ConfigJob, hoping that help someone else.

 @ComponentScan({"com.company.batch.config","com.company.batch.dao","com.company.batch.mapper","com.company.batch.model","com.company.batch.particion","com.company.batch.procesos","com.company.batch.reader","com.company.batch.writers"}) public class ConfigJob { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired @Qualifier("sqlserverDataSource") private DataSource dataSource; @Bean(name = "demoPartitionStep") public Step step1Manager(Step slaveStep) { return stepBuilderFactory.get("step1.manager").<String, String>partitioner("step1", demoPartitioner()).step(slaveStep).gridSize(numerohilos()).taskExecutor(taskExecutor()).build(); } @Bean(name = "demoPartitioner", destroyMethod = "") public Partitioner demoPartitioner() { RangePartitioner partitioner = new RangePartitioner(); //partitioner.setDataSource(dataSource); // partitioner.partition(20); return partitioner; } // slave step @Bean public Step slaveStep(ItemReader<beangenerico> demoReader) { return stepBuilderFactory.get("slaveStep").chunk(1) //.reader(pagingItemReader(null, null)).reader(demoReader).processor(compositeProcessor()).writer(new ListDelegateWriter()).build(); } @Bean public CompositeItemProcessor compositeProcessor() { List<ItemProcessor> delegates = new ArrayList<>(3); delegates.add(new CustomerItemProcessor()); delegates.add(new AccountsItemProcessor()); delegates.add(new beanDataItemProccesor()); CompositeItemProcessor processor = new CompositeItemProcessor(); processor.setDelegates(delegates); return processor; } @Bean public CustomerItemProcessor CustomerProccesor(){ return new CustomerItemProcessor(); } @Bean public AccountsItemProcessor AccountsItemProcessor(){ return new AccountsItemProcessor(); } @Bean public beanDataItemProccesor beanDataItemProccesor(){ return new beanDataItemProccesor(); } @Bean(name = "demoWriter") @StepScope public ItemWriter< beangenerico> CustomItemWriter() { // TODO Auto-generated method stub CustomItemWriter wri = new CustomItemWriter(); return wri; } @Bean(name = "demoReader") @StepScope public ItemReader<beangenerico> formiikreader(@Value("#{stepExecutionContext['fromId']}") int minValue,@Value("#{stepExecutionContext['toId']}") int maxValue){ myReader fr = new myReader(minValue,maxValue); return fr; } @Bean public TaskExecutor taskExecutor() { return new SimpleAsyncTaskExecutor("spring_batch"); } @Bean public Job job(@Qualifier("demoPartitionStep") Step demoPartitionStep) { return this.jobBuilderFactory.get("job").start(demoPartitionStep).build(); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM