Пример #1
0
    private void postReceiveMessage(
        final FetchRequest request, final MessageIterator it, final Partition partition) {
      // 如果offset仍然没有前进,递增重试次数
      if (it.getOffset() == 0) {
        request.incrementRetriesAndGet();
      } else {
        request.resetRetries();
      }

      // 非自动ack模式
      if (!partition.isAutoAck()) {
        // 如果是回滚,则回滚offset,再次发起请求
        if (partition.isRollback()) {
          request.rollbackOffset();
          partition.reset();
          this.addRequst(request);
        }
        // 如果提交,则更新临时offset到存储
        else if (partition.isAcked()) {
          partition.reset();
          this.ackRequest(request, it, true);
        } else {
          // 都不是,递增临时offset
          this.ackRequest(request, it, false);
        }
      } else {
        // 自动ack模式
        this.ackRequest(request, it, true);
      }
    }
Пример #2
0
 /**
  * 返回是否需要跳过后续的处理
  *
  * @param request
  * @param it
  * @param listener
  * @param partition
  * @return
  */
 private boolean processReceiveMessage(
     final FetchRequest request,
     final MessageIterator it,
     final MessageListener listener,
     final Partition partition) {
   int count = 0;
   while (it.hasNext()) {
     final int prevOffset = it.getOffset();
     try {
       final Message msg = it.next();
       MessageAccessor.setPartition(msg, partition);
       if (((SimpleMessageConsumer) consumer).canConsumer(msg)) listener.recieveMessages(msg);
       if (partition.isAutoAck()) {
         count++;
       } else {
         // 提交或者回滚都必须跳出循环
         if (partition.isAcked()) {
           count++;
           break;
         } else if (partition.isRollback()) {
           break;
         } else {
           // 不是提交也不是回滚,仅递增计数
           count++;
         }
       }
     } catch (final InvalidMessageException e) {
       MetaStatLog.addStat(null, StatConstants.INVALID_MSG_STAT, request.getTopic());
       // 消息体非法,获取有效offset,重新发起查询
       this.getOffsetAddRequest(request, e);
       return true;
     } catch (final Throwable e) {
       // 将指针移到上一条消息
       it.setOffset(prevOffset);
       log.error(
           "MessageListener处理消息异常,topic="
               + request.getTopic()
               + ",partition="
               + request.getPartition(),
           e);
       // 跳出循环,处理消息异常,到此为止
       break;
     }
   }
   MetaStatLog.addStatValue2(null, StatConstants.GET_MSG_COUNT_STAT, request.getTopic(), count);
   return false;
 }