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

The following examples show how to use org.apache.rocketmq.common.message.MessageConst. 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: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void request(final Message msg, final MessageQueue mq, final RequestCallback requestCallback, long timeout)
    throws RemotingException, InterruptedException, MQClientException, MQBrokerException {
    long beginTimestamp = System.currentTimeMillis();
    prepareSendRequest(msg, timeout);
    final String correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);

    final RequestResponseFuture requestResponseFuture = new RequestResponseFuture(correlationId, timeout, requestCallback);
    RequestFutureTable.getRequestFutureTable().put(correlationId, requestResponseFuture);

    long cost = System.currentTimeMillis() - beginTimestamp;
    this.sendKernelImpl(msg, mq, CommunicationMode.ASYNC, new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            requestResponseFuture.setSendRequestOk(true);
        }

        @Override
        public void onException(Throwable e) {
            requestResponseFuture.setCause(e);
            requestFail(correlationId);
        }
    }, null, timeout - cost);
}
 
Example #2
Source File: AbstractTransactionalMessageCheckListener.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void sendCheckMessage(MessageExt msgExt) throws Exception {
    CheckTransactionStateRequestHeader checkTransactionStateRequestHeader = new CheckTransactionStateRequestHeader();
    checkTransactionStateRequestHeader.setCommitLogOffset(msgExt.getCommitLogOffset());
    checkTransactionStateRequestHeader.setOffsetMsgId(msgExt.getMsgId());
    checkTransactionStateRequestHeader.setMsgId(msgExt.getUserProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX));
    checkTransactionStateRequestHeader.setTransactionId(checkTransactionStateRequestHeader.getMsgId());
    checkTransactionStateRequestHeader.setTranStateTableOffset(msgExt.getQueueOffset());
    msgExt.setTopic(msgExt.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC));
    msgExt.setQueueId(Integer.parseInt(msgExt.getUserProperty(MessageConst.PROPERTY_REAL_QUEUE_ID)));
    msgExt.setStoreSize(0);
    String groupId = msgExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
    Channel channel = brokerController.getProducerManager().getAvaliableChannel(groupId);
    if (channel != null) {
        brokerController.getBroker2Client().checkProducerTransactionState(groupId, channel, checkTransactionStateRequestHeader, msgExt);
    } else {
        LOGGER.warn("Check transaction failed, channel is null. groupId={}", groupId);
    }
}
 
Example #3
Source File: MQClientAPIImplTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #4
Source File: TransactionalMessageServiceImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * If return true, skip this msg
 *
 * @param removeMap Op message map to determine whether a half message was responded by producer.
 * @param doneOpOffset Op Message which has been checked.
 * @param msgExt Half message
 * @return Return true if put success, otherwise return false.
 */
private boolean checkPrepareQueueOffset(HashMap<Long, Long> removeMap, List<Long> doneOpOffset,
    MessageExt msgExt) {
    String prepareQueueOffsetStr = msgExt.getUserProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED_QUEUE_OFFSET);
    if (null == prepareQueueOffsetStr) {
        return putImmunityMsgBackToHalfQueue(msgExt);
    } else {
        long prepareQueueOffset = getLong(prepareQueueOffsetStr);
        if (-1 == prepareQueueOffset) {
            return false;
        } else {
            if (removeMap.containsKey(prepareQueueOffset)) {
                long tmpOpOffset = removeMap.remove(prepareQueueOffset);
                doneOpOffset.add(tmpOpOffset);
                return true;
            } else {
                return putImmunityMsgBackToHalfQueue(msgExt);
            }
        }
    }
}
 
Example #5
Source File: DefaultTransactionalMessageCheckListenerTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testResolveDiscardMsg() {
    MessageExt messageExt = new MessageExt();
    messageExt.setTopic(TopicValidator.RMQ_SYS_TRANS_HALF_TOPIC);
    messageExt.setQueueId(0);
    messageExt.setBody("test resolve discard msg".getBytes());
    messageExt.setStoreHost(new InetSocketAddress("127.0.0.1", 10911));
    messageExt.setBornHost(new InetSocketAddress("127.0.0.1", 54270));
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_TOPIC, "test_topic");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_PRODUCER_GROUP, "PID_TEST_DISCARD_MSG");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, "15");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_QUEUE_ID, "2");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TAGS, "test_discard_msg");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, "AC14157E4F1C18B4AAC27EB1A0F30000");
    listener.resolveDiscardMsg(messageExt);
}
 
Example #6
Source File: MQClientAPIImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #7
Source File: ClientRemotingProcessor.java    From rocketmq_trans_message 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 #8
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void request(final Message msg, final MessageQueueSelector selector, final Object arg,
    final RequestCallback requestCallback, final long timeout)
    throws RemotingException, InterruptedException, MQClientException, MQBrokerException {
    long beginTimestamp = System.currentTimeMillis();
    prepareSendRequest(msg, timeout);
    final String correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);

    final RequestResponseFuture requestResponseFuture = new RequestResponseFuture(correlationId, timeout, requestCallback);
    RequestFutureTable.getRequestFutureTable().put(correlationId, requestResponseFuture);

    long cost = System.currentTimeMillis() - beginTimestamp;
    this.sendSelectImpl(msg, selector, arg, CommunicationMode.ASYNC, new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            requestResponseFuture.setSendRequestOk(true);
        }

        @Override
        public void onException(Throwable e) {
            requestResponseFuture.setCause(e);
            requestFail(correlationId);
        }
    }, timeout - cost);

}
 
Example #9
Source File: ClientRemotingProcessor.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * 检查【本地事务】状态
 *
 * @param ctx     ctx
 * @param request 请求
 * @return 响应
 * @throws RemotingCommandException 当解析请求失败时
 */
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 #10
Source File: TransactionalMessageBridge.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 解析prepare消息
 * @param msgInner ;
 * @return ;
 */
private MessageExtBrokerInner parseHalfMessageInner(MessageExtBrokerInner msgInner) {
    //在属性里添加其真实的topic
    MessageAccessor.putProperty(msgInner, MessageConst.PROPERTY_REAL_TOPIC, msgInner.getTopic());
    //消息原来真实的队列id
    MessageAccessor.putProperty(msgInner, MessageConst.PROPERTY_REAL_QUEUE_ID,
        String.valueOf(msgInner.getQueueId()));

    //
    msgInner.setSysFlag(
        MessageSysFlag.resetTransactionValue(msgInner.getSysFlag(), MessageSysFlag.TRANSACTION_NOT_TYPE));
    //放入到了buildHalfTopic这个队列里
    msgInner.setTopic(TransactionalMessageUtil.buildHalfTopic());
    //队列id为0
    msgInner.setQueueId(0);
    //设置原来的属性
    msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgInner.getProperties()));
    return msgInner;
}
 
Example #11
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 #12
Source File: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #13
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 #14
Source File: CarreraMessage.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@JSONField(serialize = false)
public Map<String, String> getRmqProperties() {
    HashMap<String, String> propertyMap = new HashMap<>();
    if (StringUtils.isNotBlank(getKey()))
        propertyMap.put(MessageConst.PROPERTY_KEYS, getKey());
    if (StringUtils.isNotBlank(getTags()))
        propertyMap.put(MessageConst.PROPERTY_TAGS, getTags());
    propertyMap.put(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, createUniqID());

    Map<String, String> msgProperties = this.getMsgProperties();
    if (msgProperties != null) {
        propertyMap.putAll(msgProperties);
    }

    // PROPERTY_WAIT_STORE_MSG_OK 这个属性没用。
    //propertyMap.put(MessageConst.PROPERTY_WAIT_STORE_MSG_OK, Boolean.toString(true));
    return propertyMap;
}
 
Example #15
Source File: MessageUtil.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static Message createReplyMessage(final Message requestMessage, final byte[] body) throws MQClientException {
    if (requestMessage != null) {
        Message replyMessage = new Message();
        String cluster = requestMessage.getProperty(MessageConst.PROPERTY_CLUSTER);
        String replyTo = requestMessage.getProperty(MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT);
        String correlationId = requestMessage.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
        String ttl = requestMessage.getProperty(MessageConst.PROPERTY_MESSAGE_TTL);
        replyMessage.setBody(body);
        if (cluster != null) {
            String replyTopic = MixAll.getReplyTopic(cluster);
            replyMessage.setTopic(replyTopic);
            MessageAccessor.putProperty(replyMessage, MessageConst.PROPERTY_MESSAGE_TYPE, MixAll.REPLY_MESSAGE_FLAG);
            MessageAccessor.putProperty(replyMessage, MessageConst.PROPERTY_CORRELATION_ID, correlationId);
            MessageAccessor.putProperty(replyMessage, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT, replyTo);
            MessageAccessor.putProperty(replyMessage, MessageConst.PROPERTY_MESSAGE_TTL, ttl);

            return replyMessage;
        } else {
            throw new MQClientException(ClientErrorCode.CREATE_REPLY_MESSAGE_EXCEPTION, "create reply message fail, requestMessage error, property[" + MessageConst.PROPERTY_CLUSTER + "] is null.");
        }
    }
    throw new MQClientException(ClientErrorCode.CREATE_REPLY_MESSAGE_EXCEPTION, "create reply message fail, requestMessage cannot be null.");
}
 
Example #16
Source File: ConsumeMessageOrderlyService.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public boolean sendMessageBack(final MessageExt msg) {
    try {
        if (MixAll.MQTT_MODE) {
            log.error("send MessageBack in MQTT MODE is illegal, msg={}", msg);
            return false;
        }
        // max reconsume times exceeded then send to dead letter queue.
        Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPushConsumer.getConsumerGroup()), msg.getBody());
        String originMsgId = MessageAccessor.getOriginMessageId(msg);
        MessageAccessor.setOriginMessageId(newMsg, UtilAll.isBlank(originMsgId) ? msg.getMsgId() : originMsgId);
        newMsg.setFlag(msg.getFlag());
        MessageAccessor.setProperties(newMsg, msg.getProperties());
        MessageAccessor.putProperty(newMsg, MessageConst.PROPERTY_RETRY_TOPIC, msg.getTopic());
        MessageAccessor.setReconsumeTime(newMsg, String.valueOf(msg.getReconsumeTimes()));
        MessageAccessor.setMaxReconsumeTimes(newMsg, String.valueOf(getMaxReconsumeTimes()));
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());

        this.defaultMQPushConsumer.getDefaultMQPushConsumerImpl().getmQClientFactory().getDefaultMQProducer().send(newMsg);
        return true;
    } catch (Exception e) {
        log.error("sendMessageBack exception, group: " + this.consumerGroup + " msg: " + msg.toString(), e);
    }

    return false;
}
 
Example #17
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private void prepareSendRequest(final Message msg, long timeout) {
    String correlationId = CorrelationIdUtil.createCorrelationId();
    String requestClientId = this.getmQClientFactory().getClientId();
    MessageAccessor.putProperty(msg, MessageConst.PROPERTY_CORRELATION_ID, correlationId);
    MessageAccessor.putProperty(msg, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT, requestClientId);
    MessageAccessor.putProperty(msg, MessageConst.PROPERTY_MESSAGE_TTL, String.valueOf(timeout));

    boolean hasRouteData = this.getmQClientFactory().getTopicRouteTable().containsKey(msg.getTopic());
    if (!hasRouteData) {
        long beginTimestamp = System.currentTimeMillis();
        this.tryToFindTopicPublishInfo(msg.getTopic());
        this.getmQClientFactory().sendHeartbeatToAllBrokerWithLock();
        long cost = System.currentTimeMillis() - beginTimestamp;
        if (cost > 500) {
            log.warn("prepare send request for <{}> cost {} ms", msg.getTopic(), cost);
        }
    }
}
 
Example #18
Source File: MQClientAPIImplTest.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #19
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public boolean sendMessageBack(final MessageExt msg) {
    try {
        // max reconsume times exceeded then send to dead letter queue.
        Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPushConsumer.getConsumerGroup()), msg.getBody());
        String originMsgId = MessageAccessor.getOriginMessageId(msg);
        MessageAccessor.setOriginMessageId(newMsg, UtilAll.isBlank(originMsgId) ? msg.getMsgId() : originMsgId);
        newMsg.setFlag(msg.getFlag());
        MessageAccessor.setProperties(newMsg, msg.getProperties());
        MessageAccessor.putProperty(newMsg, MessageConst.PROPERTY_RETRY_TOPIC, msg.getTopic());
        MessageAccessor.setReconsumeTime(newMsg, String.valueOf(msg.getReconsumeTimes()));
        MessageAccessor.setMaxReconsumeTimes(newMsg, String.valueOf(getMaxReconsumeTimes()));
        MessageAccessor.clearProperty(newMsg, MessageConst.PROPERTY_TRANSACTION_PREPARED);
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());

        this.defaultMQPushConsumer.getDefaultMQPushConsumerImpl().getmQClientFactory().getDefaultMQProducer().send(newMsg);
        return true;
    } catch (Exception e) {
        log.error("sendMessageBack exception, group: " + this.consumerGroup + " msg: " + msg.toString(), e);
    }

    return false;
}
 
Example #20
Source File: TransactionalMessageServiceImplTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private PullResult createDiscardPullResult(String topic, long queueOffset, String body, int size) {
    PullResult result = createPullResult(topic, queueOffset, body, size);
    List<MessageExt> msgs = result.getMsgFoundList();
    for (MessageExt msg : msgs) {
        msg.putUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, "100000");
    }
    return result;
}
 
Example #21
Source File: DefaultTransactionalMessageCheckListenerTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private MessageExtBrokerInner createMessageExt() {
    MessageExtBrokerInner inner = new MessageExtBrokerInner();
    MessageAccessor.putProperty(inner,MessageConst.PROPERTY_REAL_QUEUE_ID,"1");
    MessageAccessor.putProperty(inner,MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX,"1234255");
    MessageAccessor.putProperty(inner,MessageConst.PROPERTY_REAL_TOPIC,"realTopic");
    inner.setTransactionId(inner.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX));
    inner.setBody("check".getBytes());
    inner.setMsgId("12344567890");
    inner.setQueueId(0);
    return inner;
}
 
Example #22
Source File: ProcessQueue.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public boolean putMessage(final List<MessageExt> msgs) {
    boolean dispatchToConsume = false;
    try {
        this.lockTreeMap.writeLock().lockInterruptibly();
        try {
            int validMsgCnt = 0;
            for (MessageExt msg : msgs) {
                MessageExt old = msgTreeMap.put(msg.getQueueOffset(), msg);
                if (null == old) {
                    validMsgCnt++;
                    this.queueOffsetMax = msg.getQueueOffset();
                }
            }
            msgCount.addAndGet(validMsgCnt);

            if (!msgTreeMap.isEmpty() && !this.consuming) {
                dispatchToConsume = true;
                this.consuming = true;
            }

            if (!msgs.isEmpty()) {
                MessageExt messageExt = msgs.get(msgs.size() - 1);
                String property = messageExt.getProperty(MessageConst.PROPERTY_MAX_OFFSET);
                if (property != null) {
                    long accTotal = Long.parseLong(property) - messageExt.getQueueOffset();
                    if (accTotal > 0) {
                        this.msgAccCnt = accTotal;
                    }
                }
            }
        } finally {
            this.lockTreeMap.writeLock().unlock();
        }
    } catch (InterruptedException e) {
        log.error("putMessage exception", e);
    }

    return dispatchToConsume;
}
 
Example #23
Source File: DefaultMQAdminExtImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(final String consumerGroup, final String clientId,
    final String topic,
    final String msgId) throws RemotingException, MQClientException, InterruptedException, MQBrokerException {
    MessageExt msg = this.viewMessage(topic, msgId);
    if (msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) == null) {
        return this.mqClientInstance.getMQClientAPIImpl().consumeMessageDirectly(RemotingUtil.socketAddress2String(msg.getStoreHost()),
            consumerGroup, clientId, msgId, timeoutMillis * 3);
    } else {
        MessageClientExt msgClient = (MessageClientExt) msg;
        return this.mqClientInstance.getMQClientAPIImpl().consumeMessageDirectly(RemotingUtil.socketAddress2String(msg.getStoreHost()),
            consumerGroup, clientId, msgClient.getOffsetMsgId(), timeoutMillis * 3);
    }
}
 
Example #24
Source File: TransactionalMessageBridge.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private MessageExtBrokerInner parseHalfMessageInner(MessageExtBrokerInner msgInner) {
    MessageAccessor.putProperty(msgInner, MessageConst.PROPERTY_REAL_TOPIC, msgInner.getTopic());
    MessageAccessor.putProperty(msgInner, MessageConst.PROPERTY_REAL_QUEUE_ID,
        String.valueOf(msgInner.getQueueId()));
    msgInner.setSysFlag(
        MessageSysFlag.resetTransactionValue(msgInner.getSysFlag(), MessageSysFlag.TRANSACTION_NOT_TYPE));
    msgInner.setTopic(TransactionalMessageUtil.buildHalfTopic());
    msgInner.setQueueId(0);
    msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgInner.getProperties()));
    return msgInner;
}
 
Example #25
Source File: ClientRemotingProcessor.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 回查事务状态
 * @param ctx ctx
 * @param request ;
 * @return ;
 * @throws RemotingCommandException ;
 */
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) {
        //事务id
        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 #26
Source File: EndTransactionProcessor.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 检查PrePare消息
 * @param msgExt ;
 * @param requestHeader ;
 * @return ;
 */
private RemotingCommand checkPrepareMessage(MessageExt msgExt, EndTransactionRequestHeader requestHeader) {

    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    if (msgExt != null) {
        //获取生产者group,和header里的不匹配返回错误
        final String pgroupRead = msgExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (!pgroupRead.equals(requestHeader.getProducerGroup())) {
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("The producer group wrong");
            return response;
        }
        //获取队列偏移量,和requestheader里的不匹配,返回错误
        if (msgExt.getQueueOffset() != requestHeader.getTranStateTableOffset()) {
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("The transaction state table offset wrong");
            return response;
        }
        //如果commitlog的偏移量不匹配,也返回错误
        if (msgExt.getCommitLogOffset() != requestHeader.getCommitLogOffset()) {
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setRemark("The commit log offset wrong");
            return response;
        }
    } else {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark("Find prepared transaction message failed");
        return response;
    }
    response.setCode(ResponseCode.SUCCESS);
    return response;
}
 
Example #27
Source File: DefaultMQPushConsumerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 将消息发回broker
 * @param msg ;
 * @param delayLevel ;
 * @param brokerName ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 * @throws MQClientException ;
 */
public void sendMessageBack(MessageExt msg, int delayLevel, final String brokerName)
    throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        String brokerAddr = (null != brokerName) ? this.mQClientFactory.findBrokerAddressInPublish(brokerName)
            : RemotingHelper.parseSocketAddressAddr(msg.getStoreHost());
        this.mQClientFactory.getMQClientAPIImpl().consumerSendMessageBack(brokerAddr, msg,
            this.defaultMQPushConsumer.getConsumerGroup(), delayLevel, 5000, getMaxReconsumeTimes());

    } catch (Exception e) {
        log.error("sendMessageBack Exception, " + this.defaultMQPushConsumer.getConsumerGroup(), e);

        Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPushConsumer.getConsumerGroup()), msg.getBody());

        String originMsgId = MessageAccessor.getOriginMessageId(msg);
        MessageAccessor.setOriginMessageId(newMsg, UtilAll.isBlank(originMsgId) ? msg.getMsgId() : originMsgId);

        newMsg.setFlag(msg.getFlag());
        MessageAccessor.setProperties(newMsg, msg.getProperties());
        MessageAccessor.putProperty(newMsg, MessageConst.PROPERTY_RETRY_TOPIC, msg.getTopic());
        MessageAccessor.setReconsumeTime(newMsg, String.valueOf(msg.getReconsumeTimes() + 1));
        MessageAccessor.setMaxReconsumeTimes(newMsg, String.valueOf(getMaxReconsumeTimes()));
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());

        this.mQClientFactory.getDefaultMQProducer().send(newMsg);
    }
}
 
Example #28
Source File: ScheduleMessageService.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private MessageExtBrokerInner messageTimeup(MessageExt msgExt) {
    MessageExtBrokerInner msgInner = new MessageExtBrokerInner();
    msgInner.setBody(msgExt.getBody());
    msgInner.setFlag(msgExt.getFlag());
    MessageAccessor.setProperties(msgInner, msgExt.getProperties());

    TopicFilterType topicFilterType = MessageExt.parseTopicFilterType(msgInner.getSysFlag());
    long tagsCodeValue =
        MessageExtBrokerInner.tagsString2tagsCode(topicFilterType, msgInner.getTags());
    msgInner.setTagsCode(tagsCodeValue);
    msgInner.setPropertiesString(MessageDecoder.messageProperties2String(msgExt.getProperties()));

    msgInner.setSysFlag(msgExt.getSysFlag());
    msgInner.setBornTimestamp(msgExt.getBornTimestamp());
    msgInner.setBornHost(msgExt.getBornHost());
    msgInner.setStoreHost(msgExt.getStoreHost());
    msgInner.setReconsumeTimes(msgExt.getReconsumeTimes());

    msgInner.setWaitStoreMsgOK(false);
    MessageAccessor.clearProperty(msgInner, MessageConst.PROPERTY_DELAY_TIME_LEVEL);

    msgInner.setTopic(msgInner.getProperty(MessageConst.PROPERTY_REAL_TOPIC));

    String queueIdStr = msgInner.getProperty(MessageConst.PROPERTY_REAL_QUEUE_ID);
    int queueId = Integer.parseInt(queueIdStr);
    msgInner.setQueueId(queueId);

    return msgInner;
}
 
Example #29
Source File: DefaultMQAdminExtImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(final String consumerGroup, final String clientId,
    final String topic,
    final String msgId) throws RemotingException, MQClientException, InterruptedException, MQBrokerException {
    MessageExt msg = this.viewMessage(topic, msgId);
    if (msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) == null) {
        return this.mqClientInstance.getMQClientAPIImpl().consumeMessageDirectly(RemotingUtil.socketAddress2String(msg.getStoreHost()),
            consumerGroup, clientId, msgId, timeoutMillis * 3);
    } else {
        MessageClientExt msgClient = (MessageClientExt) msg;
        return this.mqClientInstance.getMQClientAPIImpl().consumeMessageDirectly(RemotingUtil.socketAddress2String(msg.getStoreHost()),
            consumerGroup, clientId, msgClient.getOffsetMsgId(), timeoutMillis * 3);
    }
}
 
Example #30
Source File: EndTransactionProcessorTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private MessageExt createDefaultMessageExt() {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("12345678");
    messageExt.setQueueId(0);
    messageExt.setCommitLogOffset(123456789L);
    messageExt.setQueueOffset(1234);
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_QUEUE_ID, "0");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_PRODUCER_GROUP, "testTransactionGroup");
    return messageExt;
}