Home      DB      Replicator      CRM      Solutions  
 
Daffodil Replicator
 
Explore Replicator (OS)
 

Product Overview
Download Daffodil Replicator (E)
Getting Started Guide
User Guide
Features
How it Works?
Usage Scenarios
Usage Examples
Recommended JDBC Drivers
Replicator Class Reference
Sample Java Code
Error Messages
Daffodil Replicator (E) FAQ
Daffodil Replicator (E) BLOG
User Forum
Buy
 
Sample Java Code > Subscription


Subscription Code


package com.daffodilwoods.demo.simple;

/*
 *    Replicator - class Sample application SubscriptionReplicatorSimpleCode
 
 */

import com.daffodilwoods.ftp.server.FTPParameters;
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 SubscriptionReplicatorSimpleCode {

    // Parameters to set the server
    private static String serverSystemName = "server";
    private static String serverRepHome = "d:\rephome";
    private static int serverFTPPort = 1230;

    /**
     * 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 {
        SubscriptionReplicatorSimpleCode subscriptionReplicatorSimpleCode = new SubscriptionReplicatorSimpleCode();
        Replicator repServer = subscriptionReplicatorSimpleCode.addDataSourceToReplicator();
        PublisherDataSource publisherDataSource = subscriptionReplicatorSimpleCode.configurePublisher(repServer);
        SubscriberDataSource subscriberDataSource = subscriptionReplicatorSimpleCode.configureSubscriber(repServer);

        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);
        }
    }

    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("D:/rephome");
        FTPParameters parameters = new FTPParameters();
        parameters.setHostName(serverSystemName);
        parameters.setRootDir(serverRepHome);
        parameters.setPort(serverFTPPort);
        repServer.setFTPServerParameters(parameters);
        repServer.start();
        return repServer;
    }

    public Replicator addDataSourceToReplicator() throws RepException {
        Replicator repServer = getReplicatorInstance();

        // Add Publisher data source to the replication server object
        RemoteDataSource sourceRemoteDataSource = new RemoteDataSource();
        sourceRemoteDataSource.setReplicatorClientIPAddress("169.254.182.164");
        sourceRemoteDataSource.setReplicatorClientPortNo(new Integer(1050));
        sourceRemoteDataSource.setDataSourceName("source");
        repServer.addDataSource(sourceRemoteDataSource);

        // Add Subscriber data source to the replication server object
        RemoteDataSource targetRemoteDataSource = new RemoteDataSource();
        targetRemoteDataSource.setReplicatorClientIPAddress("169.254.182.164");
        targetRemoteDataSource.setReplicatorClientPortNo(new Integer("5010"));
        targetRemoteDataSource.setDataSourceName("target");
        repServer.addDataSource(targetRemoteDataSource);
        return repServer;

    }

    public PublisherDataSource configurePublisher(Replicator repServer) throws RepException {
        PublisherDataSource publisherDataSource = new PublisherDataSource(repServer, "source");
        return publisherDataSource;
    }

    public SubscriberDataSource configureSubscriber(Replicator repServer) throws RepException {
        SubscriberDataSource subscriberDataSource = new SubscriberDataSource(repServer, "target");
        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;
    }
}


 
  ©2008 Daffodil Software Ltd. Contact Us | Disclaimer | Sitemap