Spring启动批量Spring数据jpa

时间:2023-01-07 20:32:21

i am using Spring boot + Spring Batch + JPA + Mysql. below are the follow i am following:

我正在使用Spring启动+ Spring Batch + JPA + Mysql。以下是我关注的以下内容:

  1. spring boot application
  2. 春季启动应用

```

@SpringBootApplication(scanBasePackages = "com.xxx.xxxx.config, com.xxx.xxx.rest, com.xxx.xxx.domain, com.xxx.xxx.dataservice, com.xxx.xxx.batchconfiguration, com.xxx.xxx.steps")
    @EnableAutoConfiguration
    @EnableConfigurationProperties(Properties.class)
    @EnableEncryptableProperties
    public class SampleApplication
    {
        public static void main(String[] args) {
            SpringApplication.run(SampleApplication.class, args);
        }


    } 

```

  1. Created @Entity classes based on table structure
  2. 根据表结构创建了@Entity类

  3. created reposity interface like below
  4. 创建了如下所示的reposity界面

```

@Component
public interface ExampleRepository extends JpaRepository<tableClass, Long> {
   Page<tableClass> findTop10ByStatus(tableClassStatus status,
            Pageable pageable);
}

```

  1. Batch configuration:

```

@Configuration
    @EnableScheduling
    public class BatchScheduler {

        @Bean
        public ResourcelessTransactionManager transactionManager() {
            return new ResourcelessTransactionManager();
        }

        @Bean
        public JobExplorer jobExplorer() throws Exception {
            MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(
                    mapJobRepositoryFactory());
            jobExplorerFactory.afterPropertiesSet();
            return jobExplorerFactory.getObject();
        }

        @Bean
        public MapJobRepositoryFactoryBean mapJobRepositoryFactory() throws Exception {
            MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
            factory.setTransactionManager(transactionManager());
            return factory;
        }

        @Bean
        public JobRepository jobRepository() throws Exception {
            return mapJobRepositoryFactory().getObject();
        }

        @Bean
        public JobLauncher jobLauncher() throws Exception {
            SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
            simpleJobLauncher.setJobRepository(jobRepository());
            return simpleJobLauncher;
        }

    }

```

  1. batch configuration

```

@Configuration
    @EnableBatchProcessing
    @Import({ BatchScheduler.class })
    public class BatchConfiguration2 {

        @Autowired
        public StepBuilderFactory stepBuilderFactory;

        @Autowired
        public JobBuilderFactory jobBuilderFactory;

        @Autowired
        private SimpleJobLauncher jobLauncher;

        @Autowired
        public ExampleRepository exampleRepository ;

        @Scheduled(cron = "0/5 * * * * ?")
        public void perform() throws Exception {
            System.out.println("Job Started at :" + new Date());

            JobParameters param = new JobParametersBuilder()
                    .addString("JobID", String.valueOf(System.currentTimeMillis())).toJobParameters();

            JobExecution execution = jobLauncher.run(job(), param);

            System.out.println("Job finished with status :" + execution.getStatus());

        }

        @Bean
        public RepositoryMetadata repositoryMetadata() {
            return new DefaultRepositoryMetadata(ExampleRepository.class);
        }

        @Bean
        public RepositoryItemReader<tableClass> reader() {
            RepositoryItemReader<tableClass> fullfillment = new RepositoryItemReader<tableClass>();
            fullfillment.setRepository(ExampleRepository);
            fullfillment.setMethodName("findTop10ByStatus");
            List<tableClassStatus> list = new ArrayList<tableClassStatus>();
            list.add(tableClassStatus.FULFILLMENT_READY);
            fullfillment.setArguments(list);
            HashMap<String, Sort.Direction> sorts = new HashMap<>();
            sorts.put("id", Direction.DESC);
            fullfillment.setSort(sorts);
            return fullfillment;
        }

        /* @Bean
        public RepositoryItemWriter<tableClass> writer() {
            System.out.println("BatchConfiguration.writer()");
            RepositoryItemWriter<tableClass> itemWriter = new RepositoryItemWriter<tableClass>();
            itemWriter.setRepository(ExampleRepository);
            itemWriter.setMethodName("save");
            return itemWriter;
        }*/

        @Bean
        public RepositoryItemWriter<tableClass> writer() {
            System.out.println("BatchConfiguration.writer()");
            DefaultCrudMethods defaultCrudMethods = new DefaultCrudMethods(repositoryMetadata());
            RepositoryItemWriter<tableClass> itemWriter = new RepositoryItemWriter<tableClass>();
            itemWriter.setRepository(ExampleRepository);
            itemWriter.setMethodName(defaultCrudMethods.getSaveMethod().getName());
            return itemWriter;
        }

        @Bean
        public Step step1() throws Exception {
            return this.stepBuilderFactory.get("step1")
                    .<tableClass, tableClass> chunk(1).reader(reader())
                    .processor(new QuoteOfferFullfillmentSubmitionProcessor()).writer(writer()).build();
        }


        @Bean
        public Job job() throws Exception {

            return this.jobBuilderFactory.get("job").incrementer(new RunIdIncrementer()).start(step1())
                    .listener(new JobCompletionListener()).build();

        }

    }

```

  1. QuoteOfferFullfillmentSubmitionProcessor.java

```

public class QuoteOfferFullfillmentSubmitionProcessor implements ItemProcessor<QuoteOfferFulfillment, QuoteOfferFulfillment> {

        @Override
        public tableClass process(tableClass item) throws Exception {

            System.out.println("Processor.process() ==> ID " + item.getId());
            System.out.println("Processor.process() ==> " + item.getLenderName());
            System.out.println(
                    "QuoteOfferFullfillmentSubmitionProcessor.process() ==> source" + item.getStatus());
            item.setStatus(QuoteOfferFulfillmentStatus.PROCESSING);
            return item;
        }

    }

```

  1. JobCompletionListener.java

```

public class JobCompletionListener extends JobExecutionListenerSupport {
            @Override
            public void afterJob(JobExecution jobExecution) {
                if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
                    System.out.println("BATCH JOB COMPLETED SUCCESSFULLY");
                }
            }

        }

```

after all configuration, running the application every 5 sec the job is running fine, getting values and printing in process class, after that coming to writer class and printing all the values with out Errors.

在所有配置之后,每5秒运行一次应用程序,作业运行正常,获取值并在进程类中打印,然后进入编写器类并打印出没有错误的所有值。

the problem is: its not updating the values in the Database in write method.

问题是:它没有在write方法中更新Database中的值。

with out BatchScheduler, scheduler and job launcher (commit the code) restarting the spring boot its all updating the values in database.

没有BatchScheduler,调度程序和作业启动器(提交代码)重新启动spring引导,它所有更新数据库中的值。

Can you guys suggest me what wrong i am doing.

你们能告诉我我做错了什么吗?

1 个解决方案

#1


1  

I have resolve the issue. in the batch configuration if i use

我已经解决了这个问题。如果我使用批处理配置

```

@Bean
    public ResourcelessTransactionManager resourcelessTransactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    public JobRepository jobRepository() throws Exception {
        return new MapJobRepositoryFactoryBean(resourcelessTransactionManager()).getObject();
    }

```

it started working.

它开始工作了。

Thanks,
Bala.

#1


1  

I have resolve the issue. in the batch configuration if i use

我已经解决了这个问题。如果我使用批处理配置

```

@Bean
    public ResourcelessTransactionManager resourcelessTransactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    public JobRepository jobRepository() throws Exception {
        return new MapJobRepositoryFactoryBean(resourcelessTransactionManager()).getObject();
    }

```

it started working.

它开始工作了。

Thanks,
Bala.