【问题标题】:How to fetch data effectively from kestrel queue如何有效地从红隼队列中获取数据
【发布时间】:2011-10-01 01:50:39
【问题描述】:

出于某种原因,我们计划在我们的项目中使用 kestrel 队列。我们做了一些恶魔,主要问题是如何有效地从 CPU 利用率低的队列中获取数据。我们实现获取的方式是,如果我们从队列中获取数据失败超过 5 次,我们将线程休眠 100ms 以降低 CPU 利用率。

while (running) {
            try {
                LoginLogQueueEntry data = kestrelQueue.fetch();
                if (null != data && data.isLegal()) {
                    entryCacheList.add(data); //add the data to the local caceh
                    resetStatus();
                } else {
                    failedCount++;
                    //if there is no data in the kestrel and the local cache is not empty, insert the data into mysql database
                    if (failedCount == 1 && !entryCacheList.isEmpty()) {
                        resetStatus();
                        insertLogList(entryCacheList); // insert current data into database
                        entryCacheList.clear(); //empty local cache
                    }

                    if (failedCount >= 5 && entryCacheList.isEmpty()) {
                    //fail 5 times. Sleep current thread. 
                        failedCount = 0;
                        Thread.sleep((sleepTime + MIN_SLEEP_TIME) % MAX_SLEEP_TIME);
                    }
                }
                //Insert 1000 rows once
                if (entryCacheList.size() >= 1000) {
                    insertLogList(entryCacheList);
                    entryCacheList.clear();
                }
            } catch (Exception e) {
                logger.warn(e.getMessage());
            }

还有其他好的方法吗?我认为最完美的方式是队列可以通知工作人员我们获取数据并获取它们。

【问题讨论】:

    标签: java message-queue kestrel


    【解决方案1】:

    请参阅http://robey.lag.net/2008/11/27/scarling-to-kestrel.html 上的“阻止提取”部分

    【讨论】:

      【解决方案2】:

      这里描述了阻塞读取,在“Memcache commands”下:https://github.com/robey/kestrel/blob/master/docs/guide.md

      您可以通过用斜杠分隔选项标志来将选项标志添加到 get 命令,以便从“作业”队列中获取项目,最多等待一秒钟:

      get jobs/t=1000
      

      如果一秒钟后队列中没有显示任何内容,您将得到相同的空响应,只是比现在晚一秒钟。 :)

      在执行此操作时调整响应超时很重要。如果您使用超时为一秒的阻塞读取,但您的客户端库的响应超时为 500 毫秒,则库将在阻塞读取完成之前断开与服务器的连接。因此,请确保响应超时大于您在读取请求中使用的超时。

      【讨论】:

        【解决方案3】:

        您需要使用阻塞获取。我无法找到 API 文档,但我发现一篇文章表明它可以在 kestrel 中实现。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-24
          • 1970-01-01
          • 2012-09-18
          • 1970-01-01
          • 2010-11-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多