Share via


IScheduledEventProvider Interface

イベント プロバイダ ホストとホストされる定期的なイベント プロバイダとの対話処理で使用されるメソッドを提供します。イベント プロバイダはイベントを収集し、Notification Services アプリケーションに送信します。このアプリケーションでイベントとサブスクリプション情報が照合され、通知が生成されます。

名前空間: Microsoft.SqlServer.NotificationServices
アセンブリ: Microsoft.SqlServer.NotificationServices (microsoft.sqlserver.notificationservices.dll 内)

構文

'宣言
Public Interface IScheduledEventProvider
public interface IScheduledEventProvider
public interface class IScheduledEventProvider
public interface IScheduledEventProvider
public interface IScheduledEventProvider

解説

ホストされるイベント プロバイダは、Notification Services エンジンによってホストされます。ホストされる定期的なイベント プロバイダは、指定されたスケジュールで実行されます。

このインターフェイスは、ホストされる定期的なイベント プロバイダを開発するためのフレームワークです。ホストされる定期的なイベント プロバイダを開発する場合、そのイベント プロバイダには、このインターフェイスを Notification Services エンジンによって制御されるように実装する必要があります。

このインターフェイスには、次のメソッドが用意されています。

  • Initialize は、プロバイダ ホストが、実行前に、イベント プロバイダに自身を初期化するように通知します。

  • Run は、プロバイダ ホストが、イベントの収集の開始をイベント プロバイダに指示します。

  • Terminate は、プロバイダ ホストが、イベントの収集を停止し、保持しているリソースを解放するようにイベント プロバイダに指示します。

プロバイダ ホストは、システムの起動時に Initialize を呼び出します。このメソッドは、実行開始準備ができるように、必要なパラメータをイベント プロバイダに渡します。

プロバイダ ホストは、Initialize の呼び出しが正常に終了した後に、Run を呼び出します。Run の呼び出しは、イベントの収集および送信を開始するようにイベント プロバイダに指示します。

プロバイダ ホストは、Terminate メソッドを呼び出して、イベント プロバイダにイベントの収集を停止するよう、指示します。プロバイダ ホストは、イベント プロバイダが応答しない場合 (たとえば、プロバイダ ホストが Run または Initialize の呼び出しを、指定されたタイムアウト時間内に完了しなかった場合)、またはイベント プロバイダが停止を要求した場合に、この呼び出しを実行します。

ms149635.note(ja-jp,SQL.90).gifメモ :
定期的なイベント プロバイダを使用して新しい情報のデータ ソースをポーリングする場合、どのイベント データが新しいかを判別する場合に使用できる "ウォーターマーク" 情報を保持することをお勧めします。たとえば、データベースにバッチの最新送信日時を保存することも考えられます。この方法により、システムに障害が発生し、イベント プロバイダの再起動が必要になった場合に参照できるデータが用意されます。

カスタム イベント プロバイダの詳細については、「カスタム イベント プロバイダの開発」を参照してください。

使用例

ホストされる定期的なイベント プロバイダの開発を開始する場合は、次のインターフェイスの宣言を使用します。

public interface IScheduledEventProvider
{
    void Initialize(
            NSApplication nsApplication,
            String providerName,
            StringDictionary args,
            StopHandler stopDelegate);
    Boolean Run();
    void Terminate();
}

次の例では、イベント プロバイダはログ ファイルを読み込み、種類が Error であるエントリのイベントを作成しています。

using System;
using System.Collections.Specialized;
using Microsoft.SqlServer.NotificationServices;
using System.Diagnostics;

namespace AdventureWorks.Notifications.ScheduleEventProvider
{
    public class EventLogProvider : IScheduledEventProvider
    {
        // Initialize variables.
        NSApplication errorLogApplication = null;
        EventCollector errorEventCollector = null;
        string eventProviderName = null;
        StringDictionary arguments = null;
        string logName = null;
        DateTime eventDate = System.DateTime.Today;
        string eventClassName = null;

        public EventLogProvider()
        {
            //Add constructor logic here if required.
        }


        // Implement the IScheduledEventProvider.Initialize method.
        public void Initialize(NSApplication application,
            string providerName,
            StringDictionary args,
            StopHandler stopDelegate)
        {
            this.eventProviderName = providerName;
            this.arguments = args;
            this.errorLogApplication = application;

            // This event provider requires three arguments: the 
            // event log to check, the date used to filter the events
            // returned, and the name of the event class.
            if (3 == arguments.Count)
            {
                this.logName = arguments["logName"];
                this.eventDate = 
                    System.Convert.ToDateTime(arguments["eventDate"]);
                this.eventClassName = arguments["eventClass"];
            }
            else
            {
                throw new ArgumentException(
                    "Inadequate number of arguments supplied.");
            }

            // If necessary, validate the argument values.

            // Create the event collector.
            this.errorEventCollector = new EventCollector(
                errorLogApplication, eventProviderName);
        }


        // Implement the IScheduledEventProvider.Run method.
        public bool Run()
        {
            bool returnValue = true;
         
            try
            {
                // Open the event log & review the latest entries.
                EventLog selectedLog = new EventLog();
                selectedLog.Log = logName;
                Event errorEvent = new Event();
                errorEvent.Initialize(
                    errorLogApplication, eventClassName);
                foreach(EventLogEntry entry in selectedLog.Entries)
                {
                    if(entry.TimeWritten >= eventDate)
                        if(entry.EntryType == EventLogEntryType.Error)
                        {   
                            //Create an event for the event log data.
                            errorEvent["LogName"] = logName;
                            errorEvent["Message"] = entry.Message;
                            errorEvent["Source"] = entry.Source;
                            errorEventCollector.Write(errorEvent);
                        }
                }

                // Commit the event batch.
                int eventsSubmitted = errorEventCollector.Commit();
                errorEvent = null;
            }
            catch (Exception e)
            {
                // Provide error handling here if needed.
            }
            return returnValue;
        }


        // Implement the IScheduledEventProvider.Terminate method.
        public void Terminate()
        {
            errorEventCollector.Dispose();
        }
    }
}

プラットフォーム

開発プラットフォーム

サポートされているプラットフォームの一覧については、「SQL Server 2005 のインストールに必要なハードウェアおよびソフトウェア」を参照してください。

対象プラットフォーム

サポートされているプラットフォームの一覧については、「SQL Server 2005 のインストールに必要なハードウェアおよびソフトウェア」を参照してください。

参照

関連項目

IScheduledEventProvider Members
Microsoft.SqlServer.NotificationServices Namespace