您的位置:首页 > 编程语言 > ASP

单例模式查看新闻采集进度

2007-11-05 17:54 267 查看
最近这两天给我们公司的网站后台添加一个采集新闻的东西。由于之前用asp做过小偷之类的东西,所以搞起来也是比较轻车熟路。给大家分享一下我的控制采集进度的代码。主要用到单例模式批量添加等,其中Gather类似存放采集规则。

 

<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-0706585667512395&dt=1192846434656&lmt=1192846428&format=728x90_as&output=html&correlator=1192846434640&google_ad_channel=0432115118&url=http://blog.csdn.net/sbkyv/archive/2007/11/05/1868088.aspx" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="allowtransparency"></iframe>


using System;


using System.Collections;


using System.Threading;


using System.Data;


using System.Data.SqlClient;


using System.Configuration;




namespace common.Gather




...{




    /**//// <summary>


    /// GatherNews使用单例模式,方便查看采集进度,方便结束采集进程。


    /// </summary>


    public class GatherNews




    ...{


        private Gather _gather = null;


        private static GatherNews _gathernews = null;




        private bool _stop = false;


        private int _count = 0;




        private GatherNews(Gather obj)




        ...{


            _gather = obj;


        }






        /**//// <summary>


        /// 返回采集新闻类的实例


        /// </summary>


        /// <param name="obj"></param>


        /// <returns></returns>


        public static GatherNews Instance(Gather obj)




        ...{


            if(_gathernews==null)




            ...{


                if(obj==null)


                    return null;




                Mutex mutex = new Mutex();


                mutex.WaitOne();


                if(_gathernews==null)


                    _gathernews = new GatherNews(obj);


                mutex.Close();


            }


            return _gathernews;


        }




        public void Start()




        ...{


            StopCurrentThread();//判断线程状态是否为停止标识,如果是则结束当前线程。




            if ( _gather == null )




            ...{


                _gathernews = null;


                Thread.CurrentThread.Abort();


                return;


            }


            GatherListConfig lc = _gather.ListConfig;


            ArrayList _newsUrls =  GatherList.GatherNewsList(ref lc,_gather.Url);




            if ( _newsUrls==null )




            ...{


                _gathernews = null;




                Thread.CurrentThread.Abort();


                return;


            }




            NewsCellSet ncs = new NewsCellSet();




            for(int i=0;i<_newsUrls.Count;i++)




            ...{


                //如果状态标识为停止则跳出循环。


                if ( _stop == true )


                    break;




                NewsCell nc = GatherContent.GatherNewsContent(_gather.ContentConfig,GatherTool.GetAbsoluteUrl(_gather.Url,_newsUrls[i].ToString()));




                if ( nc != null )




                ...{


                    ncs.NewsCells.Add(nc);


                    _count ++ ;


                    if(_count>=_gather.Config.Num)




                    ...{


                        _stop = true;


                        break;


                    }


                }


            }






            /**////////////////////////////////////////执行数据写入操作//////////////////////////////////////////////




            执行数据库写入操作#region 执行数据库写入操作


                


            InsertNewsFromNewsCellSet(ncs);




            #endregion




            if (_gather.ListConfig.NextPage.Length==0)




            ...{


                _gathernews = null;


                Thread.CurrentThread.Abort();


                return;


            }


            _gather.Url = GatherTool.GetAbsoluteUrl(_gather.Url,lc.NextPage);




            Start();


        }




        public bool Stop()




        ...{


            _stop = true;


            return true;


        }






        /**//// <summary>


        /// 结束当前线程


        /// </summary>


        private void StopCurrentThread()




        ...{


            if(_stop==true)




            ...{


                _stop = false;


                _gathernews = null;


                Thread.CurrentThread.Abort();


            }


        }






        /**//// <summary>


        /// 批量写入数据库


        /// </summary>


        /// <param name="_news"></param>


        private void InsertNewsFromNewsCellSet(NewsCellSet _news)




        ...{


            if ( _news.HasResults == true )




            ...{


                SqlDataAdapter adapter = new SqlDataAdapter();


                DataTable dt = new DataTable();




                SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["myConnectString"]);




                SqlCommand insertCommand = new SqlCommand();


                insertCommand.CommandType = CommandType.Text;


                insertCommand.CommandText = "Insert Into T_news(topic,author,content,source,recorder,newstype) values(@topic,@author,@content,@source,@recorder,@newstype)";


                insertCommand.Connection = conn;




                SqlParameter _topic = new SqlParameter("@topic",SqlDbType.VarChar, 200, "topic"); 


                SqlParameter _author = new SqlParameter("@author",SqlDbType.VarChar, 20, "author"); 


                SqlParameter _content = new SqlParameter("@content",SqlDbType.Text, 8000,"content"); 


                SqlParameter _source = new SqlParameter("@source",SqlDbType.VarChar, 200, "source"); 


                SqlParameter _recorder = new SqlParameter("@recorder",SqlDbType.VarChar, 20, "recorder"); 


                SqlParameter _newstype = new SqlParameter("@newstype",SqlDbType.Int, 4, "newstype"); 


                


                insertCommand.Parameters.Add(_topic);


                insertCommand.Parameters.Add(_author);


                insertCommand.Parameters.Add(_content);


                insertCommand.Parameters.Add(_recorder);


                insertCommand.Parameters.Add(_source);


                insertCommand.Parameters.Add(_newstype);




                adapter.SelectCommand=new SqlCommand("select topic,author,content,source,recorder,newstype from T_news where id is null",conn);


                adapter.InsertCommand = insertCommand;




                adapter.Fill(dt);


                foreach(NewsCell nc in _news.NewsCells)




                ...{


                    //dt.Rows.Add.NewRow();


                    //int i = dt.Rows.Count;


                    DataRow dr = dt.NewRow();


                    dr["topic"] = nc.Topic;


                    dr["author"] = nc.Author;


                    dr["content"] = nc.Content;


                    dr["source"] = nc.Source;


                    dr["recorder"] = "";


                    dr["newstype"] = _gather.Config.Type;


                    dt.Rows.Add(dr);


                }


                try




                ...{


                    conn.Open();


                    adapter.Update(dt);


                }


                catch(Exception ex)




                ...{


                    throw(ex);


                }


                finally




                ...{


                    if ( conn.State == ConnectionState.Open )


                            conn.Close();


                }






            }


        }




        public Gather UsingGather




        ...{




            get...{return _gather;}


        }


        public int Count




        ...{




            get...{ return _count; }


        }


    }


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null 数据库 system asp class