IScheduledEventProvider Interface

Provides the methods by which the event provider host interacts with a hosted scheduled event provider. An event provider collects events and submits them to a Notification Services application, where they are matched with subscription information to produce notifications.

Namespace: Microsoft.SqlServer.NotificationServices
Assembly: Microsoft.SqlServer.NotificationServices (in microsoft.sqlserver.notificationservices.dll)

Syntax

'Declaration
Public Interface IScheduledEventProvider
public interface IScheduledEventProvider
public interface class IScheduledEventProvider
public interface IScheduledEventProvider
public interface IScheduledEventProvider

Remarks

Hosted event providers are hosted by the Notification Services engine. Hosted scheduled event providers run on a specified schedule.

This interface provides a framework for developing a hosted, scheduled event provider. Any hosted, scheduled event providers that you develop must implement this interface to be controlled by the Notification Services engine.

This interface provides the following methods:

  • Initialize allows the provider host to inform the event provider to initialize itself prior to running.

  • Run allows the provider host to ask the event provider to start collecting events.

  • Terminate allows the provider host to ask the event provider to stop collecting events, and release any resources it is holding.

The provider host calls Initialize when the system starts. This method passes required parameters to the event provider so that it can prepare to start running.

The provider host calls Run after the call to Initialize returns successfully. The Run call tells the event provider to start collecting and submitting events.

The provider host calls the Terminate method to tell the event provider to stop collecting events. It does this if the event provider is not responding (for example, if it doesn't return from the Run or Initialize calls within the specified time-out), or if the event provider requests to be stopped.

Note

When using a scheduled event provider to poll a data source for new information, Microsoft recommends that you persist "watermark" information that can be used to determine what event data is new. For instance, you might want to save the date and time of the last event batch submission in a database. That way, you will have the data available to refer to in case of system outages for which you must restart the event provider.

For more information about custom event providers, see Developing a Custom Event Provider.

Example

Use the following interface declaration to start developing your hosted scheduled event provider.

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

In the following example, the event provider reads a log file and creates events for any entries with the type 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();
        }
    }
}

Platforms

Development Platforms

For a list of the supported platforms, see Hardware and Software Requirements for Installing SQL Server 2005.

Target Platforms

For a list of the supported platforms, see Hardware and Software Requirements for Installing SQL Server 2005.

See Also

Reference

IScheduledEventProvider Members
Microsoft.SqlServer.NotificationServices Namespace