Esempio n. 1
0
  /** 客户端使用,SLAVE也会使用 */
  public static MessageExt decode(java.nio.ByteBuffer byteBuffer, final boolean readBody) {
    try {
      MessageExt msgExt = new MessageExt();

      // 1 TOTALSIZE
      int storeSize = byteBuffer.getInt();
      msgExt.setStoreSize(storeSize);

      // 2 MAGICCODE
      byteBuffer.getInt();

      // 3 BODYCRC
      int bodyCRC = byteBuffer.getInt();
      msgExt.setBodyCRC(bodyCRC);

      // 4 QUEUEID
      int queueId = byteBuffer.getInt();
      msgExt.setQueueId(queueId);

      // 5 FLAG
      int flag = byteBuffer.getInt();
      msgExt.setFlag(flag);

      // 6 QUEUEOFFSET
      long queueOffset = byteBuffer.getLong();
      msgExt.setQueueOffset(queueOffset);

      // 7 PHYSICALOFFSET
      long physicOffset = byteBuffer.getLong();
      msgExt.setCommitLogOffset(physicOffset);

      // 8 SYSFLAG
      int sysFlag = byteBuffer.getInt();
      msgExt.setSysFlag(sysFlag);

      // 9 BORNTIMESTAMP
      long bornTimeStamp = byteBuffer.getLong();
      msgExt.setBornTimestamp(bornTimeStamp);

      // 10 BORNHOST
      byte[] bornHost = new byte[4];
      byteBuffer.get(bornHost, 0, 4);
      int port = byteBuffer.getInt();
      msgExt.setBornHost(new InetSocketAddress(InetAddress.getByAddress(bornHost), port));

      // 11 STORETIMESTAMP
      long storeTimestamp = byteBuffer.getLong();
      msgExt.setStoreTimestamp(storeTimestamp);

      // 12 STOREHOST
      byte[] storeHost = new byte[4];
      byteBuffer.get(storeHost, 0, 4);
      port = byteBuffer.getInt();
      msgExt.setStoreHost(new InetSocketAddress(InetAddress.getByAddress(storeHost), port));

      // 13 RECONSUMETIMES
      int reconsumeTimes = byteBuffer.getInt();
      msgExt.setReconsumeTimes(reconsumeTimes);

      // 14 Prepared Transaction Offset
      long preparedTransactionOffset = byteBuffer.getLong();
      msgExt.setPreparedTransactionOffset(preparedTransactionOffset);

      // 15 BODY
      int bodyLen = byteBuffer.getInt();
      if (bodyLen > 0) {
        if (readBody) {
          byte[] body = new byte[bodyLen];
          byteBuffer.get(body);

          // uncompress body
          if ((sysFlag & MessageSysFlag.CompressedFlag) == MessageSysFlag.CompressedFlag) {
            body = UtilAll.uncompress(body);
          }

          msgExt.setBody(body);
        } else {
          byteBuffer.position(byteBuffer.position() + bodyLen);
        }
      }

      // 16 TOPIC
      byte topicLen = byteBuffer.get();
      byte[] topic = new byte[(int) topicLen];
      byteBuffer.get(topic);
      msgExt.setTopic(new String(topic));

      // 17 properties
      short propertiesLength = byteBuffer.getShort();
      if (propertiesLength > 0) {
        byte[] properties = new byte[propertiesLength];
        byteBuffer.get(properties);
        String propertiesString = new String(properties);
        Map<String, String> map = string2messageProperties(propertiesString);
        msgExt.setProperties(map);
      }

      // 消息ID
      ByteBuffer byteBufferMsgId = ByteBuffer.allocate(MSG_ID_LENGTH);
      String msgId =
          createMessageId(byteBufferMsgId, msgExt.getStoreHostBytes(), msgExt.getCommitLogOffset());
      msgExt.setMsgId(msgId);

      return msgExt;
    } catch (UnknownHostException e) {
      byteBuffer.position(byteBuffer.limit());
    } catch (BufferUnderflowException e) {
      byteBuffer.position(byteBuffer.limit());
    } catch (Exception e) {
      byteBuffer.position(byteBuffer.limit());
    }

    return null;
  }
  private ByteBuffer messageToByteBuffer(final MessageExt msg) throws IOException {
    int sysFlag = MessageSysFlag.clearCompressedFlag(msg.getSysFlag());
    if (msg.getBody() != null) {
      if (msg.getBody().length
          >= this.filtersrvController.getFiltersrvConfig().getCompressMsgBodyOverHowmuch()) {
        byte[] data =
            UtilAll.compress(
                msg.getBody(), this.filtersrvController.getFiltersrvConfig().getZipCompressLevel());
        if (data != null) {
          msg.setBody(data);
          sysFlag |= MessageSysFlag.CompressedFlag;
        }
      }
    }

    final int bodyLength = msg.getBody() != null ? msg.getBody().length : 0;
    byte[] topicData = msg.getTopic().getBytes(MixAll.DEFAULT_CHARSET);
    final int topicLength = topicData.length;
    String properties = MessageDecoder.messageProperties2String(msg.getProperties());
    byte[] propertiesData = properties.getBytes(MixAll.DEFAULT_CHARSET);
    final int propertiesLength = propertiesData.length;
    final int msgLen =
        4 // 1 TOTALSIZE
            + 4 // 2 MAGICCODE
            + 4 // 3 BODYCRC
            + 4 // 4 QUEUEID
            + 4 // 5 FLAG
            + 8 // 6 QUEUEOFFSET
            + 8 // 7 PHYSICALOFFSET
            + 4 // 8 SYSFLAG
            + 8 // 9 BORNTIMESTAMP
            + 8 // 10 BORNHOST
            + 8 // 11 STORETIMESTAMP
            + 8 // 12 STOREHOSTADDRESS
            + 4 // 13 RECONSUMETIMES
            + 8 // 14 Prepared Transaction Offset
            + 4
            + bodyLength // 14 BODY
            + 1
            + topicLength // 15 TOPIC
            + 2
            + propertiesLength // 16 propertiesLength
            + 0;

    ByteBuffer msgStoreItemMemory = ByteBuffer.allocate(msgLen);

    final MessageExt msgInner = msg;

    // 1 TOTALSIZE
    msgStoreItemMemory.putInt(msgLen);
    // 2 MAGICCODE
    msgStoreItemMemory.putInt(CommitLog.MessageMagicCode);
    // 3 BODYCRC
    msgStoreItemMemory.putInt(UtilAll.crc32(msgInner.getBody()));
    // 4 QUEUEID
    msgStoreItemMemory.putInt(msgInner.getQueueId());
    // 5 FLAG
    msgStoreItemMemory.putInt(msgInner.getFlag());
    // 6 QUEUEOFFSET
    msgStoreItemMemory.putLong(msgInner.getQueueOffset());
    // 7 PHYSICALOFFSET
    msgStoreItemMemory.putLong(msgInner.getCommitLogOffset());
    // 8 SYSFLAG
    msgStoreItemMemory.putInt(sysFlag);
    // 9 BORNTIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getBornTimestamp());
    // 10 BORNHOST
    msgStoreItemMemory.put(msgInner.getBornHostBytes());
    // 11 STORETIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getStoreTimestamp());
    // 12 STOREHOSTADDRESS
    msgStoreItemMemory.put(msgInner.getStoreHostBytes());
    // 13 RECONSUMETIMES
    msgStoreItemMemory.putInt(msgInner.getReconsumeTimes());
    // 14 Prepared Transaction Offset
    msgStoreItemMemory.putLong(msgInner.getPreparedTransactionOffset());
    // 15 BODY
    msgStoreItemMemory.putInt(bodyLength);
    if (bodyLength > 0) msgStoreItemMemory.put(msgInner.getBody());
    // 16 TOPIC
    msgStoreItemMemory.put((byte) topicLength);
    msgStoreItemMemory.put(topicData);
    // 17 PROPERTIES
    msgStoreItemMemory.putShort((short) propertiesLength);
    if (propertiesLength > 0) msgStoreItemMemory.put(propertiesData);

    return msgStoreItemMemory;
  }