Sample programs

This topic contains two sample programs that demonstrate how you can use EmbeddedScript with CHCCLP.

The following example connects to Access Server and two datastores, creates a subscription, maps a few tables, and starts replication.

import com.ibm.replication.cdc.scripting.EmbeddedScript;
import com.ibm.replication.cdc.scripting.EmbeddedScriptException;
import java.text.MessageFormat;

public class Sample1
{
   public static void main(String[] args)
   {
      EmbeddedScript script = new EmbeddedScript();

      try
      {
         script.open();

         script.execute(connect server username user1 password pass1);
         script.execute(connect datastore name DS1 context source);
         script.execute(connect datastore name DS2 context target);
         script.execute(add subscription name SUB1);

         String mapping = add table mapping
            + sourceSchema {0} sourceTable {1}
            + targetSchema {2} targetTable {3};

         script.execute(MessageFormat.format(mapping, new Object[] {
            USER1, TABLE_1, USER1, TABLE_1 }));
         script.execute(MessageFormat.format(mapping, new Object[] {
            USER1, TABLE_2, USER1, TABLE_2 }));
         script.execute(MessageFormat.format(mapping, new Object[] {
            USER1, TABLE_3, USER1, TABLE_3 }));

         script.execute(start mirroring);
         script.execute(disconnect server);
      }
      catch (EmbeddedScriptException e)
      {
         System.out.println(e.getResultCodeAndMessage());
      }
      finally
      {
         script.close();
      }
   }
}

The following example connects to Access Server and two datastores, gets the list of subscriptions with monitor summary and starts any that are not currently mirroring.

import com.ibm.replication.cdc.scripting.EmbeddedScript;
import com.ibm.replication.cdc.scripting.EmbeddedScriptException;
import com.ibm.replication.cdc.scripting.Result;
import com.ibm.replication.cdc.scripting.ResultStringTable;
public class Sample2
{
   public static void main(String[] args)
   {
      EmbeddedScript script = new EmbeddedScript();

      try
      {
         script.open();

         script.execute(connect server username user1 password pass1);
         script.execute(connect datastore name DS1 context source);
         script.execute(connect datastore name DS2 context target);
         script.execute(monitor replication);

         Result result = script.getResult();
         if (result.getType() == Result.TABLE)
         {
            ResultStringTable table = (ResultStringTable) result;
            
            // Prints the table to the console window.
            table.display(System.out);
            
            String subscription;
            String state;
            
            for (int row = 0, rows = table.getRowCount(); row < rows; row++)
            {
               subscription = table.getValueAt(row, SUBSCRIPTION);
               state = table.getValueAt(row, STATE);
               if (state.equals(Inactive))
               {
                  System.out.println(Starting  + subscription);
                  script.execute(start mirroring name  + subscription);
               }
            }
         }
         
         script.execute(disconnect server);
      }
      catch (EmbeddedScriptException e)
      {
         System.out.println(e.getResultCodeAndMessage());
      }
      finally
      {
         script.close();
      }
   }
}