0

This is my Spring Mongo configuration for normal Spring project . I want to know how to edit it for Spring-Batch.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;



@Configuration
public class MongoConfiguration
{

    @Autowired
    ResourceLoader       resourceLoader;

    @Value("${mongodb.keyStoreFilePath}")
    private String       keyStoreFilePath;

    @Value("${mongodb.keyStoreType}")
    private String       keyStoreType;

    @Value("${mongodb.keyStorePassword}")
    private String       keyStorePassword;

    @Value("${mongodb.databasePort}")
    private String       databasePort;

    @Value("${mongodb.databaseName}")
    private String       databaseName;

    @Value("${mongodb.dbUserName}")
    private String       dbUserName;

    @Value("${mongodb.dbPassword}")
    private String       dbPassword;

    @Value("#{'${mongodb.seeds}'.split(',')}")
    private List<String> seeds;


    @Bean
    @Profile("default")
    public MongoTemplate mongoTemplate() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException,
            NoSuchAlgorithmException, CertificateException, IOException
    {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return new MongoTemplate(mongoDbFactory(), converter);
    }



    @Bean
    @Profile("default")
    public MongoDbFactory mongoDbFactory() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException,
            NoSuchAlgorithmException, CertificateException, IOException
    {
        return new SimpleMongoDbFactory(mongoClient(), databaseName);

    }


    @Bean
    @Profile("default")
    public MongoClient mongoClient() throws IOException, KeyStoreException, NoSuchAlgorithmException,
            CertificateException, UnrecoverableKeyException, KeyManagementException
    {

        try (InputStream readStream = new FileInputStream(keyStoreFilePath))
        {
            KeyStore ksClient = KeyStore.getInstance(keyStoreType);
            ksClient.load(readStream, keyStorePassword.toCharArray());
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ksClient, keyStorePassword.toCharArray());

            KeyStore ksCACert = KeyStore.getInstance(KeyStore.getDefaultType());
            ksCACert.load(new FileInputStream(keyStoreFilePath), keyStorePassword.toCharArray());

            TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
            tmf.init(ksCACert);

            SSLContext context = SSLContext.getInstance("TLS"); // We now
                                                                // provide
                                                                // our alternate
                                                                // KeyManager
            context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            SSLSocketFactory sslSocketFactory = context.getSocketFactory();

            MongoClientOptions mongoClientOptions =
                    MongoClientOptions.builder().sslEnabled(true).socketFactory(sslSocketFactory).build();

            List<ServerAddress> seedList = new ArrayList<>();
            for (String ip : seeds)
            {
                seedList.add(new ServerAddress(ip, Integer.parseInt(databasePort)));
            }
            return new MongoClient(seedList,
                    MongoCredential.createCredential(dbUserName, databaseName, dbPassword.toCharArray()),
                    mongoClientOptions);
        }

    }

}

Now I want to add this above Mongo configuration into my Spring-Batch configuration. How should I do it? (Basically it's a migration from Oracle to MongoDB) Below code is my previous configuration using Oracle DB. Need to modify this for MongoDB. I mean I am confused what and all i should add and remove, like - JobRepository how to modify and all.

    import java.sql.SQLException;

    import javax.sql.DataSource;

    import org.apache.commons.httpclient.HostConfiguration;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
    import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
    import org.springframework.batch.core.repository.JobRepository;
    import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;

    @Configuration
    public class ApplicationConfiguration extends DefaultBatchConfigurer {


        private DataSource dataSource;
        public HttpClient httpClient;

        @Value("#{'${connectionTimeOut}'}")
        int connectionTimeOut;
        @Value("#{'${maxTotalConnections}'}")
        int maxTotalConnections;
        @Value("#{'${defaultMaxConnections}'}")
        int defaultMaxConnections;
        @Value("#{'${hostInstance}'}")
        String hostInstance;
        @Value("#{'${wagProxy}'}")
        String wagProxy;
        @Value("#{'${wagProxyPort}'}")
        int wagProxyPort;
        @Value("#{'${connectionManagerTimeOut}'}")
        int connectionManagerTimeOut;


            @Override
            protected JobRepository createJobRepository() throws Exception {
                JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
                factory.setDataSource(dataSource);
                factory.setTransactionManager(this.getTransactionManager());
                factory.afterPropertiesSet();
            return factory.getObject();
        }
            @Bean
            @Primary
            @ConfigurationProperties(prefix = "batch.datasource")
            @Qualifier("profileDataSource")
            public DataSource batchDataSource() throws SQLException {
                dataSource = DataSourceBuilder.create().build();
                super.setDataSource(dataSource);
                return dataSource;
            }

            @Bean
            @Primary
            @Qualifier("httpClient")
            public HttpClient httpClientConnection() {
                HttpConnectionManagerParams conManagerparams = null;
                MultiThreadedHttpConnectionManager connectionManager = null;
                conManagerparams = new HttpConnectionManagerParams();
                conManagerparams.setTcpNoDelay(true);
                conManagerparams.setStaleCheckingEnabled(true);
                conManagerparams.setConnectionTimeout(connectionTimeOut);
                conManagerparams.setDefaultMaxConnectionsPerHost(defaultMaxConnections);
                conManagerparams.setMaxTotalConnections(maxTotalConnections);
                connectionManager = new MultiThreadedHttpConnectionManager();
                connectionManager.setParams(conManagerparams);
                httpClient = new HttpClient(connectionManager);
                HostConfiguration hostCfg = new HostConfiguration();
                hostCfg.setHost(hostInstance);
                httpClient.setHostConfiguration(hostCfg);
                httpClient.getHostConfiguration().setProxy(wagProxy,wagProxyPort);
                httpClient.getParams().setConnectionManagerTimeout(connectionManagerTimeOut);
                return httpClient;
            }

    }
noob
  • 774
  • 1
  • 10
  • 23
user3472166
  • 11
  • 2
  • 8

1 Answers1

1

Basically its a migration from Oracle to MongoDB

For reading from Oracle, you can use for example the JdbcCursorItemReader (or other DB readers provided by Spring Batch):

@Bean
public JdbcCursorItemReader<Foo> itemReader() {
    return new JdbcCursorItemReaderBuilder<Foo>()
        .dataSource(batchDataSource()) // change accordingly if you use another data source
        .name("fooReader")
        .sql("SELECT * FROM FOO")
        .beanRowMapper(Foo.class)
        .build();
}

For writing to MongoDB, you have already defined the mongoTemplate so you can use it to create a MongoItemWriter:

@Bean
public MongoItemWriter<String> itemWriter() {
    return new MongoItemWriterBuilder<String>()
        .template(mongoTemplate())
        .build();
}

Then you define a chunk-oriented step to read from Oracle and write to MongoDB.

Hope this helps.

EDIT: Adding a mongo reader sample:

@Bean
public MongoItemReader<Foo> mongoItemReader() { 
    Query query; // specify your mongo query
    return new MongoItemReaderBuilder<Foo>()
        .template(mongoTemplate())
        .targetType(Foo.class)
        .query(query)
        .build();
}
Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • But i want to use mongo db reader, not other reader. What and all config i should add in the configuration class – user3472166 Oct 12 '18 at 09:08
  • Ah ok I understood that you are reading from oracle when you said "migration from Oracle to MongoDB". In that case, same for the mongo reader, it uses the mongo template. I edited the question to include a mongo reader sample. – Mahmoud Ben Hassine Oct 12 '18 at 10:06
  • I want to know Whether we should config JobRepository or not? and use of setTargettype and setCollection --> Please tell me its uses what we actually set in this – user3472166 Oct 12 '18 at 10:09
  • If you want to persist Spring Metadata in a persistent store then yes, otherwise no need to configure it, a (in-memory) Map based job repository will be used by default. – Mahmoud Ben Hassine Oct 12 '18 at 10:12