19 using System.Collections;
20 using System.ComponentModel;
21 using System.Configuration;
23 using System.Diagnostics;
25 using System.Net.Sockets;
26 using System.Runtime.Remoting;
27 using System.Runtime.Remoting.Channels;
28 using System.ServiceProcess;
29 using System.Threading;
32 [assembly: log4net.Config.XmlConfigurator(Watch=
true)]
34 namespace Lucene.Net.Distributed.Operations
44 private System.ComponentModel.Container components = null;
45 private ServiceController scMonitor =
new ServiceController();
46 private Thread serviceThread;
47 private int sleepTime = 5000;
48 private bool bRun =
true;
49 private string ipAddress =
"";
51 private static readonly log4net.ILog oLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
57 InitializeComponent();
65 System.ServiceProcess.ServiceBase[] ServicesToRun;
66 ServicesToRun =
new System.ServiceProcess.ServiceBase[] {
new LuceneMonitor() };
67 System.ServiceProcess.ServiceBase.Run(ServicesToRun);
74 private void InitializeComponent()
76 components =
new System.ComponentModel.Container();
77 this.ServiceName =
"LuceneMonitor";
83 protected override void Dispose(
bool disposing )
87 if (components != null)
92 base.Dispose( disposing );
98 protected override void OnStart(
string[] args)
100 ThreadStart threadStart =
new ThreadStart(MonitorService);
101 serviceThread =
new Thread(threadStart);
102 serviceThread.Start();
105 private void LogMessage(
string message)
107 this.LogMessage(message, Level.Info);
109 private void LogMessage(
string message, Level msgLevel)
111 if (msgLevel==Level.Info)
113 if (oLog.IsInfoEnabled)
116 else if (msgLevel==Level.Warn)
118 if (oLog.IsWarnEnabled)
122 private void LogMessage(
string message, Level msgLevel,
int ErrorLevel)
124 if (msgLevel==Level.Error)
126 if (oLog.IsErrorEnabled)
129 EventLog.WriteEntry(this.ServiceName, message, EventLogEntryType.Error, ErrorLevel);
134 private void MonitorService()
136 this.LogMessage(this.ServiceName+
" started");
137 scMonitor.ServiceName=
"LuceneServer";
138 this.sleepTime = (ConfigurationManager.AppSettings[
"ServiceSleepTime"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings[
"ServiceSleepTime"]) : this.sleepTime);
139 this.ipAddress = (ConfigurationManager.AppSettings[
"IPAddress"] != null ? ConfigurationManager.AppSettings[
"IPAddress"] :
"");
140 this.port = (ConfigurationManager.AppSettings[
"Port"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings[
"Port"]) : 0);
141 this.LogMessage(
"ServiceSleepTime = "+this.sleepTime.ToString()+
"; ipAddress="+this.ipAddress+
"; port="+this.port.ToString());
146 Thread.Sleep(sleepTime);
150 private void CheckService()
156 if (scMonitor.Status.Equals(ServiceControllerStatus.StopPending))
157 scMonitor.WaitForStatus(ServiceControllerStatus.Stopped);
159 if (scMonitor.Status.Equals(ServiceControllerStatus.Stopped))
162 foreach (IChannel ic
in ChannelServices.RegisteredChannels)
163 ChannelServices.UnregisterChannel(ic);
165 this.LogMessage(scMonitor.ServiceName +
" started (Service stopped or StopPending)", Level.Error, 99);
170 this.LogMessage(scMonitor.ServiceName +
" error: "+e.Message+e.StackTrace, Level.Error, 199);
179 protected override void OnStop()