using System;
using System.Threading;
namespace ConsoleApplication5
{
internal class Program
{
private static void Main(string[]
args)
{
var publisher = new Publisher();
var johnsSubscriber = new
JohnsSubscriber();
var stevesSubscriber = new
StevesSubcsriber();
publisher.SqlServerDownEvent += johnsSubscriber.SendJohnEmail;
publisher.SqlServerDownEvent += stevesSubscriber.SendSteveText;
while (true)
{
if (PingSqlServer() == false)
{
//When SQL server is down raise event
Console.WriteLine("Server down!");
publisher.RaiseEvent();
}
else
{
Console.WriteLine("All is good");
}
Thread.Sleep(1000);
}
}
private static bool PingSqlServer()
{
//All this method does is randomly return a false
var random = new Random();
int i = random.Next(1, 30);
if (i > 20)
{
return false;
}
else
{
return true;
}
}
}
public class Publisher
{
public event EventHandler<ServerDownEventArgs>
SqlServerDownEvent;
public void
RaiseEvent()
{
SqlServerDownEvent(this, new ServerDownEventArgs(DateTime.Now));
}
}
public class ServerDownEventArgs : EventArgs
{
public readonly DateTime Dt;
public ServerDownEventArgs(DateTime
dt)
{
Dt = dt;
}
}
public class JohnsSubscriber
{
public void
SendJohnEmail(object sender, ServerDownEventArgs e)
{
Console.WriteLine("An
email was set to John, that the server went down as {0}",
e.Dt);
}
}
public class StevesSubcsriber
{
public void
SendSteveText(object sender, ServerDownEventArgs e)
{
Console.WriteLine("A
text was set to Steve, that the server went down as {0}",
e.Dt);
}
}
}
No comments:
Post a Comment
Comments will appear once they have been approved by the moderator