How to java-configure separate datasources for spring batch data and business data? Should I even do it?

后端 未结 7 1558
感情败类
感情败类 2020-11-29 22:33

My main job does only read operations and the other one does some writing but on MyISAM engine which ignores transactions, so I wouldn\'t require necessarily tr

7条回答
  •  遥遥无期
    2020-11-29 22:55

    I have my data sources in a separate configuration class. In the batch configuration, we extend DefaultBatchConfigurer and override the setDataSource method, passing in the specific database to use with Spring Batch with a @Qualifier. I was unable to get this to work using the constructor version, but the setter method worked for me.

    My Reader, Processor, and Writer's are in their own self contained classes, along with the steps.

    This is using Spring Boot 1.1.8 & Spring Batch 3.0.1. Note: We had a different setup for a project using Spring Boot 1.1.5 that did not work the same on the newer version.

    package org.sample.config.jdbc;
    
    import javax.sql.DataSource;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.env.Environment;
    
    import com.atomikos.jdbc.AtomikosDataSourceBean;
    import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
    
    /**
     * The Class DataSourceConfiguration.
     *
     */
    @Configuration
    public class DataSourceConfig {
    
        private final static Logger log = LoggerFactory.getLogger(DataSourceConfig.class);
    
        @Autowired private Environment env;
    
        /**
         * Siphon data source.
         *
         * @return the data source
         */
        @Bean(name = "mainDataSource")
        @Primary
        public DataSource mainDataSource() {
    
            final String user = this.env.getProperty("db.main.username");
            final String password = this.env.getProperty("db.main.password");
            final String url = this.env.getProperty("db.main.url");
    
            return this.getMysqlXADataSource(url, user, password);
        }
    
        /**
         * Batch data source.
         *
         * @return the data source
         */
        @Bean(name = "batchDataSource", initMethod = "init", destroyMethod = "close")
        public DataSource batchDataSource() {
    
            final String user = this.env.getProperty("db.batch.username");
            final String password = this.env.getProperty("db.batch.password");
            final String url = this.env.getProperty("db.batch.url");
    
            return this.getAtomikosDataSource("metaDataSource", this.getMysqlXADataSource(url, user, password));
        }
    
        /**
         * Gets the mysql xa data source.
         *
         * @param url the url
         * @param user the user
         * @param password the password
         * @return the mysql xa data source
         */
        private MysqlXADataSource getMysqlXADataSource(final String url, final String user, final String password) {
    
            final MysqlXADataSource mysql = new MysqlXADataSource();
            mysql.setUser(user);
            mysql.setPassword(password);
            mysql.setUrl(url);
            mysql.setPinGlobalTxToPhysicalConnection(true);
    
            return mysql;
        }
    
        /**
         * Gets the atomikos data source.
         *
         * @param resourceName the resource name
         * @param xaDataSource the xa data source
         * @return the atomikos data source
         */
        private AtomikosDataSourceBean getAtomikosDataSource(final String resourceName, final MysqlXADataSource xaDataSource) {
    
            final AtomikosDataSourceBean atomikos = new AtomikosDataSourceBean();
            atomikos.setUniqueResourceName(resourceName);
            atomikos.setXaDataSource(xaDataSource);
            atomikos.setMaxLifetime(3600);
            atomikos.setMinPoolSize(2);
            atomikos.setMaxPoolSize(10);
    
            return atomikos;
        }
    
    }
    
    
    package org.sample.settlement.batch;
    
    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.launch.support.RunIdIncrementer;
    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.transaction.PlatformTransactionManager;
    
    /**
     * The Class BatchConfiguration.
     *
     */
    @Configuration
    @EnableBatchProcessing
    public class BatchConfiguration extends DefaultBatchConfigurer {
        private final static Logger log = LoggerFactory.getLogger(BatchConfiguration.class);
        @Autowired private JobBuilderFactory jobs;
        @Autowired private StepBuilderFactory steps;
        @Autowired private PlatformTransactionManager transactionManager;
        @Autowired @Qualifier("processStep") private Step processStep;
    
        /**
         * Process payments job.
         *
         * @return the job
         */
        @Bean(name = "processJob")
        public Job processJob() {
            return this.jobs.get("processJob")
                        .incrementer(new RunIdIncrementer())
                        .start(processStep)
                        .build();
        }
    
        @Override
        @Autowired
        public void setDataSource(@Qualifier("batchDataSource") DataSource batchDataSource) {
            super.setDataSource(batchDataSource);
        }
    }
    

提交回复
热议问题