Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- java.lang.IllegalStateException: Failed to execute CommandLineRunner
- at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at com.example.XmlFileOutputApplication.main(XmlFileOutputApplication.java:26) [classes/:na]
- 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.
- at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:132) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
- at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
- at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- 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]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at com.sun.proxy.$Proxy39.createJobExecution(Unknown Source) ~[na:na]
- at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:134) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE]
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
- at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
- at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- 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]
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
- at com.sun.proxy.$Proxy37.run(Unknown Source) ~[na:na]
- at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:186) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:172) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:166) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
- ... 5 common frames omitted
- 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
- 2018-12-08 22:27:41.017 INFO 21172 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
- 2018-12-08 22:27:41.017 INFO 21172 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
- @Data
- @AllArgsConstructor
- @Builder
- @NoArgsConstructor
- public class Customer {
- private Long id;
- private String firstName;
- private String lastName;
- private String birthdate;
- }
- public class CustomerRowMapper implements RowMapper<Customer> {
- @Override
- public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
- return Customer.builder().id(rs.getLong("id"))
- .firstName(rs.getString("firstName"))
- .lastName(rs.getString("lastName"))
- .birthdate(rs.getString("birthdate")).build();
- }
- }
- @Configuration
- public class JobConfiguration {
- @Autowired
- private JobBuilderFactory jobBuilderFactory;
- @Autowired
- private StepBuilderFactory stepBuilderFactory;
- @Autowired
- private DataSource dataSource;
- @Bean
- public JdbcPagingItemReader<Customer> customerPagingItemReader(){
- // reading database records using JDBC in a paging fashion
- JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
- reader.setDataSource(this.dataSource);
- reader.setFetchSize(1000);
- reader.setRowMapper(new CustomerRowMapper());
- // Sort Keys
- Map<String, Order> sortKeys = new HashMap<>();
- sortKeys.put("id", Order.ASCENDING);
- // MySQL implementation of a PagingQueryProvider using database specific features.
- MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
- queryProvider.setSelectClause("id, firstName, lastName, birthdate");
- queryProvider.setFromClause("from customer");
- queryProvider.setSortKeys(sortKeys);
- reader.setQueryProvider(queryProvider);
- return reader;
- }
- @Bean
- public StaxEventItemWriter<Customer> customerItemWriter() throws Exception{
- String customerOutputPath = File.createTempFile("customerOutput", ".out").getAbsolutePath();
- System.out.println(">> Output Path = "+customerOutputPath);
- Map<String, Class> aliases = new HashMap<>();
- aliases.put("customer", Customer.class);
- XStreamMarshaller marshaller = new XStreamMarshaller();
- marshaller.setAliases(aliases);
- // StAX and Marshaller for serializing object to XML.
- StaxEventItemWriter<Customer> writer = new StaxEventItemWriter<>();
- writer.setRootTagName("customers");
- writer.setMarshaller(marshaller);
- writer.setResource(new FileSystemResource(customerOutputPath));
- writer.afterPropertiesSet();
- return writer;
- }
- @Bean
- public Step step1() throws Exception {
- return stepBuilderFactory.get("step1")
- .<Customer, Customer>chunk(100)
- .reader(customerPagingItemReader())
- .writer(customerItemWriter())
- .build();
- }
- @Bean
- public Job job() throws Exception {
- return jobBuilderFactory.get("job")
- .start(step1())
- .build();
- }
- }
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/test
- spring.datasource.username=root
- spring.datasource.password=root
- spring.batch.initialize-schema=always
- @SpringBootApplication
- @EnableBatchProcessing
- public class XmlFileOutputApplication implements CommandLineRunner{
- @Autowired
- private JobLauncher jobLauncher;
- @Autowired
- private Job job;
- public static void main(String[] args) {
- SpringApplication.run(XmlFileOutputApplication.class, args);
- }
- @Override
- public void run(String... args) throws Exception {
- JobParameters jobParameters = new JobParametersBuilder()
- .addString("JobId", String.valueOf(System.currentTimeMillis()))
- .addDate("date", new Date())
- .addLong("time",System.currentTimeMillis()).toJobParameters();
- JobExecution execution = jobLauncher.run(job, jobParameters);
- System.out.println("STATUS :: "+execution.getStatus());
- }
- }
Add Comment
Please, Sign In to add comment