Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.securess.batch.config;
- import java.io.IOException;
- import java.io.Writer;
- import javax.sql.DataSource;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.batch.core.Job;
- import org.springframework.batch.core.Step;
- import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
- import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
- import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
- import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
- import org.springframework.batch.core.repository.JobRepository;
- import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
- import org.springframework.batch.item.ItemReaderException;
- import org.springframework.batch.item.ParseException;
- import org.springframework.batch.item.database.JdbcCursorItemReader;
- import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
- import org.springframework.batch.item.file.FlatFileHeaderCallback;
- import org.springframework.batch.item.file.FlatFileItemWriter;
- import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
- import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.jdbc.DataSourceBuilder;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import org.springframework.core.io.FileSystemResource;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import com.zaxxer.hikari.HikariDataSource;
- import de.securess.batch.model.MyItem;
- @Configuration
- @EnableBatchProcessing
- public class BatchConfiguration extends DefaultBatchConfigurer{
- private Logger log = LoggerFactory.getLogger(getClass().getName());
- @Autowired
- private JobBuilderFactory jobBuilderFactory;
- @Autowired
- private StepBuilderFactory stepBuilderFactory;
- @Bean
- public DataSource fodataSource() {
- return DataSourceBuilder
- .create()
- .driverClassName("org.postgresql.Driver")
- .url("jdbc:postgresql://localhost:5432/otherdb")
- .username("postgres")
- .password("password")
- .type(HikariDataSource.class)
- .build();
- }
- @Bean
- @Primary
- public DataSource dataSource() {
- return DataSourceBuilder
- .create()
- .driverClassName("org.postgresql.Driver")
- .url("jdbc:postgresql://localhost:5432/postgres")
- .username("postgres")
- .password("password")
- .type(HikariDataSource.class)
- .build();
- }
- @Bean
- JdbcCursorItemReader<MyItem> myItemReader(){
- JdbcCursorItemReader<MyItem> reader = new JdbcCursorItemReaderBuilder<MyItem>()
- .dataSource(fodataSource())
- .name("reader")
- .sql("Select somefield1,somefield2 from somewhere")
- .rowMapper(new MyItemRowMapper())
- .build();
- log.debug("JB: "+reader.getDataSource().toString());
- return reader;
- }
- @Bean
- public MyItemProcessor processor() {
- return new MyItemProcessor();
- }
- @Bean
- public FlatFileItemWriter<MyItem> myItemWriter(){
- FlatFileItemWriter<MyItem> myItemWriter = new FlatFileItemWriter<MyItem>();
- myItemWriter.setResource(new FileSystemResource(CSV_FILE_OUT));
- myItemWriter.setHeaderCallback(new FlatFileHeaderCallback() {
- public void writeHeader(Writer writer) throws IOException {
- writer.write("somefield1;somefield2");
- }
- });
- myItemWriter.setLineAggregator(new DelimitedLineAggregator<MyItem>() {{
- setDelimiter(";");
- setFieldExtractor(new BeanWrapperFieldExtractor<MyItem>() {{
- setNames(new String[] {"somefield1","somefield2"});
- }});
- }});
- return myItemWriter;
- }
- @Bean
- public Job exportUserJob(@Qualifier("step1") Step step1) throws ParseException, ItemReaderException, Exception {
- Job job = jobBuilderFactory.get("exportUserJob")
- .start(step1)
- .build();
- return job;
- }
- @Bean
- protected Step step1(JdbcCursorItemReader<MyItem> reader,
- MyItemProcessor processor,
- FlatFileItemWriter<MyItem> writer) {
- Step step = stepBuilderFactory.get("step1")
- .<MyItem,MyItem> chunk(10)
- .reader(myItemReader())
- .processor(processor())
- .writer(myItemWriter())
- .build();
- log.info("JB: "+step.toString());
- return step;
- }
- private static final String CSV_FILE_OUT = "csv/out.csv";
- }
- package de.securess.batch;
- import org.junit.Assert;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.batch.core.JobExecution;
- import org.springframework.batch.test.JobLauncherTestUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes= de.securess.batch.config.BatchConfiguration.class)
- public class SpringBatchJdbcFlatApplicationTests {
- @Autowired
- private JobLauncherTestUtils jobLauncherTestUtils;
- @Test
- public void contextLoads() throws Exception {
- JobExecution jobExecution = jobLauncherTestUtils.launchJob();
- Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
- }
- }
- 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]
- 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)}
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:401) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) ~[spring-boot-test-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE]
- at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit.jar:4.12]
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit.jar:4.12]
- at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit.jar:4.12]
- at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit.jar:4.12]
- at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit.jar:4.12]
- at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit.jar:4.12]
- at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit.jar:4.12]
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit.jar:4.12]
- at org.junit.runner.JUnitCore.run(JUnitCore.java:115) [junit.jar:4.12]
- at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42) [org.junit.vintage.engine_4.12.0.v20170910-2246.jar:4.12.0]
- at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) ~[na:1.8.0_162]
- at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_162]
- at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_162]
- at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_162]
- at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_162]
- at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_162]
- at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) ~[na:1.8.0_162]
- at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) ~[na:1.8.0_162]
- at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_162]
- at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) ~[na:1.8.0_162]
- at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:83) ~[org.junit.vintage.engine_4.12.0.v20170910-2246.jar:4.12.0]
- at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:74) ~[org.junit.vintage.engine_4.12.0.v20170910-2246.jar:4.12.0]
- at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170) ~[org.junit.platform.launcher_1.0.0.v20170910-2246.jar:1.0.0]
- at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154) ~[org.junit.platform.launcher_1.0.0.v20170910-2246.jar:1.0.0]
- at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90) ~[org.junit.platform.launcher_1.0.0.v20170910-2246.jar:1.0.0]
- at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:86) ~[.cp/:na]
- at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) ~[.cp/:na]
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) ~[.cp/:na]
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) ~[.cp/:na]
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) ~[.cp/:na]
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) ~[.cp/:na]
- 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)}
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
- ... 47 common frames omitted
Add Comment
Please, Sign In to add comment