Guest User

Untitled

a guest
Dec 8th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. java.lang.IllegalStateException: Failed to execute CommandLineRunner
  2. at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  3. at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  4. at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  5. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  6. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  7. at com.example.XmlFileOutputApplication.main(XmlFileOutputApplication.java:26) [classes/:na]
  8. Caused by: org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={}. If you want to run this job again, change the parameters.
  9. at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:132) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
  10. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
  11. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
  12. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
  13. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
  14. at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  15. at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  16. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  17. at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  18. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  19. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  20. at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:181) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
  21. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  22. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  23. at com.sun.proxy.$Proxy39.createJobExecution(Unknown Source) ~[na:na]
  24. at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:134) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
  25. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
  26. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
  27. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
  28. at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
  29. at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  30. at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  31. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  32. at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
  33. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  34. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
  35. at com.sun.proxy.$Proxy37.run(Unknown Source) ~[na:na]
  36. at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  37. at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:186) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  38. at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:172) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  39. at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:166) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  40. at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
  41. ... 5 common frames omitted
  42.  
  43. 2018-12-08 22:27:41.017 INFO 21172 --- [ main] o.s.b.f.support.DisposableBeanAdapter : Destroy method 'close' on bean with name 'customerItemWriter' threw an exception: java.lang.NullPointerException
  44. 2018-12-08 22:27:41.017 INFO 21172 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
  45. 2018-12-08 22:27:41.017 INFO 21172 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
  46.  
  47. @Data
  48. @AllArgsConstructor
  49. @Builder
  50. @NoArgsConstructor
  51. public class Customer {
  52. private Long id;
  53. private String firstName;
  54. private String lastName;
  55. private String birthdate;
  56. }
  57.  
  58. public class CustomerRowMapper implements RowMapper<Customer> {
  59. @Override
  60. public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
  61. return Customer.builder().id(rs.getLong("id"))
  62. .firstName(rs.getString("firstName"))
  63. .lastName(rs.getString("lastName"))
  64. .birthdate(rs.getString("birthdate")).build();
  65. }
  66. }
  67.  
  68. @Configuration
  69. public class JobConfiguration {
  70. @Autowired
  71. private JobBuilderFactory jobBuilderFactory;
  72.  
  73. @Autowired
  74. private StepBuilderFactory stepBuilderFactory;
  75.  
  76. @Autowired
  77. private DataSource dataSource;
  78.  
  79. @Bean
  80. public JdbcPagingItemReader<Customer> customerPagingItemReader(){
  81. // reading database records using JDBC in a paging fashion
  82. JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
  83. reader.setDataSource(this.dataSource);
  84. reader.setFetchSize(1000);
  85. reader.setRowMapper(new CustomerRowMapper());
  86.  
  87. // Sort Keys
  88. Map<String, Order> sortKeys = new HashMap<>();
  89. sortKeys.put("id", Order.ASCENDING);
  90.  
  91. // MySQL implementation of a PagingQueryProvider using database specific features.
  92. MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
  93. queryProvider.setSelectClause("id, firstName, lastName, birthdate");
  94. queryProvider.setFromClause("from customer");
  95. queryProvider.setSortKeys(sortKeys);
  96.  
  97. reader.setQueryProvider(queryProvider);
  98.  
  99. return reader;
  100. }
  101.  
  102.  
  103. @Bean
  104. public StaxEventItemWriter<Customer> customerItemWriter() throws Exception{
  105. String customerOutputPath = File.createTempFile("customerOutput", ".out").getAbsolutePath();
  106. System.out.println(">> Output Path = "+customerOutputPath);
  107.  
  108. Map<String, Class> aliases = new HashMap<>();
  109. aliases.put("customer", Customer.class);
  110.  
  111. XStreamMarshaller marshaller = new XStreamMarshaller();
  112. marshaller.setAliases(aliases);
  113.  
  114. // StAX and Marshaller for serializing object to XML.
  115. StaxEventItemWriter<Customer> writer = new StaxEventItemWriter<>();
  116. writer.setRootTagName("customers");
  117. writer.setMarshaller(marshaller);
  118. writer.setResource(new FileSystemResource(customerOutputPath));
  119. writer.afterPropertiesSet();
  120.  
  121. return writer;
  122. }
  123.  
  124.  
  125. @Bean
  126. public Step step1() throws Exception {
  127. return stepBuilderFactory.get("step1")
  128. .<Customer, Customer>chunk(100)
  129. .reader(customerPagingItemReader())
  130. .writer(customerItemWriter())
  131. .build();
  132. }
  133.  
  134. @Bean
  135. public Job job() throws Exception {
  136. return jobBuilderFactory.get("job")
  137. .start(step1())
  138. .build();
  139. }
  140. }
  141.  
  142. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  143. spring.datasource.url=jdbc:mysql://localhost:3306/test
  144. spring.datasource.username=root
  145. spring.datasource.password=root
  146. spring.batch.initialize-schema=always
  147.  
  148. @SpringBootApplication
  149. @EnableBatchProcessing
  150. public class XmlFileOutputApplication implements CommandLineRunner{
  151. @Autowired
  152. private JobLauncher jobLauncher;
  153.  
  154. @Autowired
  155. private Job job;
  156.  
  157. public static void main(String[] args) {
  158. SpringApplication.run(XmlFileOutputApplication.class, args);
  159. }
  160.  
  161. @Override
  162. public void run(String... args) throws Exception {
  163. JobParameters jobParameters = new JobParametersBuilder()
  164. .addString("JobId", String.valueOf(System.currentTimeMillis()))
  165. .addDate("date", new Date())
  166. .addLong("time",System.currentTimeMillis()).toJobParameters();
  167.  
  168. JobExecution execution = jobLauncher.run(job, jobParameters);
  169. System.out.println("STATUS :: "+execution.getStatus());
  170. }
  171. }
Add Comment
Please, Sign In to add comment