package com.daffodilwoods.demo.simple;
/*
* Replicator - class Sample application OneToOne
*/
import com.daffodilwoods.replicator.dataSources.JDBCDataSource;
import com.daffodilwoods.replicator.server.Replicator;
import com.daffodilwoods.replicator.server.datasources.RepSource;
import com.daffodilwoods.replicator.server.datasources.RepTarget;
import com.daffodilwoods.replicator.server.jobs.OneToOneJob;
import com.daffodilwoods.replicator.server.jobs._Job;
import com.daffodilwoods.replicator.server.schedule.Schedule;
import com.daffodilwoods.replicator.server.schedule._OccurrenceTypes;
/**
* This is Sample application which show The replication of one data base into
* another data base. SQL Server is taken in this sample as source data base as
* well target data base
*/
public class OneToOne {
private String SOURCE_DATABASE_SERVER_NAME = "localhost";
private String SOURCE_SERVER_PORT = "1433";
private String SOURCE_DATABASE_NAME = "Northwind";
private String SOURCE_USER_NAME = "sa";
private String SOURCE_PASSWORD = "hrhk";
private String SOURCE_DATA_SOURCE_NAME = "Northwind";
private String SOURCE_DATASOUREC_CLASSNAME = "com.microsoft.jdbcx.sqlserver.SQLServerDataSource";
private String SOURCE_REPLICATION_HOME = "C:/source";
private String SOURCE_TABLENAMES[] = { "dbo.Customers" };
// The following constants specifies data base server name OR IP Address,
// data base port no , data base name , user-name , password
// and , source name and replication home path for TARGET data source
private String TARGET_DATABASE_SERVER_NAME = "localhost";
private String TARGET_SERVER_PORT = "1433";
private String TARGET_DATABASE_NAME = "target";
private String TARGET_USER_NAME = "sa";
private String TARGET_PASSWORD = "hrhk";
private String TARGET_DATA_SOURCE_NAME = "target";
private String TARGET_DATASOUREC_CLASSNAME = "com.microsoft.jdbcx.sqlserver.SQLServerDataSource";
private String TARGET_REPLICATION_HOME = "C:/target";
// Constants to define the table names as schemaName.tableName
private String TARGET_TABLENAMES[] = { "dbo.Customers"};
// Constants to define the Job name
private String JOB_NAME = "OneToOne";
// Constants to define the Schedule name,OccurrenceType,OccurrenceInterval
private String SCHEDULE_NAME = "scheduleName";
private int OCCURRENCETYPE = _OccurrenceTypes.MINUTE;
private int OCCURRENCEINTERVAL = 2;
private SampleJBDCDataSource sampleJdbcDataSource = new SampleJBDCDataSource();
public void setSourceDataSource(Replicator repServer) {
JDBCDataSource sourceDataSource;
try {
JDBCDataSource sourceJDBCDataSource = sampleJdbcDataSource.getJDBCDataSource(SOURCE_USER_NAME,
SOURCE_PASSWORD, SOURCE_DATABASE_NAME, SOURCE_SERVER_PORT, SOURCE_DATABASE_SERVER_NAME,
SOURCE_DATA_SOURCE_NAME, SOURCE_DATASOUREC_CLASSNAME, SOURCE_REPLICATION_HOME, SOURCE_TABLENAMES);
sourceJDBCDataSource.enableExistingDataForReplication(true);
repServer.addDataSource(sourceJDBCDataSource);
System.out.println(" Data soruce one created successfully ");
} catch (Exception e) {
e.printStackTrace();
}
}
public void setTargetDataSource(Replicator repServer) {
JDBCDataSource targetDataSoruce;
try {
JDBCDataSource targetJDBCDataSoruce = sampleJdbcDataSource.getJDBCDataSource(TARGET_USER_NAME,
TARGET_PASSWORD, TARGET_DATABASE_NAME, TARGET_SERVER_PORT, TARGET_DATABASE_SERVER_NAME,
TARGET_DATA_SOURCE_NAME, TARGET_DATASOUREC_CLASSNAME, TARGET_REPLICATION_HOME, null);
repServer.addDataSource(targetJDBCDataSoruce);
/* To add Replication DataSource follow following configruation*/
//RemoteDataSource targetRemoteDataSource = new RemoteDataSource();
//targetRemoteDataSource.setReplicatorClientIPAddress("169.254.182.164");
//targetRemoteDataSource.setReplicatorClientPortNo(new Integer("5010"));
//targetRemoteDataSource.setDataSourceName("target");
//repServer.addDataSource(targetRemoteDataSource);
System.out.println(" Data soruce Two created successfully ");
} catch (Exception e) {
e.printStackTrace();
}
}
public void performReplicationJob(Replicator repServer)throws Exception {
try {
RepSource soruceDataSource = new RepSource(repServer, SOURCE_DATA_SOURCE_NAME);
RepTarget targetDataSource = new RepTarget(repServer, TARGET_DATA_SOURCE_NAME);
targetDataSource.setCreateSchema(true);
targetDataSource.populateExistingSourceData(true);
Schedule schedule = new Schedule();
schedule.setScheduleName("scheduleName");
schedule.setOccurrenceType(_OccurrenceTypes.HOUR);
schedule.setOccurrenceInterval(2);
_Job job = new OneToOneJob(soruceDataSource, targetDataSource);
job.setName(JOB_NAME);
repServer.addJob(job);
System.out.println(" Going to perform the job ");
repServer.doJob(JOB_NAME);
// if there is more than one job to be executed then use
// repServer.doJobs();
System.out.println(" Job performed successfully ");
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Job Excecution faild : "+e.getMessage());
}
}
/**
* Creating replication server instance and add data sources to server
*
*/
public void replicateDataSource() {
try {
Replicator repServer = new Replicator();
repServer.setReplicationHome(SOURCE_REPLICATION_HOME);
OneToOne oneToOne = new OneToOne();
oneToOne.setSourceDataSource(repServer);
oneToOne.setTargetDataSource(repServer);
oneToOne.performReplicationJob(repServer);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new OneToOne().replicateDataSource();
}
}