正则表达式进行大文件查找时的超时处理

       应用系统服务器端的日志查找有很多的实现方式,限于软件、资源的问题,决定自己开发个远程日志单点查看工具,实现原理:

服务端职责:提供消息接收、监听,提供日志搜索查找与反馈

客户端职责:发送查询指令,接收查询结果

UI:提供关键字(支持正则表达式)、全字、大小写查找,可设定开始时间、可搜索的机器及其目录……

其中C/S通过Socket的方式连接通讯。

 

因为提供到正则表达式支持,所以在不正确的输入正则表达式时,会造成搜索查找非常耗时,所以在附加正则时,需要进行超时处理,具体如何实现,且看下文:

public class AsynchronousRegex
    {
        private MatchCollection mc;
        private bool isMatch = false;
        private int _timeout;
        private int sleepCounter = 0;
        private int sleepInterval = 100;

        public bool IsTimeout
        {
            get;
            set;
        }

        public AsynchronousRegex(int timeout)
        {
            this._timeout = timeout;
            this.IsTimeout = false;
            this.mc = null;
        }

        /// 
        /// get matches items
        /// 
        /// 
        /// 
        /// 
        /// 
        public MatchCollection Matches(string input, string regex, RegexOptions options)
        {
            Reg r = new Reg(regex, input, options);
            r.OnMatchComplete += new Reg.MatchCompleteHandler(this.MatchCompleteHandler);
            Thread t = new Thread(new ThreadStart(r.Matchs));
            t.Start();

            this.Sleep(t);

            t = null;
            return mc;
        }

        /// 
        /// check match 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool IsMatch(string input, string regex, RegexOptions options)
        {
            Reg r = new Reg(regex, input, options);
            r.OnIsMatchComplete += new Reg.IsMatchCompleteHandler(this.IsMatchCompleteHandler);

            Thread t = new Thread(new ThreadStart(r.IsMatch));
            t.Start();

            this.Sleep(t);

            t = null;
            return isMatch;
        }

        private void Sleep(Thread t)
        {
            if (t != null && t.IsAlive)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(this.sleepInterval));
                this.sleepCounter++;
                if (this.sleepCounter * this.sleepInterval >= this._timeout)
                {
                    t.Abort();
                    this.IsTimeout = true;
                }
                else
                {
                    this.Sleep(t);
                }
            }
        }

        private void MatchCompleteHandler(MatchCollection mc)
        {
            this.mc = mc;
        }

        private void IsMatchCompleteHandler(bool _IsMatch)
        {
            this.isMatch = _IsMatch;
        }

        class Reg
        {
            internal delegate void MatchCompleteHandler(MatchCollection mc);
            internal delegate void IsMatchCompleteHandler(bool isMatch);
            internal event MatchCompleteHandler OnMatchComplete;
            internal event IsMatchCompleteHandler OnIsMatchComplete;

            public Reg(string regex, string input, RegexOptions options)
            {
                this.Regexs = regex;
                this.Inputs = input;
                this.Options = options;
            }

            /// 
            /// 内容
            /// 
            public string Inputs
            {
                get;
                set;
            }

            /// 
            /// 规则
            /// 
            public string Regexs
            {
                get;
                set;
            }

            public RegexOptions Options
            {
                get;
                set;
            }

            internal void Matchs()
            {
                MatchCollection mc = Regex.Matches(this.Inputs, Regexs, Options);
                if (mc != null && mc.Count > 0)    // 这里有可能造成cpu资源耗尽
                {
                    this.OnMatchComplete(mc);
                }
            }

            internal void IsMatch()
            {
                Regex regex = new Regex(Regexs, Options);
                if (regex.IsMatch(this.Inputs))
                {
                    this.OnIsMatchComplete(true);
                }
            }
        }
    }

你可能感兴趣的:(Java,正则表达式,regex,string,thread,input,null)