及时消息生成lucene

管理员 .NET C#及时消息生成lucene已关闭评论7,547字数 2326阅读7分45秒阅读模式

上次我们讲的是生成全部索引,现在继续我们上次说的话题,通过消息队列及时生成lucene索引,在这里我们不阐述怎么来创建ActiveMQ服务,只介绍发送、接收来生成lucene的方式。

1、首先我们要添加一条数据到数据库中文章源自运维生存时间-https://www.ttlsa.com/csharp/create-message-lucene/

        /// <summary>
        /// FilmTab添加一条数据
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="description">描述</param>
        /// <returns></returns>
        public static int AddFilmTab(string title, string description)
        {
            string sql = "insert into FilmTbl(title,description) values(@title,@description)select @@identity";
            Common.DB.SqlParam param = new Common.DB.SqlParam();
            param.AddParam("@title", title);
            param.AddParam("@description", description);
            return St.ToInt32(BaseInfoDB.GetFirst(sql, param));
        }

2、完成添加到数据库后我们要把添加成功的数据的ID获取到,并发送MQ消息。文章源自运维生存时间-https://www.ttlsa.com/csharp/create-message-lucene/

int id=AddFilmTab("test title","test description");
SendAddProductMessage(id);//开始发送消息

///发送mq的方法,此方法需要引用第三方的几个dll,(Apache.NMS.ActiveMQ.dll,Apache.NMS.dll,MQ.dll)最后一个dll是我整理出来的,如果不清楚怎么写请加入我们的群,里面已经上传完成的代码。文章源自运维生存时间-https://www.ttlsa.com/csharp/create-message-lucene/

 public static void SendAddProductMessage(int id)
        {
            Sender sender = new Sender();
            sender.SetQueueName(St.ConfigKey("MQAddProduct"));
            Dictionary<string, object> msg = new Dictionary<string, object>();
            msg.Add("MethodType", 1);
            ArrayList list = new ArrayList();
            list.Add(id.ToString());
            msg.Add("MethodParameter", list);
            sender.Send(msg);
        }

3、接下来就是接收mq消息.并单条生成lucene文件了,注意:接收mq消息最好是个windows服务,这样服务器注销时还可以继续执行方法,文章源自运维生存时间-https://www.ttlsa.com/csharp/create-message-lucene/

while (true)//写个死循环,一直执行读取mq方法
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
IQueueConsumer consumer = ConsumerFactory.CreateQueueConsumer(St.ConfigKey("MQAddProduct"));
dictionary = consumer.ReceiveMapMessage();//以上三行代码全部都是创建mq对象所用
if ((dictionary == null) || !string.IsNullOrEmpty(consumer.ErrorMessage))
{
    continue;
}
ArrayList list = (ArrayList)dictionary["MethodParameter"];
string id = Convert.ToString(list[0]);//这里已经获取到mq发送过来的id了。
if (!string.IsNullOrEmpty(id))
{
//这里的main方法就是我们生成大圈时所用到的方法.
//具体代码进入以下链接查看:https://www.ttlsa.com/csharp/lucene-net-to-achieve-high-performance-in-reading-and-writing/
//但这里务必注意id此时千万不能为空,否则整个lucene都会被清空,只留最新的一条数据。
    main(id);
    
}
}

好了。lucene的mq就介绍到这里了,因为lucene是单线程的,所以生成lucene的时候千万注意要加线程锁,否则lucene的列会乱。所以这几天我正在寻找其它的搜索方式来代替luceue,已经看了一个叫hubbledotnet的一个c#的东东。觉得还不错,这几天正在测试阶段。如果并发不错的话下次我会写一个hubble.net的文章。文章源自运维生存时间-https://www.ttlsa.com/csharp/create-message-lucene/ 文章源自运维生存时间-https://www.ttlsa.com/csharp/create-message-lucene/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
管理员
  • 本文由 发表于 27/03/2014 00:27:22
  • 转载请务必保留本文链接:https://www.ttlsa.com/csharp/create-message-lucene/
  • Lucene