org.apache.rocketmq.common.message.MessageDecoder Java Examples

The following examples show how to use org.apache.rocketmq.common.message.MessageDecoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PullMessageProcessor.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 从GetMessageResult获取要读取的消息
 * @param getMessageResult ;
 * @param group ;
 * @param topic ;
 * @param queueId 队列id
 * @return ;
 */
private byte[] readGetMessageResult(final GetMessageResult getMessageResult, final String group, final String topic,
    final int queueId) {

    final ByteBuffer byteBuffer = ByteBuffer.allocate(getMessageResult.getBufferTotalSize());

    long storeTimestamp = 0;
    try {
        List<ByteBuffer> messageBufferList = getMessageResult.getMessageBufferList();
        for (ByteBuffer bb : messageBufferList) {
            byteBuffer.put(bb);
            storeTimestamp = bb.getLong(MessageDecoder.MESSAGE_STORE_TIMESTAMP_POSTION);
        }
    } finally {
        //释放资源
        getMessageResult.release();
    }

    this.brokerController.getBrokerStatsManager().recordDiskFallBehindTime(group, topic, queueId, this.brokerController.getMessageStore().now() - storeTimestamp);
    return byteBuffer.array();
}
 
Example #2
Source File: EndTransactionProcessor.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private MessageExtBrokerInner endMessageTransaction(MessageExt msgExt) {
    MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
    msgInner.setTopic(msgExt.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC));
    msgInner.setQueueId(Integer.parseInt(msgExt.getUserProperty(MessageConst.PROPERTY_REAL_QUEUE_ID)));
    msgInner.setBody(msgExt.getBody());
    msgInner.setFlag(msgExt.getFlag());
    msgInner.setBornTimestamp(msgExt.getBornTimestamp());
    msgInner.setBornHost(msgExt.getBornHost());
    msgInner.setStoreHost(msgExt.getStoreHost());
    msgInner.setReconsumeTimes(msgExt.getReconsumeTimes());
    msgInner.setWaitStoreMsgOK(false);
    msgInner.setTransactionId(msgExt.getUserProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX));
    msgInner.setSysFlag(msgExt.getSysFlag());
    TopicFilterType topicFilterType =
        (msgInner.getSysFlag() & MessageSysFlag.MULTI_TAGS_FLAG) == MessageSysFlag.MULTI_TAGS_FLAG ? TopicFilterType.MULTI_TAG
            : TopicFilterType.SINGLE_TAG;
    long tagsCodeValue = MessageExtBrokerInner.tagsString2tagsCode(topicFilterType, msgInner.getTags());
    msgInner.setTagsCode(tagsCodeValue);
    MessageAccessor.setProperties(msgInner, msgExt.getProperties());
    msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgExt.getProperties()));
    MessageAccessor.clearProperty(msgInner, MessageConst.PROPERTY_REAL_TOPIC);
    MessageAccessor.clearProperty(msgInner, MessageConst.PROPERTY_REAL_QUEUE_ID);
    return msgInner;
}
 
Example #3
Source File: MessageStoreWithFilterTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
protected List<MessageExtBrokerInner> putMsg(DefaultMessageStore master, int topicCount,
                                             int msgCountPerTopic) throws Exception {
    List<MessageExtBrokerInner> msgs = new ArrayList<MessageExtBrokerInner>();
    for (int i = 0; i < topicCount; i++) {
        String realTopic = topic + i;
        for (int j = 0; j < msgCountPerTopic; j++) {
            MessageExtBrokerInner msg = buildMessage();
            msg.setTopic(realTopic);
            msg.putUserProperty("a", String.valueOf(j * 10 + 5));
            msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

            PutMessageResult result = master.putMessage(msg);

            msg.setMsgId(result.getAppendMessageResult().getMsgId());

            msgs.add(msg);
        }
    }

    return msgs;
}
 
Example #4
Source File: MessageStoreWithFilterTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public MessageExtBrokerInner buildMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic(topic);
    msg.setTags(System.currentTimeMillis() + "TAG");
    msg.setKeys("Hello");
    msg.setBody(msgBody);
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(queueId);
    msg.setSysFlag(0);
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setStoreHost(StoreHost);
    msg.setBornHost(BornHost);
    for (int i = 1; i < 3; i++) {
        msg.putUserProperty(String.valueOf(i), "imagoodperson" + i);
    }
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

    return msg;
}
 
Example #5
Source File: MessageStoreWithFilterTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
protected List<MessageExtBrokerInner> putMsg(DefaultMessageStore master, int topicCount,
                                             int msgCountPerTopic) throws Exception {
    List<MessageExtBrokerInner> msgs = new ArrayList<MessageExtBrokerInner>();
    for (int i = 0; i < topicCount; i++) {
        String realTopic = topic + i;
        for (int j = 0; j < msgCountPerTopic; j++) {
            MessageExtBrokerInner msg = buildMessage();
            msg.setTopic(realTopic);
            msg.putUserProperty("a", String.valueOf(j * 10 + 5));
            msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

            PutMessageResult result = master.putMessage(msg);

            msg.setMsgId(result.getAppendMessageResult().getMsgId());

            msgs.add(msg);
        }
    }

    return msgs;
}
 
Example #6
Source File: MessageStoreWithFilterTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
protected List<MessageExtBrokerInner> putMsg(DefaultMessageStore master, int topicCount,
                                             int msgCountPerTopic) throws Exception {
    List<MessageExtBrokerInner> msgs = new ArrayList<MessageExtBrokerInner>();
    for (int i = 0; i < topicCount; i++) {
        String realTopic = topic + i;
        for (int j = 0; j < msgCountPerTopic; j++) {
            MessageExtBrokerInner msg = buildMessage();
            msg.setTopic(realTopic);
            msg.putUserProperty("a", String.valueOf(j * 10 + 5));
            msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

            PutMessageResult result = master.putMessage(msg);

            msg.setMsgId(result.getAppendMessageResult().getMsgId());

            msgs.add(msg);
        }
    }

    return msgs;
}
 
Example #7
Source File: PullMessageProcessor.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private void generateOffsetMovedEvent(final OffsetMovedEvent event) {
    try {
        MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
        msgInner.setTopic(TopicValidator.RMQ_SYS_OFFSET_MOVED_EVENT);
        msgInner.setTags(event.getConsumerGroup());
        msgInner.setDelayTimeLevel(0);
        msgInner.setKeys(event.getConsumerGroup());
        msgInner.setBody(event.encode());
        msgInner.setFlag(0);
        msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgInner.getProperties()));
        msgInner.setTagsCode(MessageExtBrokerInner.tagsString2tagsCode(TopicFilterType.SINGLE_TAG, msgInner.getTags()));

        msgInner.setQueueId(0);
        msgInner.setSysFlag(0);
        msgInner.setBornTimestamp(System.currentTimeMillis());
        msgInner.setBornHost(RemotingUtil.string2SocketAddress(this.brokerController.getBrokerAddr()));
        msgInner.setStoreHost(msgInner.getBornHost());

        msgInner.setReconsumeTimes(0);

        PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessage(msgInner);
    } catch (Exception e) {
        log.warn(String.format("generateOffsetMovedEvent Exception, %s", event.toString()), e);
    }
}
 
Example #8
Source File: ClientRemotingProcessor.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ConsumeMessageDirectlyResultRequestHeader requestHeader =
        (ConsumeMessageDirectlyResultRequestHeader) request
            .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);

    final MessageExt msg = MessageDecoder.decode(ByteBuffer.wrap(request.getBody()));

    ConsumeMessageDirectlyResult result =
        this.mqClientFactory.consumeMessageDirectly(msg, requestHeader.getConsumerGroup(), requestHeader.getBrokerName());

    if (null != result) {
        response.setCode(ResponseCode.SUCCESS);
        response.setBody(result.encode());
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer Group <%s> not exist in this consumer", requestHeader.getConsumerGroup()));
    }

    return response;
}
 
Example #9
Source File: TransactionalMessageBridge.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 获取消息列表
 * @param getMessageResult getMessageResult
 * @return ;
 */
private List<MessageExt> decodeMsgList(GetMessageResult getMessageResult) {
    List<MessageExt> foundList = new ArrayList<>();
    try {
        List<ByteBuffer> messageBufferList = getMessageResult.getMessageBufferList();
        for (ByteBuffer bb : messageBufferList) {
            MessageExt msgExt = MessageDecoder.decode(bb);
            foundList.add(msgExt);
        }

    } finally {
        getMessageResult.release();
    }

    return foundList;
}
 
Example #10
Source File: ConsumeQueueTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public MessageExtBrokerInner buildMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic(topic);
    msg.setTags("TAG1");
    msg.setKeys("Hello");
    msg.setBody(msgBody);
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(queueId);
    msg.setSysFlag(0);
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setStoreHost(StoreHost);
    msg.setBornHost(BornHost);
    for (int i = 0; i < 1; i++) {
        msg.putUserProperty(String.valueOf(i), "imagoodperson" + i);
    }
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

    return msg;
}
 
Example #11
Source File: PullMessageProcessor.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void generateOffsetMovedEvent(final OffsetMovedEvent event) {
    try {
        MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
        msgInner.setTopic(MixAll.OFFSET_MOVED_EVENT);
        msgInner.setTags(event.getConsumerGroup());
        msgInner.setDelayTimeLevel(0);
        msgInner.setKeys(event.getConsumerGroup());
        msgInner.setBody(event.encode());
        msgInner.setFlag(0);
        msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgInner.getProperties()));
        msgInner.setTagsCode(MessageExtBrokerInner.tagsString2tagsCode(TopicFilterType.SINGLE_TAG, msgInner.getTags()));

        msgInner.setQueueId(0);
        msgInner.setSysFlag(0);
        msgInner.setBornTimestamp(System.currentTimeMillis());
        msgInner.setBornHost(RemotingUtil.string2SocketAddress(this.brokerController.getBrokerAddr()));
        msgInner.setStoreHost(msgInner.getBornHost());

        msgInner.setReconsumeTimes(0);

        PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessage(msgInner);
    } catch (Exception e) {
        log.warn(String.format("generateOffsetMovedEvent Exception, %s", event.toString()), e);
    }
}
 
Example #12
Source File: ConsumeQueueTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
public MessageExtBrokerInner buildMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic(topic);
    msg.setTags("TAG1");
    msg.setKeys("Hello");
    msg.setBody(msgBody);
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(queueId);
    msg.setSysFlag(0);
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setStoreHost(StoreHost);
    msg.setBornHost(BornHost);
    for (int i = 0; i < 1; i++) {
        msg.putUserProperty(String.valueOf(i), "imagoodperson" + i);
    }
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

    return msg;
}
 
Example #13
Source File: PullMessageProcessor.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
private byte[] readGetMessageResult(final GetMessageResult getMessageResult, final String group, final String topic, final int queueId) {
    final ByteBuffer byteBuffer = ByteBuffer.allocate(getMessageResult.getBufferTotalSize());

    long storeTimestamp = 0;
    try {
        List<ByteBuffer> messageBufferList = getMessageResult.getMessageBufferList();
        for (ByteBuffer bb : messageBufferList) {

            byteBuffer.put(bb);
            storeTimestamp = bb.getLong(MessageDecoder.MESSAGE_STORE_TIMESTAMP_POSTION);
        }
    } finally {
        getMessageResult.release();
    }

    this.brokerController.getBrokerStatsManager().recordDiskFallBehindTime(group, topic, queueId, this.brokerController.getMessageStore().now() - storeTimestamp);
    return byteBuffer.array();
}
 
Example #14
Source File: ConsumeQueueTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public MessageExtBrokerInner buildIPv6HostMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic(topic);
    msg.setTags("TAG1");
    msg.setKeys("Hello");
    msg.setBody(msgBody);
    msg.setMsgId("24084004018081003FAA1DDE2B3F898A00002A9F0000000000000CA0");
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(queueId);
    msg.setSysFlag(0);
    msg.setBornHostV6Flag();
    msg.setStoreHostAddressV6Flag();
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setBornHost(new InetSocketAddress("1050:0000:0000:0000:0005:0600:300c:326b", 123));
    msg.setStoreHost(new InetSocketAddress("::1", 124));
    for (int i = 0; i < 1; i++) {
        msg.putUserProperty(String.valueOf(i), "imagoodperson" + i);
    }
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

    return msg;
}
 
Example #15
Source File: ClientRemotingProcessor.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ConsumeMessageDirectlyResultRequestHeader requestHeader =
        (ConsumeMessageDirectlyResultRequestHeader) request
            .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);

    final MessageExt msg = MessageDecoder.decode(ByteBuffer.wrap(request.getBody()));

    ConsumeMessageDirectlyResult result =
        this.mqClientFactory.consumeMessageDirectly(msg, requestHeader.getConsumerGroup(), requestHeader.getBrokerName());

    if (null != result) {
        response.setCode(ResponseCode.SUCCESS);
        response.setBody(result.encode());
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer Group <%s> not exist in this consumer", requestHeader.getConsumerGroup()));
    }

    return response;
}
 
Example #16
Source File: ClientRemotingProcessor.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final ConsumeMessageDirectlyResultRequestHeader requestHeader =
        (ConsumeMessageDirectlyResultRequestHeader) request
            .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class);

    final MessageExt msg = MessageDecoder.decode(ByteBuffer.wrap(request.getBody()));

    ConsumeMessageDirectlyResult result =
        this.mqClientFactory.consumeMessageDirectly(msg, requestHeader.getConsumerGroup(), requestHeader.getBrokerName());

    if (null != result) {
        response.setCode(ResponseCode.SUCCESS);
        response.setBody(result.encode());
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer Group <%s> not exist in this consumer", requestHeader.getConsumerGroup()));
    }

    return response;
}
 
Example #17
Source File: ConsumeQueueTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public MessageExtBrokerInner buildMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic(topic);
    msg.setTags("TAG1");
    msg.setKeys("Hello");
    msg.setBody(msgBody);
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(queueId);
    msg.setSysFlag(0);
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setStoreHost(StoreHost);
    msg.setBornHost(BornHost);
    for (int i = 0; i < 1; i++) {
        msg.putUserProperty(String.valueOf(i), "imagoodperson" + i);
    }
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

    return msg;
}
 
Example #18
Source File: MessageStoreWithFilterTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public MessageExtBrokerInner buildMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic(topic);
    msg.setTags(System.currentTimeMillis() + "TAG");
    msg.setKeys("Hello");
    msg.setBody(msgBody);
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(queueId);
    msg.setSysFlag(0);
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setStoreHost(StoreHost);
    msg.setBornHost(BornHost);
    for (int i = 1; i < 3; i++) {
        msg.putUserProperty(String.valueOf(i), "imagoodperson" + i);
    }
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));

    return msg;
}
 
Example #19
Source File: ClientRemotingProcessor.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public RemotingCommand checkTransactionState(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader =
        (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }

    return null;
}
 
Example #20
Source File: SendMessageProcessorTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private RemotingCommand createSendTransactionMsgCommand(int requestCode) {
    SendMessageRequestHeader header = createSendMsgRequestHeader();
    int sysFlag = header.getSysFlag();
    Map<String, String> oriProps = MessageDecoder.string2messageProperties(header.getProperties());
    oriProps.put(MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    header.setProperties(MessageDecoder.messageProperties2String(oriProps));
    sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
    header.setSysFlag(sysFlag);
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, header);
    request.setBody(new byte[] {'a'});
    request.makeCustomHeaderToNet();
    return request;
}
 
Example #21
Source File: Detector.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private SendMessageRequestHeader getMessageRequestHeader(Message msg, MessageQueue mq) {
    SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
    requestHeader.setProducerGroup(producer.getProducerGroup());
    requestHeader.setTopic(msg.getTopic());
    requestHeader.setDefaultTopic(producer.getCreateTopicKey());
    requestHeader.setDefaultTopicQueueNums(producer.getDefaultTopicQueueNums());
    requestHeader.setQueueId(mq.getQueueId());
    requestHeader.setSysFlag(0);
    requestHeader.setBornTimestamp(System.currentTimeMillis());
    requestHeader.setFlag(msg.getFlag());
    requestHeader.setProperties(MessageDecoder.messageProperties2String(msg.getProperties()));
    requestHeader.setReconsumeTimes(0);
    requestHeader.setUnitMode(producer.isUnitMode());
    return requestHeader;
}
 
Example #22
Source File: DefaultMQPullConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public MessageExt viewMessage(String topic,
    String uniqKey) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        MessageDecoder.decodeMessageId(uniqKey);
        return this.viewMessage(uniqKey);
    } catch (Exception e) {
        // Ignore
    }
    return this.defaultMQPullConsumerImpl.queryMessageByUniqKey(topic, uniqKey);
}
 
Example #23
Source File: CommitLog.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private boolean isMappedFileMatchedRecover(final MappedFile mappedFile) {
    ByteBuffer byteBuffer = mappedFile.sliceByteBuffer();

    int magicCode = byteBuffer.getInt(MessageDecoder.MESSAGE_MAGIC_CODE_POSTION);
    if (magicCode != MESSAGE_MAGIC_CODE) {
        return false;
    }

    int sysFlag = byteBuffer.getInt(MessageDecoder.SYSFLAG_POSITION);
    int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20;
    int msgStoreTimePos = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength;
    long storeTimestamp = byteBuffer.getLong(msgStoreTimePos);
    if (0 == storeTimestamp) {
        return false;
    }

    if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable()
        && this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) {
        if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestampIndex()) {
            log.info("find check timestamp, {} {}",
                storeTimestamp,
                UtilAll.timeMillisToHumanString(storeTimestamp));
            return true;
        }
    } else {
        if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestamp()) {
            log.info("find check timestamp, {} {}",
                storeTimestamp,
                UtilAll.timeMillisToHumanString(storeTimestamp));
            return true;
        }
    }

    return false;
}
 
Example #24
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void endTransaction(
    final SendResult sendResult,
    final LocalTransactionState localTransactionState,
    final Throwable localException) throws RemotingException, MQBrokerException, InterruptedException, UnknownHostException {
    final MessageId id;
    if (sendResult.getOffsetMsgId() != null) {
        id = MessageDecoder.decodeMessageId(sendResult.getOffsetMsgId());
    } else {
        id = MessageDecoder.decodeMessageId(sendResult.getMsgId());
    }
    String transactionId = sendResult.getTransactionId();
    final String brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(sendResult.getMessageQueue().getBrokerName());
    EndTransactionRequestHeader requestHeader = new EndTransactionRequestHeader();
    requestHeader.setTransactionId(transactionId);
    requestHeader.setCommitLogOffset(id.getOffset());
    switch (localTransactionState) {
        case COMMIT_MESSAGE:
            requestHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_COMMIT_TYPE);
            break;
        case ROLLBACK_MESSAGE:
            requestHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_ROLLBACK_TYPE);
            break;
        case UNKNOW:
            requestHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_NOT_TYPE);
            break;
        default:
            break;
    }

    requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
    requestHeader.setTranStateTableOffset(sendResult.getQueueOffset());
    requestHeader.setMsgId(sendResult.getMsgId());
    String remark = localException != null ? ("executeLocalTransactionBranch exception: " + localException.toString()) : null;
    this.mQClientFactory.getMQClientAPIImpl().endTransactionOneway(brokerAddr, requestHeader, remark,
        this.defaultMQProducer.getSendMsgTimeout());
}
 
Example #25
Source File: DefaultMQPushConsumer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Override
public MessageExt viewMessage(String topic,
    String msgId) throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        MessageDecoder.decodeMessageId(msgId);
        return this.viewMessage(msgId);
    } catch (Exception e) {
        // Ignore
    }
    return this.defaultMQPushConsumerImpl.queryMessageByUniqKey(topic, msgId);
}
 
Example #26
Source File: ClientRemotingProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public RemotingCommand checkTransactionState(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader =
        (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        if (StringUtils.isNotEmpty(this.mqClientFactory.getClientConfig().getNamespace())) {
            messageExt.setTopic(NamespaceUtil
                .withoutNamespace(messageExt.getTopic(), this.mqClientFactory.getClientConfig().getNamespace()));
        }
        String transactionId = messageExt.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
        if (null != transactionId && !"".equals(transactionId)) {
            messageExt.setTransactionId(transactionId);
        }
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }

    return null;
}
 
Example #27
Source File: DefaultLitePullConsumerTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResultExt createPullResult(PullMessageRequestHeader requestHeader, PullStatus pullStatus,
    List<MessageExt> messageExtList) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (MessageExt messageExt : messageExtList) {
        outputStream.write(MessageDecoder.encode(messageExt, false));
    }
    return new PullResultExt(pullStatus, requestHeader.getQueueOffset() + messageExtList.size(), 123, 2048, messageExtList, 0, outputStream.toByteArray());
}
 
Example #28
Source File: DefaultMessageStore.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public MessageExt lookMessageByOffset(long commitLogOffset, int size) {
    SelectMappedBufferResult sbr = this.commitLog.getMessage(commitLogOffset, size);
    if (null != sbr) {
        try {
            return MessageDecoder.decode(sbr.getByteBuffer(), true, false);
        } finally {
            sbr.release();
        }
    }

    return null;
}
 
Example #29
Source File: HATest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private MessageExtBrokerInner buildMessage() {
    MessageExtBrokerInner msg = new MessageExtBrokerInner();
    msg.setTopic("FooBar");
    msg.setTags("TAG1");
    msg.setBody(MessageBody);
    msg.setKeys(String.valueOf(System.currentTimeMillis()));
    msg.setQueueId(Math.abs(QueueId.getAndIncrement()) % QUEUE_TOTAL);
    msg.setSysFlag(0);
    msg.setBornTimestamp(System.currentTimeMillis());
    msg.setStoreHost(StoreHost);
    msg.setBornHost(BornHost);
    msg.setPropertiesString(MessageDecoder.messageProperties2String(msg.getProperties()));
    return msg;
}
 
Example #30
Source File: CommitLog.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
/**
 * According to receive certain message or offset storage time if an error occurs, it returns -1
 */
public long pickupStoreTimestamp(final long offset, final int size) {
    if (offset >= this.getMinOffset()) {
        SelectMappedBufferResult result = this.getMessage(offset, size);
        if (null != result) {
            try {
                return result.getByteBuffer().getLong(MessageDecoder.MESSAGE_STORE_TIMESTAMP_POSTION);
            } finally {
                result.release();
            }
        }
    }

    return -1;
}