[DBMoto API]外部からレプリケーションやグループを開始・停止するサンプルC#プログラム


DBMoto APIを使用することで、GUIツールであるDBMoto Management Centerからの操作ではなく、バッチを実行することで外部からDBMotoの操作を制御することが可能です。

ここではDBMotoのレプリケーションジョブやグループジョブを外部から制御するプログラムをご紹介します。
これらを使用することで、任意のタイミングでジョブを実行・停止したり、他アプリケーションとの連携が可能となります。

●レプリケーションを有効化・無効化するサンプルC#プログラム

using System;
using System.Collections.Generic;
using System.Text;
using HiTSoftware.DBMoto.Application;
using HiTSoftware.DBMoto.ObjectModel;

namespace APISamples
{
    public class SampleReplicationAsynch
    {
        public static void Main()
        {
            DBMotoApplication dbmApp = null;
            IServer dbmServer = null;
            IMetadata currentMetadata = null;

            try
            {
                // 初期設定、サーバ接続
                dbmApp = DBMotoApplication.Instance;
                dbmServer = dbmApp.Servers["local"];
                dbmServer.Connect();

                // カレントメタデータのロード
                currentMetadata = dbmServer.Metadatas.DefaultMetadata;
                currentMetadata.Load(true);

                // レプリケーション名
                IReplication repl = currentMetadata.Replications["Replication"];

                // レプリケーション有効化
                if (repl.ReplStatus == ReplStatus.Disabled)
                    repl.AsynchEnable();

                // 待機
                System.Threading.Thread.Sleep(3000);

                // レプリケーション無効化
                if (repl.ReplStatus != ReplStatus.Disabled)
                    repl.AsynchDisable();

                // 待機
                System.Threading.Thread.Sleep(3000);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (currentMetadata != null)
                    currentMetadata.Unload();
                if (dbmServer != null)
                    dbmServer.Disconnect();
                if (dbmApp != null)
                    dbmApp.Dispose();
            }
        }

    }
}

●グループを有効化・無効化するサンプルC#プログラム

using System;
using System.Collections.Generic;
using System.Text;
using HiTSoftware.DBMoto.Application;
using HiTSoftware.DBMoto.ObjectModel;

namespace APISamples
{
    public class SampleGroupAsynch
    {
        public static void Main()
        {
            DBMotoApplication dbmApp = null;
            IServer dbmServer = null;
            IMetadata currentMetadata = null;

            try
            {
                // 初期設定、サーバ接続
                dbmApp = DBMotoApplication.Instance;
                dbmServer = dbmApp.Servers["local"];
                dbmServer.Connect();

                // カレントメタデータのロード
                currentMetadata = dbmServer.Metadatas.DefaultMetadata;
                currentMetadata.Load(true);

                // グループ名
                IGroup group = currentMetadata.Groups["Group"];

                // グループ有効化
                if (group.ReplStatus == ReplStatus.Disabled)
                    group.AsynchEnable();

                // 待機
                System.Threading.Thread.Sleep(3000);

                // グループ無効化
                if (group.ReplStatus != ReplStatus.Disabled)
                    group.AsynchDisable();

                // 待機
                System.Threading.Thread.Sleep(3000);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (currentMetadata != null)
                    currentMetadata.Unload();
                if (dbmServer != null)
                    dbmServer.Disconnect();
                if (dbmApp != null)
                    dbmApp.Dispose();
            }
        }

    }
}
関連したトピックス

コメントを残す

メールアドレスが公開されることはありません。

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください