Java Code Examples for org.apache.rocketmq.common.message.MessageExt#getBody()

The following examples show how to use org.apache.rocketmq.common.message.MessageExt#getBody() . 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: RmqAdminServiceImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private Message queryMessageByOffset(String nameServer, String topic, String brokerName, Integer qid, long offset) throws Exception {
    DefaultMQPullConsumer mqPullConsumer = getMqPullConsumer(nameServer);

    MessageQueue mq = new MessageQueue();
    mq.setTopic(topic);
    mq.setBrokerName(brokerName);
    mq.setQueueId(qid);

    PullResult pullResult = mqPullConsumer.pull(mq, "*", offset, 1, 5000);
    if (pullResult == null || pullResult.getPullStatus() != PullStatus.FOUND) {
        throw new MqException(String.format("[RMQ] message not exsit, nsrv:%s, topic:%s, brokerName:%s, qid:%s, offset:%d", nameServer, topic, brokerName, qid, offset));
    }

    MessageExt messageExt = pullResult.getMsgFoundList().get(0);
    if (messageExt.getBody().length == 0) {
        return null;
    }

    String msg;
    if (CodecsUtils.isUtf8(messageExt.getBody())) {
        msg = new String(messageExt.getBody(), "UTF-8");
    } else {
        msg = java.util.Base64.getEncoder().encodeToString(messageExt.getBody());
    }
    return new Message(brokerName + "_" + qid, offset, msg, messageExt.getTags(), messageExt.getKeys(), messageExt.getStoreSize(), messageExt.getBornTimestamp());
}
 
Example 2
Source File: ConsumeMessageOrderlyService.java    From rocketmq-all-4.1.0-incubating 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()));
        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 3
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 4
Source File: AbstractMQConsumer.java    From rocketmq-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 反序列化解析消息
 *
 * @param message  消息体
 * @return 序列化结果
 */
protected T parseMessage(MessageExt message) {
    if (message == null || message.getBody() == null) {
        return null;
    }
    final Type type = this.getMessageType();
    if (type instanceof Class) {
        try {
            T data = gson.fromJson(new String(message.getBody()), type);
            return data;
        } catch (JsonSyntaxException e) {
            log.error("parse message json fail : {}", e.getMessage());
        }
    } else {
        log.warn("Parse msg error. {}", message);
    }
    return null;
}
 
Example 5
Source File: DefaultRocketMQListenerContainer.java    From spring-boot-starter-rocketmq with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Object doConvertMessage(MessageExt messageExt) {
    if (Objects.equals(messageType, MessageExt.class)) {
        return messageExt;
    } else {
        String str = new String(messageExt.getBody(), Charset.forName(charset));
        if (Objects.equals(messageType, String.class)) {
            return str;
        } else {
            // if msgType not string, use objectMapper change it.
            try {
                return objectMapper.readValue(str, messageType);
            } catch (Exception e) {
                log.info("convert failed. str:{}, msgType:{}", str, messageType);
                throw new RuntimeException("cannot convert message to " + messageType, e);
            }
        }
    }
}
 
Example 6
Source File: ChargeOrderTranListenerImpl.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
/**
 * 根据订单号进行回查
 * @param msg
 * @return
 */
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    String message = new String(msg.getBody());
    String msgId = msg.getMsgId();
    int reconsumeTimes = msg.getReconsumeTimes();
    LOGGER.info("订单入库本地事务回查--接收到消息, msgId={},message={},reconsumeTimes={}", msgId, message, reconsumeTimes);
    // 消息解码
    WalletPaymentProtocol walletPaymentProtocol = new WalletPaymentProtocol();
    walletPaymentProtocol.decode(message);
    String orderId = walletPaymentProtocol.getOrderId();
    // 订单查询
    OrderInfoDO orderInfoDO = new OrderInfoDO().setOrderId(orderId);
    OrderInfoDobj orderInfoDobj = orderChargeService.queryOrderInfo(orderInfoDO);
    if (orderInfoDobj == null) {
        LOGGER.info("订单入库本地事务回查--本地不存在订单,[消息回滚],orderId={},msgId={}", orderId, msgId);
        return LocalTransactionState.ROLLBACK_MESSAGE;
    }
    LOGGER.info("订单入库本地事务回查--本地存在订单信息,orderInfoDobj={},msgId={},[消息提交]", orderInfoDobj, msgId);
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 7
Source File: ClearUserMessageListener.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageList, ConsumeConcurrentlyContext context) {
    for (MessageExt messageExt : messageList) {
        String message = new String(messageExt.getBody());
        logger.info("收到清除用户消息 | {}", message);
        JSONObject messageJSON = JSON.parseObject(message);
        Long userId = messageJSON.getLong("userId");
        if (userId != null) {
            userLoginLogMapper.deleteAllByUserId(userId);
            logger.info("删除用户登录记录完成 | {}", userId);
        }
    }
    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
 
Example 8
Source File: DefaultMQPullConsumerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void sendMessageBack(MessageExt msg, int delayLevel, final String brokerName, String consumerGroup)
    throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        String brokerAddr = (null != brokerName) ? this.mQClientFactory.findBrokerAddressInPublish(brokerName)
            : RemotingHelper.parseSocketAddressAddr(msg.getStoreHost());

        if (UtilAll.isBlank(consumerGroup)) {
            consumerGroup = this.defaultMQPullConsumer.getConsumerGroup();
        }

        this.mQClientFactory.getMQClientAPIImpl().consumerSendMessageBack(brokerAddr, msg, consumerGroup, delayLevel, 3000,
            this.defaultMQPullConsumer.getMaxReconsumeTimes());
    } catch (Exception e) {
        log.error("sendMessageBack Exception, " + this.defaultMQPullConsumer.getConsumerGroup(), e);

        Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPullConsumer.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(this.defaultMQPullConsumer.getMaxReconsumeTimes()));
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());
        this.mQClientFactory.getDefaultMQProducer().send(newMsg);
    }
}
 
Example 9
Source File: RocketMQTemplate.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private Object doConvertMessage(MessageExt messageExt, Type type) {
    if (Objects.equals(type, MessageExt.class)) {
        return messageExt;
    } else if (Objects.equals(type, byte[].class)) {
        return messageExt.getBody();
    } else {
        String str = new String(messageExt.getBody(), Charset.forName(charset));
        if (Objects.equals(type, String.class)) {
            return str;
        } else {
            // If msgType not string, use objectMapper change it.
            try {
                if (type instanceof Class) {
                    //if the messageType has not Generic Parameter
                    return this.getMessageConverter().fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) type);
                } else {
                    //if the messageType has Generic Parameter, then use SmartMessageConverter#fromMessage with third parameter "conversionHint".
                    //we have validate the MessageConverter is SmartMessageConverter in this#getMethodParameter.
                    return ((SmartMessageConverter) this.getMessageConverter()).fromMessage(MessageBuilder.withPayload(str).build(), (Class<?>) ((ParameterizedType) type).getRawType(), null);
                }
            } catch (Exception e) {
                log.error("convert failed. str:{}, msgType:{}", str, type);
                throw new RuntimeException("cannot convert message to " + type, e);
            }
        }
    }
}
 
Example 10
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void sendMessageBack(MessageExt msg, int delayLevel, final String brokerName, String consumerGroup)
    throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        String brokerAddr = (null != brokerName) ? this.mQClientFactory.findBrokerAddressInPublish(brokerName)
            : RemotingHelper.parseSocketAddressAddr(msg.getStoreHost());

        if (UtilAll.isBlank(consumerGroup)) {
            consumerGroup = this.defaultMQPullConsumer.getConsumerGroup();
        }

        this.mQClientFactory.getMQClientAPIImpl().consumerSendMessageBack(brokerAddr, msg, consumerGroup, delayLevel, 3000,
            this.defaultMQPullConsumer.getMaxReconsumeTimes());
    } catch (Exception e) {
        log.error("sendMessageBack Exception, " + this.defaultMQPullConsumer.getConsumerGroup(), e);

        Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPullConsumer.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(this.defaultMQPullConsumer.getMaxReconsumeTimes()));
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());
        this.mQClientFactory.getDefaultMQProducer().send(newMsg);
    } finally {
        msg.setTopic(NamespaceUtil.withoutNamespace(msg.getTopic(), this.defaultMQPullConsumer.getNamespace()));
    }
}
 
Example 11
Source File: UacPushMessageListener.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Consume message consume concurrently status.
 *
 * @param messageExtList             the message ext list
 * @param consumeConcurrentlyContext the consume concurrently context
 *
 * @return the consume concurrently status
 */
@Override
@MqConsumerStore
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExtList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
	MessageExt msg = messageExtList.get(0);
	String body = new String(msg.getBody());
	String topicName = msg.getTopic();
	String tags = msg.getTags();
	String keys = msg.getKeys();

	try {
		MqMessage.checkMessage(body, topicName, tags, keys);
		String mqKV = redisService.getKey(keys);
		if (PublicUtil.isNotEmpty(mqKV)) {
			log.error("MQ消费Topic={},tag={},key={}, 重复消费", topicName, tags, keys);
			return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
		}
		if (AliyunMqTopicConstants.MqTopicEnum.TPC_TOPIC.getTopic().equals(topicName)) {
			mqMessageService.deleteMessageTopic(body, tags);
		} else {
			log.info("OPC订单信息消 topicName={} 不存在", topicName);
		}
	} catch (IllegalArgumentException ex) {
		log.error("校验MQ message 失败 ex={}", ex.getMessage(), ex);
	} catch (Exception e) {
		log.error("处理MQ message 失败 topicName={}, keys={}, ex={}", topicName, keys, e.getMessage(), e);
		return ConsumeConcurrentlyStatus.RECONSUME_LATER;
	}

	redisService.setKey(keys, keys, 10, TimeUnit.DAYS);
	return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
 
Example 12
Source File: NotifySendListenerImpl.java    From order-charge-notify with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {

    try {
        for (MessageExt msg : msgs) {
            // 消息解码
            String message = new String(msg.getBody());
            // 消费次数
            int reconsumeTimes = msg.getReconsumeTimes();
            String msgId = msg.getMsgId();
            String logSuffix = ",msgId=" + msgId + ",reconsumeTimes=" + reconsumeTimes;

            LOGGER.info("[通知发送消息消费者]-OrderNotifySendProducer-接收到消息,message={},{}", message, logSuffix);
            // 请求组装
            OrderResultNofityProtocol protocol = new OrderResultNofityProtocol();
            protocol.decode(message);
            // 参数加签,获取用户privatekey
            String privateKey = protocol.getPrivateKey();
            String notifyUrl = protocol.getMerchantNotifyUrl();
            String purseId = protocol.getPurseId();
            ChargeNotifyRequest chargeNotifyRequest = new ChargeNotifyRequest();
            chargeNotifyRequest.setChannel_orderid(protocol.getChannelOrderId())
                    .setFinish_time(DateUtil.formatDate(new Date(System.currentTimeMillis())))
                    .setOrder_status(NotifyConstant.NOTIFY_SUCCESS)
                    .setPlat_orderid(protocol.getOrderId())
                    .setSign(chargeNotifyRequest.sign(privateKey));
            LOGGER.info("[通知发送消息消费者]-OrderNotifySendProducer-订单结果通知入参:{},{}", chargeNotifyRequest.toString(), logSuffix);
            // 通知发送
            return sendNotifyByPost(reconsumeTimes, logSuffix, protocol, notifyUrl, purseId, chargeNotifyRequest);
        }
    } catch (Exception e) {
        LOGGER.error("[通知发送消息消费者]消费异常,e={}", LogExceptionWapper.getStackTrace(e));
    }
    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
 
Example 13
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
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()));
        MessageAccessor.clearProperty(newMsg, MessageConst.PROPERTY_TRANSACTION_PREPARED);
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());

        this.mQClientFactory.getDefaultMQProducer().send(newMsg);
    } finally {
        msg.setTopic(NamespaceUtil.withoutNamespace(msg.getTopic(), this.defaultMQPushConsumer.getNamespace()));
    }
}
 
Example 14
Source File: ClearUserTransactionListener.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    String message = new String(msg.getBody());
    logger.info("收到检查清除用户本地事务状态消息 | {}", message);
    JSONObject messageJSON = JSON.parseObject(message);
    Long userId = messageJSON.getLong("userId");

    User user = userMapper.getById(userId);
    return user == null ? LocalTransactionState.COMMIT_MESSAGE : LocalTransactionState.ROLLBACK_MESSAGE;
}
 
Example 15
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void sendMessageBack(MessageExt msg, int delayLevel, final String brokerName, String consumerGroup)
    throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    try {
        String brokerAddr = (null != brokerName) ? this.mQClientFactory.findBrokerAddressInPublish(brokerName)
            : RemotingHelper.parseSocketAddressAddr(msg.getStoreHost());

        if (UtilAll.isBlank(consumerGroup)) {
            consumerGroup = this.defaultMQPullConsumer.getConsumerGroup();
        }

        this.mQClientFactory.getMQClientAPIImpl().consumerSendMessageBack(brokerAddr, msg, consumerGroup, delayLevel, 3000,
            this.defaultMQPullConsumer.getMaxReconsumeTimes());
    } catch (Exception e) {
        log.error("sendMessageBack Exception, " + this.defaultMQPullConsumer.getConsumerGroup(), e);

        Message newMsg = new Message(MixAll.getRetryTopic(this.defaultMQPullConsumer.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(this.defaultMQPullConsumer.getMaxReconsumeTimes()));
        newMsg.setDelayTimeLevel(3 + msg.getReconsumeTimes());
        this.mQClientFactory.getDefaultMQProducer().send(newMsg);
    }
}
 
Example 16
Source File: WalletPaymentMsgListenerImpl.java    From order-charge-notify with Apache License 2.0 4 votes vote down vote up
/**
 * 钱包扣款,并插入扣款流水
 *
 * @param msg
 */
private ConsumeConcurrentlyStatus walletCharge(MessageExt msg, String msgId) {
    String message = new String(msg.getBody());
    LOGGER.info("msgId={},钱包扣款消费者接收到消息,message={}", msgId, message);
    WalletPaymentProtocol payProtocol = new WalletPaymentProtocol();
    payProtocol.decode(message);

    // 幂等消费逻辑: 根据订单号查询扣款流水,如果存在则直接返回消费成功
    String orderId = payProtocol.getOrderId();
    ChargeRecordEntity chargeRecordEntity = walletService.queryChargeRecordByOrderId(orderId);
    if (chargeRecordEntity != null) {
        LOGGER.info("[扣款本地事务回查]-本地已经存在orderId=[{}]对应的扣款流水,不需要重复消费,msgId={}", orderId, msgId);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    try {
        // 组装半消息:扣款成功修改订单状态为成功,消息事件=修改订单支付状态
        OrderStatusUpdateProtocol orderStatusUpdateProtocol = new OrderStatusUpdateProtocol();
        orderStatusUpdateProtocol.setTopicName(MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getTopic());
        orderStatusUpdateProtocol.setOrderId(payProtocol.getOrderId())
                .setChargeMoney(payProtocol.getChargeMoney())
                .setPurseId(payProtocol.getPurseId())
                .setMerchantName(payProtocol.getMerchantName())
                .setEventType(UpdateEventTypeConst.EVENT_UPDATE_PAY_STATUS.getEventType());

        Message updateOrderStatusMsg =
                new Message(MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getTopic(),
                        orderStatusUpdateProtocol.encode().getBytes());
        // 半消息发送
        TransactionSendResult transactionSendResult = orderStatusUpdateProducer.getProducer()
                /**
                 * sendMessageInTransaction(final Message msg,
                 * final Object arg) 第二个参数为回调参数,可以为null,
                 * 该参数和LocalTransactionState executeLocalTransaction(Message msg, Object arg)第二个参数为同一个值
                 */
                .sendMessageInTransaction(updateOrderStatusMsg, null);
        if (transactionSendResult == null) {
            // 发送未知重新消费
            LOGGER.info("msgId={},订单状态更新半消息发送状态未知,消费状态[RECONSUME_LATER],等待重新消费,orderId={}", msgId, payProtocol.getOrderId(), message);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
        if (transactionSendResult.getLocalTransactionState().equals(LocalTransactionState.COMMIT_MESSAGE)) {
            LOGGER.info("msgId={},订单状态更新半消息发送成功,消费状态[CONSUME_SUCCESS],orderId={},sendResult={}", msgId, payProtocol.getOrderId(), transactionSendResult);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        if (transactionSendResult.getLocalTransactionState().equals(LocalTransactionState.UNKNOW)) {
            LOGGER.warn("msgId={},订单状态更新本地事务执行状态未知,半消息发送未知,消费状态[RECONSUME_LATER],orderId={},sendResult={}", msgId, payProtocol.getOrderId(), transactionSendResult);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    } catch (Exception e) {
        LOGGER.error("msgId={},订单状态更新半消息发送异常,消费状态[RECONSUME_LATER],e={}", msgId, LogExceptionWapper.getStackTrace(e));
    }
    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
 
Example 17
Source File: DefaultRequestProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
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.COMPRESSED_FLAG;
            }
        }
    }

    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.MESSAGE_MAGIC_CODE);
    // 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;
}
 
Example 18
Source File: DefaultRequestProcessor.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
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.COMPRESSED_FLAG;
            }
        }
    }

    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.MESSAGE_MAGIC_CODE);
    // 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;
}
 
Example 19
Source File: DefaultRequestProcessor.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
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.COMPRESSED_FLAG;
            }
        }
    }

    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.MESSAGE_MAGIC_CODE);
    // 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;
}
 
Example 20
Source File: RocketQueue.java    From NetDiscovery with Apache License 2.0 3 votes vote down vote up
@Override
public Request poll(String spiderName) {

    Request request = null;

    MessageExt messageExt = consumer.getMessage(spiderName);

    if (messageExt!=null) {

        byte[] body = messageExt.getBody();
        request = deserialize(body);
    }

    return request;
}