Do I have to have a JDBC DataSource, replication data source, jobs,
subscription, and publisher java source file and if so how are they
pulled together. I expected to find a subscriber java source file
which would have the replication data source, schedule, subscription
in it and I think that is what I see in the sample but where is the
Publisher sample.
I have gone through your query and please use these below code for
Subscription.
So, you should start Replicator on LapTop and Replication DataSource
on Main Office and then perform the subscription or you can use
PublisherJDBCSubscriptionReplicatorSimpleCode.java file at Laptop ,
use SampleSubscriberReplicationDataSource.java file at Main Office and
use SampleJBDCDataSource.java file on both side.
PublisherJDBCSubscriptionReplicatorSimpleCode.java
package test;
import java.io.IOException;
import com.daffodilwoods.ftp.server.FTPParameters;
import com.daffodilwoods.replicator.dataSources.JDBCDataSource;
import com.daffodilwoods.replicator.dataSources.RemoteDataSource;
import com.daffodilwoods.replicator.server.Replicator;
import
com.daffodilwoods.replicator.server.datasources.PublisherDataSource;
import
com.daffodilwoods.replicator.server.datasources.SubscriberDataSource;
import com.daffodilwoods.replicator.server.schedule.JobSchedular;
import com.daffodilwoods.replicator.server.schedule.Schedule;
import com.daffodilwoods.replicator.server.schedule._OccurrenceTypes;
import com.daffodilwoods.replicator.server.subscription.Subscription;
import com.daffodilwoods.replicator.utils.RepException;
import com.daffodilwoods.replicator.utils.ReplicationSaver;
/**
* This is Sample application which show The replication of one data
base into
* another data base. SQL Server is taken in this sample as publisher
data base
* as well subscriber data base
*/
public class PublisherJDBCSubscriptionReplicatorSimpleCode {
// Parameters to set the server
private static String serverSystemName = "localhost";
private static String serverRepHome = "d:\\rephome";
private static int serverFTPPort = 1230;
// Parameters to set the publisher
private static String PUBLISHER_DATABASE_SERVER_NAME = "localhost";
private static String PUBLISHER_SERVER_PORT = "5432";
private static String PUBLISHER_DATABASE_NAME = "Northwind";
private static String PUBLISHER_USER_NAME = "user";
private static String PUBLISHER_PASSWORD = "pass";
private static String PUBLISHER_DATA_SOURCE_NAME = "publisher";
private static String PUBLISHER_DATASOUREC_CLASSNAME =
"org.postgresql.jdbc2.optional.PoolingDataSource";
private static String PUBLISHER_REPLICATION_HOME = "d:\\source";
private static String PUBLISHER_TABLENAMES[] = { "dbo.Customers" };
private SampleJBDCDataSource sampleJdbcDataSource = new
SampleJBDCDataSource();
/**
* if we use currentmode as subscription then we can call the
doSubscription
* method for replication. if we use currentmode as realTime we can
call the
* realtime scheduling without call the doSubscription method for
* replication. if we use currentmode as nonRealTime we can add the
schedule
* using NonRealtime scheduling for replication.
*/
private static String currentmode = "runSubscription";
public static void main(String[] args) throws RepException {
PublisherJDBCSubscriptionReplicatorSimpleCode
subscriptionReplicatorSimpleCode = new
PublisherJDBCSubscriptionReplicatorSimpleCode();
Replicator repServer =
subscriptionReplicatorSimpleCode.addSubscriberDataSourceToReplicator();
PublisherDataSource publisherDataSource =
subscriptionReplicatorSimpleCode.configurePublisher(repServer);
SubscriberDataSource subscriberDataSource =
subscriptionReplicatorSimpleCode.configureSubscriber(repServer);
subscriberDataSource.setCreateSchema(true);
subscription.setBidirectional(true);
Schedule schedule = subscriptionReplicatorSimpleCode.getSchedule();
Subscription subscription = new Subscription(publisherDataSource,
subscriberDataSource);
subscription.setName("Subscription");
repServer.addSubscription(subscription);
if (currentmode.equals("runSubscription")) {
subscriptionReplicatorSimpleCode.runSubscriptionWithoutSchedule(repServer);
} else if (currentmode.equals("realTime")) {
subscriptionReplicatorSimpleCode.addRealTimeScheduleOnSubscription(repServer,
subscription, schedule);
} else if (currentmode.equals("nonRealTime")) {
subscriptionReplicatorSimpleCode.addNonRealTimeScheduleOnSubscription(repServer,
subscription, schedule);
}
}
public void setPublisherDataSource(Replicator repServer) {
try {
JDBCDataSource publisherJDBCDataSource =
sampleJdbcDataSource.getJDBCDataSource(PUBLISHER_USER_NAME,
PUBLISHER_PASSWORD, PUBLISHER_DATABASE_NAME, PUBLISHER_SERVER_PORT,
PUBLISHER_DATABASE_SERVER_NAME, PUBLISHER_DATA_SOURCE_NAME,
PUBLISHER_DATASOUREC_CLASSNAME, PUBLISHER_TABLENAMES);
repServer.addDataSource(publisherJDBCDataSource);
System.out.println(" Data soruce one created successfully ");
} catch (Exception e) {
e.printStackTrace();
}
}
private void runSubscriptionWithoutSchedule(Replicator repServer)
throws RepException {
// if there is more than one job to be executed then use
// repServer.doSubscriptions();
repServer.doSubscription("Subscription");
}
public Replicator getReplicatorInstance() throws RepException {
Replicator repServer = new Replicator();
repServer.setReplicationHome(serverRepHome);
FTPParameters parameters = null;
try {
parameters = new FTPParameters();
} catch (IOException e) {
e.printStackTrace();
}
parameters.setHostName(serverSystemName);
parameters.setRootDir(serverRepHome);
parameters.setPort(serverFTPPort);
repServer.setFTPServerParameters(parameters);
repServer.start();
return repServer;
}
public Replicator addSubscriberDataSourceToReplicator() throws
RepException {
Replicator repServer = getReplicatorInstance();
// Add Subscriber data source to the replication server object
RemoteDataSource targetRemoteDataSource = new RemoteDataSource();
targetRemoteDataSource.setReplicatorClientIPAddress("169.254.182.171");
targetRemoteDataSource.setReplicatorClientPortNo(new
Integer("3050"));
targetRemoteDataSource.setDataSourceName("subscriber");
repServer.addDataSource(targetRemoteDataSource);
return repServer;
}
public PublisherDataSource configurePublisher(Replicator repServer)
throws RepException {
setPublisherDataSource(repServer);
PublisherDataSource publisherDataSource = new
PublisherDataSource(repServer, PUBLISHER_DATA_SOURCE_NAME);
return publisherDataSource;
}
public SubscriberDataSource configureSubscriber(Replicator repServer)
throws RepException {
SubscriberDataSource subscriberDataSource = new
SubscriberDataSource(repServer, "subscriber");
return subscriberDataSource;
}
/**
* After making the Subscription we can apply non real time schedule
using
* this method.
*
* @param repServer
* @param subscription
* @param schedule
* @throws RepException
*/
public void addNonRealTimeScheduleOnSubscription(Replicator
repServer, Subscription subscription, Schedule schedule) throws
RepException {
// set the Occurrence Type as hours
schedule.setOccurrenceType(_OccurrenceTypes.HOUR);
// set schedule interval in no of hours
schedule.setOccurrenceInterval(2);
subscription.setSchedule(schedule);
JobSchedular.getSharedInstance().scheduleSubscription(subscription,
new ReplicationSaver(), repServer);
}
/**
* After making the Subscription we can apply real time schedule
using this
* method.
*
* @param repServer
* @param Subscription
* @throws RepException
*/
public void addRealTimeScheduleOnSubscription(Replicator repServer,
Subscription subscription, Schedule schedule) throws RepException {
schedule.setOccurrenceType(_OccurrenceTypes.REALTIME);
subscription.setSchedule(schedule);
JobSchedular.getSharedInstance().scheduleSubscription(subscription,
new ReplicationSaver(), repServer);
}
// making a realTime Schedule
public Schedule getSchedule() {
Schedule schedule = new Schedule();
schedule.setScheduleName("scheduleName");
return schedule;
}
}
-----------------------------------------------------------------------------------
SampleSubscriberReplicationDataSource.java
package test;
import java.io.IOException;
import com.daffodilwoods.ftp.server.FTPParameters;
import
com.daffodilwoods.replicator.client.webservice.ReplicationDataSource;
import com.daffodilwoods.replicator.dataSources.JDBCDataSource;
import com.daffodilwoods.replicator.utils.RepException;
import
com.daffodilwoods.webservices.server.WebServiceServerProperties;
public class SampleSubscriberReplicationDataSource {
// parameters of replication datasource
private static String databaseServerName = "localhost";
private static String portNo = "1433";
private static String databaseName = "bnorthwind";
private static String userName = "user";
private static String password = "pass";
private static String dataSourceName = "subscriber";
private static String dataSourceClassName =
"com.microsoft.jdbcx.sqlserver.SQLServerDataSource";
private static String replicationHome = "D:\\target";
private static String systemName = "localhost";
private static int webServicePort = 3050;
private static int ftpPort = 4050;
public static void main(String[] args) throws RepException {
ReplicationDataSource replicationDataSource =
ReplicationDataSource.getSharedInstance();
SampleSubscriberReplicationDataSource.configureReplicationDataSource(replicationDataSource);
SampleJBDCDataSource sampleJdbcDataSource = new
SampleJBDCDataSource();
JDBCDataSource jdbcDataSource =
sampleJdbcDataSource.getJDBCDataSource(userName,
password, databaseName, portNo, databaseServerName,
dataSourceName, dataSourceClassName, null);
try {
replicationDataSource.addDataSource(jdbcDataSource);
replicationDataSource.start();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//Configure the replication datasource
public static void
configureReplicationDataSource(ReplicationDataSource
replicationClient) {
WebServiceServerProperties webClientProperties = new
WebServiceServerProperties();
webClientProperties.setPort(webServicePort);
FTPParameters parameters = null;
try {
parameters = new FTPParameters();
} catch (IOException e) {
e.printStackTrace();
}
parameters.setHostName(systemName);
parameters.setRootDir(replicationHome);
parameters.setPort(ftpPort);
replicationClient.setFTPServerParameters(parameters);
replicationClient.setWebServicesProperties(webClientProperties);
}
}
---------------------------------------------------------------------------------------------
SampleJBDCDataSource.java
package test;
import com.daffodilwoods.replicator.dataSources.JDBCDataSource;
public class SampleJBDCDataSource {
/*
* Making JDBCDataSource
*
* @param userName : set the database user name
* @param password : set the database password
* @param databaseName : set the databae name
* @param portNo : set the port number of database
* @param databaseServerName : set the database server name
* @param dataSourceName : set the datasource name like source
or target
* @param dataSourceClassName : set the class name which you
get the datasource.
* @param replicationHome : set the replication Home for set he
exception trace, schema and data in XML.
* @param tablesName : set the tables name which you want to
enables.
* @return JDBCDataSource
*/
public JDBCDataSource getJDBCDataSource(String userName, String
password, String databaseName, String portNo, String
databaseServerName, String dataSourceName, String dataSourceClassName,
String[] tablesName) {
JDBCDataSource dataSource;
try {
dataSource = new JDBCDataSource();
dataSource.setUserName(userName);
dataSource.setPassword(password);
dataSource.setDatabaseName(databaseName);
dataSource.setDatabasePortNo(portNo);
dataSource.setDatabaseServerName(databaseServerName);
dataSource.setDataSourceName(dataSourceName);
dataSource.setDataSourceClassName(dataSourceClassName);
if (tablesName != null) {
dataSource.setTable(tablesName);
}
dataSource.enableExistingDataForReplication(true);
return dataSource;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Any Confusion Please revert back
Thanks & Regards
Anil Garg