Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在springbatch(spring-boot-1.5.2.RELEASE)中使用多个数据源在启动时引发异常_Java_Spring_Maven_Spring Boot_Spring Batch - Fatal编程技术网

Java 在springbatch(spring-boot-1.5.2.RELEASE)中使用多个数据源在启动时引发异常

Java 在springbatch(spring-boot-1.5.2.RELEASE)中使用多个数据源在启动时引发异常,java,spring,maven,spring-boot,spring-batch,Java,Spring,Maven,Spring Boot,Spring Batch,我尝试使用两个数据源,一个用于SpringBatch的元数据表,另一个用于读取/处理/写入应用程序数据库。当我试图同时使用这两种语言时,它给出了以下异常 java.lang.IllegalStateException: To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2 at org.springframework.batch.core.co

我尝试使用两个数据源,一个用于SpringBatch的元数据表,另一个用于读取/处理/写入应用程序数据库。当我试图同时使用这两种语言时,它给出了以下异常

    java.lang.IllegalStateException: To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2
    at org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.getConfigurer(AbstractBatchConfiguration.java:108) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration.initialize(SimpleBatchConfiguration.java:114) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$ReferenceTargetSource.createObject(SimpleBatchConfiguration.java:142) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.aop.target.AbstractLazyCreationTargetSource.getTarget(AbstractLazyCreationTargetSource.java:86) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.sun.proxy.$Proxy43.getJobInstances(Unknown Source)


----------

    import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
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.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import com.springbatch_sample.domain.Person;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;



    @Bean
    @Primary
    public DataSource hsqldbDataSource() throws SQLException {
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new org.hsqldb.jdbcDriver());
        dataSource.setUrl("jdbc:hsqldb:hsql://localhost:9001/xdb");
        dataSource.setUsername("sa");
        dataSource.setPassword("sa");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    @Qualifier("mysqlDataSource")
    public DataSource mysqlDataSource() throws SQLException {
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new com.ibm.db2.jcc.DB2Driver());
        dataSource.setUrl("jdbc:db2://xxxx");
        dataSource.setUsername("user");
        dataSource.setPassword("pswd");
        //DatabasePopulatorUtils.execute(databasePopulator(), dataSource);
        return dataSource;
    }

    @Bean
    public FlatFileItemReader<Person> reader() {
        FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
        reader.setResource(new ClassPathResource("sample-data.csv"));
        reader.setLineMapper(new DefaultLineMapper<Person>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] { "firstName", "lastName" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                setTargetType(Person.class);
            }});
        }});
        return reader;
    }

    @Bean
    public PersonItemProcessor processor() {
        return new PersonItemProcessor();
    }

    @Bean
    public JdbcBatchItemWriter<Person> writer() throws SQLException {
        JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
        writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
        writer.setDataSource(hsqldbDataSource());
        return writer;
    }
    // end::readerwriterprocessor[]

    // tag::jobstep[]
    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener) throws SQLException {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(step1())
                .end()
                .build();
    }

    @Bean
    public Step step1() throws SQLException {
        return stepBuilderFactory.get("step1")
                .<Person, Person> chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
    // end::jobstep[]
}
java.lang.IllegalStateException:要使用默认的批处理配置器,上下文必须包含不超过一个数据源(找到2个)
在org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.getConfigurer(AbstractBatchConfiguration.java:108)~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
在org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration.initialize(SimpleBatchConfiguration.java:114)~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
在org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$ReferenceTargetSource.createObject(SimpleBatchConfiguration.java:142)~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
在org.springframework.aop.target.AbstractLazyCreationTargetSource.getTarget(AbstractLazyCreationTargetSource.java:86)~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
位于com.sun.proxy.$Proxy43.getJobInstances(未知源)
----------
导入java.sql.SQLException;
导入javax.sql.DataSource;
导入org.springframework.batch.core.Job;
导入org.springframework.batch.core.Step;
导入org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
导入org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
导入org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
导入org.springframework.batch.core.launch.support.RunIdIncrementer;
导入org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
导入org.springframework.batch.item.database.JdbcBatchItemWriter;
导入org.springframework.batch.item.file.FlatFileItemReader;
导入org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
导入org.springframework.batch.item.file.mapping.DefaultLineMapper;
导入org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.Primary;
导入org.springframework.core.io.ClassPathResource;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.jdbc.datasource.SimpleDriverDataSource;
导入com.springbatch_sample.domain.Person;
@配置
@启用批处理
公共类批处理配置{
@自动连线
公共建筑商建筑商工厂;
@自动连线
公共StepBuilderFactory StepBuilderFactory;
@豆子
@初级的
公共数据源hsqldbDataSource()引发SQLException{
final SimpleDriverDataSource dataSource=新SimpleDriverDataSource();
setDriver(new org.hsqldb.jdbcDriver());
setUrl(“jdbc:hsqldb:hsql://localhost:9001/xdb");
dataSource.setUsername(“sa”);
dataSource.setPassword(“sa”);
返回数据源;
}
@豆子
公共JdbcTemplate JdbcTemplate(最终数据源数据源){
返回新的JdbcTemplate(数据源);
}
@豆子
@限定符(“mysqlDataSource”)
公共数据源mysqlDataSource()引发SQLException{
final SimpleDriverDataSource dataSource=新SimpleDriverDataSource();
setDriver(新的com.ibm.db2.jcc.DB2Driver());
setUrl(“jdbc:db2://xxxx”);
dataSource.setUsername(“用户”);
dataSource.setPassword(“pswd”);
//execute(databasePopulator(),dataSource);
返回数据源;
}
@豆子
公共FlatFileItemReader()读取器{
FlatFileItemReader=新的FlatFileItemReader();
setResource(新类路径资源(“sample data.csv”);
reader.setLineMapper(新的DefaultLineMapper(){{
setLineTokenizer(新的DelimitedLineTokenizer(){{
setNames(新字符串[]{“firstName”,“lastName”});
}});
setFieldSetMapper(新的BeanRapperFieldSetMapper(){{
setTargetType(Person.class);
}});
}});
返回读取器;
}
@豆子
公共PersonItemProcessor处理器(){
返回新的PersonItemProcessor();
}
@豆子
公共JdbcBatchItemWriter writer()引发SQLException{
JdbcBatchItemWriter writer=新的JdbcBatchItemWriter();
writer.setItemSqlParameterSourceProvider(新的BeanPropertyItemSqlParameterSourceProvider());
setSql(“插入人(名字、姓氏)值(:firstName,:lastName)”;
setDataSource(hsqldbDataSource());
返回作者;
}
//end::readerwriterprocessor[]
//标记::作业步骤[]
@豆子
公共作业导入器作业(JobCompletionNotificationListener侦听器)引发SQLException{
返回jobBuilderFactory.get(“importUserJob”)
.incrementer(新的RunIdIncrementer())
.listener(侦听器)
.flow(步骤1())
(完)
.build();
}
@豆子
公共步骤step1()引发SQLException{
返回stepBuilderFactory.get(“step1”)
.chunk(10)
.reader(reader())
.processor(处理器())
.writer(writer())
.build();
}
//结束::作业步骤[]
}

查看此链接。
同样的问题,我通过这个链接解决了我的问题。

请添加stacktrace。我明白了,对于工作元数据和itemWriter,你就是我们