IEventProvider Interface

Provides a framework for developing a hosted continuous 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 IEventProvider
public interface IEventProvider
public interface class IEventProvider
public interface IEventProvider
public interface IEventProvider

Remarks

Hosted event providers are hosted by the Notification Services engine. Hosted continuous event providers run until terminated. The engine calls the Initialize, Run, and Terminate methods as necessary.

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

The IEventProvider 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 Notification Services engine starts. This method passes required parameters to the event provider so that it can prepare to run.

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 stop event collection. It does this if the event provider is not responding (for example, if it does not return from the Run or Initialize calls within the specified time-out), or if the event provider requests to be stopped.

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

Example

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

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

The following example creates a continuous event provider that monitors a directory for text files. When it detects a new file, it parses the event data and submits it into the instance of Notification Services. It then renames the text file and moves it to an archiving directory.

using System;
using System.IO;
using System.Text;
using Microsoft.SqlServer.NotificationServices;
using System.Collections.Specialized;

namespace Adventureworks.Notifications.EventProviders
{  
    // This event provider picks up text files from a legacy
    // system, parses them, and submits the data as events to
    // the Notification Services system. These files provide 
    // information about customers whose order levels have 
    // dropped significantly. 
    public class OrderVarianceProvider : IEventProvider
    {
        NSApplication orderApplication = null;
        string eventDirectory = null;
        string eventClassName= null;
        string eventProviderName= null;
        FileSystemWatcher textWatcher= null;


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

        // Implement the IEventProvider.Initialize method.
        // appstate     = NSApplication that hosts the event provider.
        // providerName = Event provider name from application definition.
        // args         = StringDictionary object for name/value pairs 
        //                used for initialization arguments.
        // stopDelegate = StopHandler delegate that enables the 
        //                event provider to terminate itself.
        public void Initialize(NSApplication appState,
            string providerName,
            StringDictionary args,
            StopHandler stopDelegate)
        {
            this.orderApplication = appState;
            this.eventProviderName = providerName;
        
            // This event provider requires 2 arguments.
            // eventClass     = Event class name for which this event
            //                  provider submits events.
            // eventDirectory = Full path to the location where the 
            //                  event provider picks up event files.
            if (2 == args.Count)
            {
                this.eventClassName = args["eventClass"];
                this.eventDirectory = args["eventDirectory"];
            }
            else
            {
                throw new ArgumentException(
                    "Inadequate number of arguments supplied.");
            }

            // If necessary, validate argument values.
        }

        //Implement the IEventProvider.Run method.
        public Boolean Run()
        {           
            bool returnValue = true; 
    
            // Process any existing event files.
            DirectoryInfo eventDirectoryInfo = 
                new DirectoryInfo(eventDirectory);

            foreach (FileInfo eventFile 
                in eventDirectoryInfo.GetFiles("*.txt")) 
            {
                ProcessEventFile(eventFile.FullName);
            }

            // Create a FileSystemWatcher 
            // to watch for new event files.
            textWatcher = new FileSystemWatcher();
            textWatcher.Path = eventDirectory;
            textWatcher.NotifyFilter = NotifyFilters.FileName;
            textWatcher.Filter = "*.txt";

            // Add an event handler.
            textWatcher.Created 
                += new FileSystemEventHandler(OnCreated);

            // Begin watching for files.
            textWatcher.EnableRaisingEvents = true;

            return returnValue;
        }
        // The OnCreated event handler calls 
        // ProcessEventFile when a new event file 
        // appears in the watched directory.
        private void OnCreated(object source, 
            FileSystemEventArgs e)
        {
            ProcessEventFile(e.FullPath);
        }

        // This method is called initially in Run()
        // and then each time a text file is placed
        // in the watched directory. It parses the
        // text file and submits the data as events.
        private void ProcessEventFile(string fileName)
        {
            // Create the Event object.
            Event orderEvent = 
                new Event(orderApplication, eventClassName);

            // Create the EventCollector object.
            EventCollector orderEventCollector = 
                new EventCollector(orderApplication, eventProviderName);

            try
            {

                // Read event data out of the text file.
                StreamReader fileReader = new StreamReader(fileName);
                string textEvent;
                while ((textEvent = fileReader.ReadLine()) != null)
                {
                    string[] eventFields = textEvent.Split('|');
                    for (int i = 0; i < eventFields.Length; i++) 
                    {
                        // Set the event field in the MyEvent object.
                        orderEvent[i] = eventFields[i];
                    }

                    // Write the event to the EventCollector.
                    orderEventCollector.Write(orderEvent);
                }

                // Submit the event batch to the
                // Notification Services application database.
                int eventsSubmitted = orderEventCollector.Commit();
                orderEvent = null;
                

                // Close the StreamReader.
                fileReader.Close();

                // Move and rename the file once it processed.
                FileInfo renameFile = new FileInfo(fileName);
                StringBuilder builder = new StringBuilder(
                    @"C:\complete\events");
                builder.Append(DateTime.Now.Day);
                builder.Append(DateTime.Now.Month);
                builder.Append(DateTime.Now.Year);
                builder.Append(DateTime.Now.Hour);
                builder.Append(DateTime.Now.Minute);
                builder.Append(DateTime.Now.Second);
                builder.Append(".done");
                string newName = builder.ToString(); 
                renameFile.MoveTo(newName);

            }
            catch (Exception ex)
            {
                orderEventCollector.Abort();
                // Provide additional error 
                // handling here if needed.
            }
        }

        // Implement the IEventProvider.Terminate method.
        public void Terminate()
        {
            // Remove the event handler.
            if (null != textWatcher)
            {
                textWatcher.Created -= 
                    new FileSystemEventHandler(OnCreated);
            }
        }
    }
}

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

IEventProvider Members
Microsoft.SqlServer.NotificationServices Namespace