public void prepareMsg() {
    while (true) {
      BatchMessage msgTuple = null;
      try {
        msgTuple = super.getBatchQueue().poll(1, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        return;
      }
      if (msgTuple == null) {
        return;
      }

      if (msgTuple.getMsgList().size() == 0) {
        super.finish(msgTuple.getBatchId());
        return;
      }

      BatchMsgsTag partTag = new BatchMsgsTag();
      Set<String> msgIds = partTag.getMsgIds();
      for (MessageExt msg : msgTuple.getMsgList()) {
        String msgId = msg.getMsgId();
        msgIds.add(msgId);
        MessageCacheItem item =
            new MessageCacheItem(msgTuple.getBatchId(), msg, msgTuple.getMessageStat());
        msgCache.put(msgId, item);
        msgQueue.offer(item);
      }
      batchMsgsMap.put(msgTuple.getBatchId(), partTag);
    }
  }
  void queryByKey(final DefaultMQAdminExt admin, final String topic, final String key)
      throws MQClientException, InterruptedException {
    admin.start();

    QueryResult queryResult = admin.queryMessage(topic, key, 32, 0, Long.MAX_VALUE);
    System.out.printf(
        "%-50s %-4s  %s\n", //
        "#Message ID", //
        "#QID", //
        "#Offset");
    for (MessageExt msg : queryResult.getMessageList()) {
      System.out.printf("%-50s %-4d %d\n", msg.getMsgId(), msg.getQueueId(), msg.getQueueOffset());
    }
  }
Пример #3
0
    public void executeOnTimeup() {
      ConsumeQueue cq =
          ScheduleMessageService.this.defaultMessageStore.findConsumeQueue(
              SCHEDULE_TOPIC, delayLevel2QueueId(delayLevel));
      if (cq != null) {
        SelectMapedBufferResult bufferCQ = cq.getIndexBuffer(this.offset);
        if (bufferCQ != null) {
          try {
            long nextOffset = offset;
            int i = 0;
            for (; i < bufferCQ.getSize(); i += ConsumeQueue.CQStoreUnitSize) {
              long offsetPy = bufferCQ.getByteBuffer().getLong();
              int sizePy = bufferCQ.getByteBuffer().getInt();
              long tagsCode = bufferCQ.getByteBuffer().getLong();

              // 队列里存储的tagsCode实际是一个时间点
              long deliverTimestamp = tagsCode;

              nextOffset = offset + (i / ConsumeQueue.CQStoreUnitSize);

              long countdown = deliverTimestamp - System.currentTimeMillis();
              // 时间到了,该投递
              if (countdown <= 0) {
                MessageExt msgExt =
                    ScheduleMessageService.this.defaultMessageStore.lookMessageByOffset(
                        offsetPy, sizePy);
                if (msgExt != null) {
                  MessageExtBrokerInner msgInner = this.messageTimeup(msgExt);
                  PutMessageResult putMessageResult =
                      ScheduleMessageService.this.defaultMessageStore.putMessage(msgInner);
                  // 成功
                  if (putMessageResult != null
                      && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) {
                    continue;
                  }
                  // 失败
                  else {
                    log.error(
                        "a message time up, but reput it failed, topic: {} msgId {}",
                        msgExt.getTopic(),
                        msgExt.getMsgId());
                    ScheduleMessageService.this.timer.schedule(
                        new DeliverDelayedMessageTimerTask(this.delayLevel, nextOffset),
                        DELAY_FOR_A_PERIOD);
                    ScheduleMessageService.this.updateOffset(this.delayLevel, nextOffset);
                    return;
                  }
                }
              }
              // 时候未到,继续定时
              else {
                ScheduleMessageService.this.timer.schedule(
                    new DeliverDelayedMessageTimerTask(this.delayLevel, nextOffset), countdown);
                ScheduleMessageService.this.updateOffset(this.delayLevel, nextOffset);
                return;
              }
            } // end of for

            nextOffset = offset + (i / ConsumeQueue.CQStoreUnitSize);
            ScheduleMessageService.this.timer.schedule(
                new DeliverDelayedMessageTimerTask(this.delayLevel, nextOffset), DELAY_FOR_A_WHILE);
            ScheduleMessageService.this.updateOffset(this.delayLevel, nextOffset);
            return;
          } finally {
            // 必须释放资源
            bufferCQ.release();
          }
        } // end of if (bufferCQ != null)
      } // end of if (cq != null)

      ScheduleMessageService.this.timer.schedule(
          new DeliverDelayedMessageTimerTask(this.delayLevel, this.offset), DELAY_FOR_A_WHILE);
    }