2017-06-06 145 views
0

嘿,我是新来的春天,我试图做一个程序,处理一堆来自数据库的记录,并为他们做些什么。基本上获取处于特定状态的所有记录,在操作系统中运行命令(每条记录一条),更改这些记录中的内容。使用弹簧从JPA批处理数据

我觉得理想的工具是使用spring的批处理服务支持。我已经有我想要读取的记录映射到一个JPA bean(Schedule),并且我有CrudRepository(ScheduleRepository),所以我认为将所有东西连接在一起很容易,尽管我不知道如何。

我试图跟着batch service guide做一些改动来阅读来自JPA的输入。但是当我试图运行时,当spring试图实例化JpaPagingItemReader时,我得到了一个N​​ullPointerException异常。

我会尽量附上我找到最相关的问题代码:

@Configuration 
@EnableBatchProcessing 
public class BatchConfiguration { 
    //@Autowired 
    //public ScheduleRepository repository; 
    @Autowired 
    public JobBuilderFactory jobBuilderFactory; 
    @Autowired 
    public StepBuilderFactory stepBuilderFactory; 
    @Bean 
    public Job synchronizeProcess(JobCompletionNotificationListener listener) { 
     return jobBuilderFactory.get("synchronizeProcess") 
       .incrementer(new RunIdIncrementer()) 
       .listener(listener) 
       .flow(step1()) 
       .end() 
       .build(); 
    } 

    private Step step1() { 
     return stepBuilderFactory.get("step1") 
       .<Schedule, Schedule> chunk(10) 
       .reader(new JpaPagingItemReader<Schedule>()) 
       .processor(new ScheduleExectutor()) 
       .writer(new JpaItemWriter<Schedule>()) 
       .build(); 
    } 
} 

的application.properties:

spring.jpa.hibernate.ddl-auto=none 
spring.datasource.url=jdbc:postgresql://localhost:5432/my_db 
spring.datasource.username=myuser 
spring.datasource.password=mypassword 

下面是完整的日志:

$ mvn spring-boot:run 
[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building runner 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-compile @ runner >>> 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ runner --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 1 resource 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ runner --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ runner --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory /home/msl09/unsynced/workspace/syncweb_scheduler/runner/src/test/resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ runner --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile @ runner <<< 
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ runner --- 

. ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.3.RELEASE) 

2017-06-06 12:40:55.492 INFO 21085 --- [   main] b.j.t.syncweb.runner.RunnerApplication : Starting RunnerApplication on myhost-debian with PID 21085 (/home/msl09/unsynced/workspace/syncweb_scheduler/runner/target/classes started by msl09 in /home/msl09/unsynced/workspace/syncweb_scheduler/runner) 
2017-06-06 12:40:55.494 INFO 21085 --- [   main] b.j.t.syncweb.runner.RunnerApplication : No active profile set, falling back to default profiles: default 
2017-06-06 12:40:55.533 INFO 21085 --- [   main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]4d488830: startup date [Tue Jun 06 12:40:55 BRT 2017]; root of context hierarchy 
2017-06-06 12:40:55.957 WARN 21085 --- [   main] o.s.c.a.ConfigurationClassEnhancer  : @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details. 
2017-06-06 12:40:55.966 WARN 21085 --- [   main] o.s.c.a.ConfigurationClassEnhancer  : @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details. 
2017-06-06 12:40:56.326 INFO 21085 --- [   main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' 
2017-06-06 12:40:56.340 INFO 21085 --- [   main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ 
     name: default 
     ...] 
2017-06-06 12:40:56.385 INFO 21085 --- [   main] org.hibernate.Version     : HHH000412: Hibernate Core {5.0.12.Final} 
2017-06-06 12:40:56.386 INFO 21085 --- [   main] org.hibernate.cfg.Environment   : HHH000206: hibernate.properties not found 
2017-06-06 12:40:56.387 INFO 21085 --- [   main] org.hibernate.cfg.Environment   : HHH000021: Bytecode provider name : javassist 
2017-06-06 12:40:56.431 INFO 21085 --- [   main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
2017-06-06 12:40:56.501 INFO 21085 --- [   main] org.hibernate.dialect.Dialect   : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect 
2017-06-06 12:40:56.621 INFO 21085 --- [   main] o.h.e.j.e.i.LobCreatorBuilderImpl  : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 
2017-06-06 12:40:56.622 INFO 21085 --- [   main] org.hibernate.type.BasicTypeRegistry  : HHH000270: Type registration [java.util.UUID] overrides previous : [email protected] 
2017-06-06 12:40:56.809 INFO 21085 --- [   main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 
2017-06-06 12:40:57.031 WARN 21085 --- [   main] o.s.b.a.batch.BasicBatchConfigurer  : JPA does not support custom isolation levels, so locks may not be taken when launching Jobs 
2017-06-06 12:40:57.032 INFO 21085 --- [   main] o.s.b.c.r.s.JobRepositoryFactoryBean  : No database type set, using meta data indicating: POSTGRES 
2017-06-06 12:40:57.151 INFO 21085 --- [   main] o.s.b.c.l.support.SimpleJobLauncher  : No TaskExecutor has been set, defaulting to synchronous executor. 
2017-06-06 12:40:57.164 INFO 21085 --- [   main] o.s.jdbc.datasource.init.ScriptUtils  : Executing SQL script from class path resource [org/springframework/batch/core/schema-postgresql.sql] 
2017-06-06 12:40:57.964 INFO 21085 --- [   main] o.s.jdbc.datasource.init.ScriptUtils  : Executed SQL script from class path resource [org/springframework/batch/core/schema-postgresql.sql] in 800 ms. 
2017-06-06 12:40:58.098 INFO 21085 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
2017-06-06 12:40:58.109 INFO 21085 --- [   main] o.s.b.a.b.JobLauncherCommandLineRunner : Running default command line with: [] 
2017-06-06 12:40:58.214 INFO 21085 --- [   main] o.s.b.c.l.support.SimpleJobLauncher  : Job: [FlowJob: [name=synchronizeProcess]] launched with the following parameters: [{run.id=1}] 
2017-06-06 12:40:58.238 INFO 21085 --- [   main] o.s.batch.core.job.SimpleStepHandler  : Executing step: [step1] 
2017-06-06 12:40:58.251 ERROR 21085 --- [   main] o.s.batch.core.step.AbstractStep   : Encountered an error executing step step1 in job synchronizeProcess 

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader 
     at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] 
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] 
     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at com.sun.proxy.$Proxy74.run(Unknown Source) [na:na] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:231) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:123) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:117) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at br.jus.tjse.syncweb.runner.RunnerApplication.main(RunnerApplication.java:10) [classes/:na] 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] 
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] 
     at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) [spring-boot-maven-plugin-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] 
Caused by: java.lang.NullPointerException: null 
     at org.springframework.batch.item.database.JpaPagingItemReader.doOpen(JpaPagingItemReader.java:177) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     ... 41 common frames omitted 

2017-06-06 12:40:58.264 ERROR 21085 --- [   main] o.s.batch.core.step.AbstractStep   : Exception while closing step execution resources in step step1 in job synchronizeProcess 

org.springframework.batch.item.ItemStreamException: Error while closing item reader 
     at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:136) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.item.support.CompositeItemStream.close(CompositeItemStream.java:85) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.step.tasklet.TaskletStep.close(TaskletStep.java:305) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:271) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] 
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] 
     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) [spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE] 
     at com.sun.proxy.$Proxy74.run(Unknown Source) [na:na] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:231) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:123) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:117) [spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at br.jus.tjse.syncweb.runner.RunnerApplication.main(RunnerApplication.java:10) [classes/:na] 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] 
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] 
     at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) [spring-boot-maven-plugin-1.5.3.RELEASE.jar:1.5.3.RELEASE] 
     at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] 
Caused by: java.lang.NullPointerException: null 
     at org.springframework.batch.item.database.JpaPagingItemReader.doClose(JpaPagingItemReader.java:236) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.close(AbstractItemCountingItemStreamItemReader.java:133) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 
     ... 41 common frames omitted 

2017-06-06 12:40:58.305 INFO 21085 --- [   main] o.s.b.c.l.support.SimpleJobLauncher  : Job: [FlowJob: [name=synchronizeProcess]] completed with the following parameters: [{run.id=1}] and the following status: [FAILED] 
2017-06-06 12:40:58.307 INFO 21085 --- [   main] b.j.t.syncweb.runner.RunnerApplication : Started RunnerApplication in 3.142 seconds (JVM running for 5.223) 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 4.444 s 
[INFO] Finished at: 2017-06-06T12:40:58-03:00 
[INFO] Final Memory: 34M/351M 
[INFO] ------------------------------------------------------------------------ 
2017-06-06 12:40:58.478 INFO 21085 --- [  Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]4d488830: startup date [Tue Jun 06 12:40:55 BRT 2017]; root of context hierarchy 
2017-06-06 12:40:58.479 INFO 21085 --- [  Thread-3] o.s.j.e.a.AnnotationMBeanExporter  : Unregistering JMX-exposed beans on shutdown 
2017-06-06 12:40:58.480 INFO 21085 --- [  Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 

什么是实例化JpaPaginItemReader的正确方法?

+0

这里的信息不够详细 - 堆栈跟踪也许,你在教程中的位置? –

+1

您需要将其定义为'@ Bean'并注入'EntityManagerFactory',您显然需要告诉它执行哪个查询来获取您的项目。这同样适用于作者。 –

回答

0

@M。 Deinum感谢了一大堆,使用你的提示我能够弄清楚如何修复这个bug。下面是我的配置类如何现在:

@Configuration 
@EnableBatchProcessing 
public class BatchConfiguration { 
    @Autowired 
    public JobBuilderFactory jobBuilderFactory; 
    @Autowired 
    public StepBuilderFactory stepBuilderFactory; 
    @Autowired 
    public EntityManagerFactory entityManagerFactory; 

    @Bean 
    public Job synchronizeProcess(JobCompletionNotificationListener listener) { 
     return jobBuilderFactory.get("synchronizeProcess") 
       .incrementer(new RunIdIncrementer()) 
       .listener(listener) 
       .flow(step1()) 
       .end() 
       .build(); 
    } 

    @Bean 
    public Step step1() { 
     return stepBuilderFactory.get("step1") 
       .<Schedule, Schedule> chunk(10) 
       .reader(reader()) 
       .processor(new ScheduleExectutor()) 
       .writer(writer()) 
       .build(); 
    } 

    @Bean 
    public JpaItemWriter<Schedule> writer() { 
     JpaItemWriter<Schedule> jpaItemWriter = new JpaItemWriter<>(); 
     jpaItemWriter.setEntityManagerFactory(entityManagerFactory); 
     return jpaItemWriter; 
    } 

    @Bean 
    public ItemReader<Schedule> reader() { 
     JpaPagingItemReader<Schedule> jpaPagingItemReader = new JpaPagingItemReader<Schedule>(); 
     jpaPagingItemReader.setEntityManagerFactory(entityManagerFactory); 
     jpaPagingItemReader.setQueryString("SELECT s FROM Schedule s WHERE s.status = 'WAITING'"); 
     jpaPagingItemReader.setPageSize(10); 
     return jpaPagingItemReader; 
    } 

} 

现在休眠小号抱怨附表没有映射,但至少这是一个不同的错误,我认为这更多涉及到我不知道JPQL是如何工作的。

+0

我刚才提到的最后一个问题是因为s​​pring没有扫描我的数据包(bean所在的位置)。使用的解决方案在这里找到:https://stackoverflow.com/questions/33985306/spring-boot-hibernate-user-is-not-mapped –