Guest User

Untitled

a guest
Jul 26th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.16 KB | None | 0 0
  1. package de.securess.batch.config;
  2.  
  3. import java.io.IOException;
  4. import java.io.Writer;
  5.  
  6. import javax.sql.DataSource;
  7.  
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.batch.core.Job;
  11. import org.springframework.batch.core.Step;
  12. import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
  13. import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
  14. import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
  15. import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
  16. import org.springframework.batch.core.repository.JobRepository;
  17. import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
  18. import org.springframework.batch.item.ItemReaderException;
  19. import org.springframework.batch.item.ParseException;
  20. import org.springframework.batch.item.database.JdbcCursorItemReader;
  21. import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
  22. import org.springframework.batch.item.file.FlatFileHeaderCallback;
  23. import org.springframework.batch.item.file.FlatFileItemWriter;
  24. import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
  25. import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Qualifier;
  28. import org.springframework.boot.context.properties.ConfigurationProperties;
  29. import org.springframework.boot.jdbc.DataSourceBuilder;
  30. import org.springframework.context.annotation.Bean;
  31. import org.springframework.context.annotation.Configuration;
  32. import org.springframework.context.annotation.Primary;
  33. import org.springframework.core.io.FileSystemResource;
  34. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  35.  
  36.  
  37. import com.zaxxer.hikari.HikariDataSource;
  38.  
  39. import de.securess.batch.model.MyItem;
  40.  
  41. @Configuration
  42. @EnableBatchProcessing
  43. public class BatchConfiguration extends DefaultBatchConfigurer{
  44.  
  45. private Logger log = LoggerFactory.getLogger(getClass().getName());
  46.  
  47.  
  48. @Autowired
  49. private JobBuilderFactory jobBuilderFactory;
  50.  
  51. @Autowired
  52. private StepBuilderFactory stepBuilderFactory;
  53.  
  54.  
  55. @Bean
  56. public DataSource fodataSource() {
  57. return DataSourceBuilder
  58. .create()
  59. .driverClassName("org.postgresql.Driver")
  60. .url("jdbc:postgresql://localhost:5432/otherdb")
  61. .username("postgres")
  62. .password("password")
  63. .type(HikariDataSource.class)
  64. .build();
  65. }
  66.  
  67. @Bean
  68. @Primary
  69. public DataSource dataSource() {
  70. return DataSourceBuilder
  71. .create()
  72. .driverClassName("org.postgresql.Driver")
  73. .url("jdbc:postgresql://localhost:5432/postgres")
  74. .username("postgres")
  75. .password("password")
  76. .type(HikariDataSource.class)
  77. .build();
  78. }
  79.  
  80.  
  81. @Bean
  82. JdbcCursorItemReader<MyItem> myItemReader(){
  83. JdbcCursorItemReader<MyItem> reader = new JdbcCursorItemReaderBuilder<MyItem>()
  84. .dataSource(fodataSource())
  85. .name("reader")
  86. .sql("Select somefield1,somefield2 from somewhere")
  87. .rowMapper(new MyItemRowMapper())
  88. .build();
  89. log.debug("JB: "+reader.getDataSource().toString());
  90. return reader;
  91. }
  92.  
  93. @Bean
  94. public MyItemProcessor processor() {
  95. return new MyItemProcessor();
  96. }
  97.  
  98. @Bean
  99. public FlatFileItemWriter<MyItem> myItemWriter(){
  100. FlatFileItemWriter<MyItem> myItemWriter = new FlatFileItemWriter<MyItem>();
  101. myItemWriter.setResource(new FileSystemResource(CSV_FILE_OUT));
  102. myItemWriter.setHeaderCallback(new FlatFileHeaderCallback() {
  103. public void writeHeader(Writer writer) throws IOException {
  104. writer.write("somefield1;somefield2");
  105. }
  106. });
  107. myItemWriter.setLineAggregator(new DelimitedLineAggregator<MyItem>() {{
  108. setDelimiter(";");
  109. setFieldExtractor(new BeanWrapperFieldExtractor<MyItem>() {{
  110. setNames(new String[] {"somefield1","somefield2"});
  111. }});
  112. }});
  113.  
  114. return myItemWriter;
  115. }
  116. @Bean
  117. public Job exportUserJob(@Qualifier("step1") Step step1) throws ParseException, ItemReaderException, Exception {
  118. Job job = jobBuilderFactory.get("exportUserJob")
  119. .start(step1)
  120. .build();
  121. return job;
  122. }
  123.  
  124. @Bean
  125. protected Step step1(JdbcCursorItemReader<MyItem> reader,
  126. MyItemProcessor processor,
  127. FlatFileItemWriter<MyItem> writer) {
  128. Step step = stepBuilderFactory.get("step1")
  129. .<MyItem,MyItem> chunk(10)
  130. .reader(myItemReader())
  131. .processor(processor())
  132. .writer(myItemWriter())
  133. .build();
  134. log.info("JB: "+step.toString());
  135. return step;
  136. }
  137.  
  138.  
  139.  
  140. private static final String CSV_FILE_OUT = "csv/out.csv";
  141.  
  142. }
  143.  
  144. package de.securess.batch;
  145.  
  146. import org.junit.Assert;
  147. import org.junit.Test;
  148. import org.junit.runner.RunWith;
  149. import org.springframework.batch.core.JobExecution;
  150. import org.springframework.batch.test.JobLauncherTestUtils;
  151. import org.springframework.beans.factory.annotation.Autowired;
  152. import org.springframework.boot.test.context.SpringBootTest;
  153. import org.springframework.test.context.junit4.SpringRunner;
  154.  
  155. @RunWith(SpringRunner.class)
  156. @SpringBootTest(classes= de.securess.batch.config.BatchConfiguration.class)
  157. public class SpringBatchJdbcFlatApplicationTests {
  158.  
  159. @Autowired
  160. private JobLauncherTestUtils jobLauncherTestUtils;
  161.  
  162. @Test
  163. public void contextLoads() throws Exception {
  164.  
  165. JobExecution jobExecution = jobLauncherTestUtils.launchJob();
  166. Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
  167. }
  168. }
  169.  
  170. 2018-07-26 19:18:09.057 ERROR 10060 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@79a13920] to prepare test instance [de.securess.batch.SpringBatchJdbcFlatApplicationTests@7f2c57fe]
  171.  
  172. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'de.securess.batch.SpringBatchJdbcFlatApplicationTests': Unsatisfied dependency expressed through field 'jobLauncherTestUtils'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.test.JobLauncherTestUtils' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  173. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  174. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  175. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  176. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  177. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:401) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  178. at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  179. at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  180. at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) ~[spring-boot-test-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE]
  181. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  182. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  183. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  184. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit.jar:4.12]
  185. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  186. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  187. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  188. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit.jar:4.12]
  189. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit.jar:4.12]
  190. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit.jar:4.12]
  191. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit.jar:4.12]
  192. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit.jar:4.12]
  193. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  194. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  195. at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit.jar:4.12]
  196. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  197. at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit.jar:4.12]
  198. at org.junit.runner.JUnitCore.run(JUnitCore.java:115) [junit.jar:4.12]
  199. at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42) [org.junit.vintage.engine_4.12.0.v20170910-2246.jar:4.12.0]
  200. at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) ~[na:1.8.0_162]
  201. at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_162]
  202. at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_162]
  203. at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_162]
  204. at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_162]
  205. at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_162]
  206. at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) ~[na:1.8.0_162]
  207. at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) ~[na:1.8.0_162]
  208. at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_162]
  209. at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) ~[na:1.8.0_162]
  210. at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:83) ~[org.junit.vintage.engine_4.12.0.v20170910-2246.jar:4.12.0]
  211. at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:74) ~[org.junit.vintage.engine_4.12.0.v20170910-2246.jar:4.12.0]
  212. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170) ~[org.junit.platform.launcher_1.0.0.v20170910-2246.jar:1.0.0]
  213. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154) ~[org.junit.platform.launcher_1.0.0.v20170910-2246.jar:1.0.0]
  214. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90) ~[org.junit.platform.launcher_1.0.0.v20170910-2246.jar:1.0.0]
  215. at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86) ~[.cp/:na]
  216. at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) ~[.cp/:na]
  217. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) ~[.cp/:na]
  218. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) ~[.cp/:na]
  219. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) ~[.cp/:na]
  220. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) ~[.cp/:na]
  221. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.test.JobLauncherTestUtils' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  222. at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  223. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  224. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  225. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  226. ... 47 common frames omitted
Add Comment
Please, Sign In to add comment