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

The following examples show how to use org.apache.rocketmq.common.message.Message#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: DefaultMQProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private boolean tryToCompressMessage(final Message msg) {
    if (msg instanceof MessageBatch) {
        //batch dose not support compressing right now
        return false;
    }
    byte[] body = msg.getBody();
    if (body != null) {
        if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
            try {
                byte[] data = UtilAll.compress(body, zipCompressLevel);
                if (data != null) {
                    msg.setBody(data);
                    return true;
                }
            } catch (IOException e) {
                log.error("tryToCompressMessage exception", e);
                log.warn(msg.toString());
            }
        }
    }

    return false;
}
 
Example 2
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private boolean tryToCompressMessage(final Message msg) {
    if (msg instanceof MessageBatch) {
        //batch dose not support compressing right now
        return false;
    }
    byte[] body = msg.getBody();
    if (body != null) {
        if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
            try {
                byte[] data = UtilAll.compress(body, zipCompressLevel);
                if (data != null) {
                    msg.setBody(data);
                    return true;
                }
            } catch (IOException e) {
                log.error("tryToCompressMessage exception", e);
                log.warn(msg.toString());
            }
        }
    }

    return false;
}
 
Example 3
Source File: Validators.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
/**
 * Validate message
 *
 * @param msg
 * @param defaultMQProducer
 * @throws MQClientException
 */
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
    throws MQClientException {
    if (null == msg) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
    }
    // topic
    Validators.checkTopic(msg.getTopic());

    // body
    if (null == msg.getBody()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
    }

    if (0 == msg.getBody().length) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
    }

    /**
     * 消息内容不要超过4M
     */
    if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
            "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
    }
}
 
Example 4
Source File: DefaultMQProducerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private boolean tryToCompressMessage(final Message msg) {
    if (msg instanceof MessageBatch) {
        //batch dose not support compressing right now
        return false;
    }
    byte[] body = msg.getBody();
    if (body != null) {
        if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
            try {
                byte[] data = UtilAll.compress(body, zipCompressLevel);
                if (data != null) {
                    msg.setBody(data);
                    return true;
                }
            } catch (IOException e) {
                log.error("tryToCompressMessage exception", e);
                log.warn(msg.toString());
            }
        }
    }

    return false;
}
 
Example 5
Source File: ChargeOrderTranListenerImpl.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
/**
 * 执行本地订单入库操作
 * @param msg
 * @param arg
 * @return
 */
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
    // 消息解码
    String message = new String(msg.getBody());
    WalletPaymentProtocol walletPaymentProtocol = new WalletPaymentProtocol();
    walletPaymentProtocol.decode(message);
    LOGGER.info("订单入库实体WalletPaymentProtocol={}", walletPaymentProtocol.toString());
    // 组装下单实体
    OrderInfoDO orderInfoDO = new OrderInfoDO();
    BeanUtils.copyProperties(walletPaymentProtocol, orderInfoDO);
    String orderId = orderInfoDO.getOrderId();
    // 执行下单操作
    try {
        if (!orderChargeService.insertOrder(orderInfoDO)) {
            LOGGER.error("订单入库失败,事务消息回滚,LocalTransactionState={},orderId={}", LocalTransactionState.ROLLBACK_MESSAGE, orderId);
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    } catch (Exception e) {
        LOGGER.error("订单入库异常,等待回查发起,orderId={},e={}", orderId, LogExceptionWapper.getStackTrace(e));
        return LocalTransactionState.UNKNOW;
    }
    LOGGER.info("订单入库成功,orderId={}", orderId);
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 6
Source File: Validators.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
    throws MQClientException {
    if (null == msg) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
    }
    // topic
    Validators.checkTopic(msg.getTopic());
    Validators.isNotAllowedSendTopic(msg.getTopic());

    // body
    if (null == msg.getBody()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
    }

    if (0 == msg.getBody().length) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
    }

    if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
            "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
    }
}
 
Example 7
Source File: Validators.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
/**
 * Validate message
 *
 * @param msg
 * @param defaultMQProducer
 * @throws MQClientException
 */
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
    throws MQClientException {
    if (null == msg) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
    }
    // topic
    Validators.checkTopic(msg.getTopic());
    // body
    if (null == msg.getBody()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
    }

    if (0 == msg.getBody().length) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
    }

    if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
            "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
    }
}
 
Example 8
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private boolean tryToCompressMessage(final Message msg) {
    if (msg instanceof MessageBatch) {
        //batch dose not support compressing right now
        return false;
    }
    byte[] body = msg.getBody();
    if (body != null) {
        if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
            try {
                byte[] data = UtilAll.compress(body, zipCompressLevel);
                if (data != null) {
                    msg.setBody(data);
                    return true;
                }
            } catch (IOException e) {
                log.error("tryToCompressMessage exception", e);
                log.warn(msg.toString());
            }
        }
    }

    return false;
}
 
Example 9
Source File: Validators.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * Validate message
 *
 * @param msg 消息
 * @param defaultMQProducer producer
 * @throws MQClientException 当 msg 格式不正确
 */
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
    throws MQClientException {
    if (null == msg) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
    }
    // topic
    Validators.checkTopic(msg.getTopic());
    // body
    if (null == msg.getBody()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
    }

    if (0 == msg.getBody().length) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
    }

    if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
            "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
    }
}
 
Example 10
Source File: Validators.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
/**
 * Validate message
 */
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
    throws MQClientException {
    if (null == msg) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
    }
    // topic
    Validators.checkTopic(msg.getTopic());

    // body
    if (null == msg.getBody()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
    }

    if (0 == msg.getBody().length) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
    }

    if (defaultMQProducer != null && msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
            "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
    }
}
 
Example 11
Source File: SplitBatchProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override public List<Message> next() {
    int nextIndex = currIndex;
    int totalSize = 0;
    for (; nextIndex < messages.size(); nextIndex++) {
        Message message = messages.get(nextIndex);
        int tmpSize = message.getTopic().length() + message.getBody().length;
        Map<String, String> properties = message.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            tmpSize += entry.getKey().length() + entry.getValue().length();
        }
        tmpSize = tmpSize + 20; //for log overhead
        if (tmpSize > sizeLimit) {
            //it is unexpected that single message exceeds the sizeLimit
            //here just let it go, otherwise it will block the splitting process
            if (nextIndex - currIndex == 0) {
                //if the next sublist has no element, add this one and then break, otherwise just break
                nextIndex++;
            }
            break;
        }
        if (tmpSize + totalSize > sizeLimit) {
            break;
        } else {
            totalSize += tmpSize;
        }

    }
    List<Message> subList = messages.subList(currIndex, nextIndex);
    currIndex = nextIndex;
    return subList;
}
 
Example 12
Source File: ListSplitter.java    From spring-boot-starter-samples with Apache License 2.0 5 votes vote down vote up
@Override public List<Message> next() {
    int nextIndex = currIndex;
    int totalSize = 0;
    for (; nextIndex < messages.size(); nextIndex++) {
        Message message = messages.get(nextIndex);
        int tmpSize = message.getTopic().length() + message.getBody().length;
        Map<String, String> properties = message.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            tmpSize += entry.getKey().length() + entry.getValue().length();
        }
        tmpSize = tmpSize + 20; //for log overhead
        if (tmpSize > SIZE_LIMIT) {
            //it is unexpected that single message exceeds the SIZE_LIMIT
            //here just let it go, otherwise it will block the splitting process
            if (nextIndex - currIndex == 0) {
               //if the next sublist has no element, add this one and then break, otherwise just break
               nextIndex++;  
            }
            break;
        }
        if (tmpSize + totalSize > SIZE_LIMIT) {
            break;
        } else {
            totalSize += tmpSize;
        }

    }
    List<Message> subList = messages.subList(currIndex, nextIndex);
    currIndex = nextIndex;
    return subList;
}
 
Example 13
Source File: MqMessage.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new Mq message.
 *
 * @param message the message
 */
public MqMessage(Message message) {
	this.body = new String(message.getBody());
	this.topic = message.getTopic();
	this.key = message.getKeys();
	this.tag = message.getTags();

}
 
Example 14
Source File: MqMessage.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Check message message.
 *
 * @param message the message
 *
 * @return the message
 */
public static Message checkMessage(Message message) {

	String body = new String(message.getBody());
	String topic = message.getTopic();
	String key = message.getKeys();
	String tag = message.getTags();
	printCheckMessageLog(topic, key, body, tag);
	checkMessage(topic, key, body);
	return buildMessage(body, topic, tag, key);

}
 
Example 15
Source File: SplitBatchProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public List<Message> next() {
    int nextIndex = currIndex;
    int totalSize = 0;
    for (; nextIndex < messages.size(); nextIndex++) {
        Message message = messages.get(nextIndex);
        int tmpSize = message.getTopic().length() + message.getBody().length;
        Map<String, String> properties = message.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            tmpSize += entry.getKey().length() + entry.getValue().length();
        }
        tmpSize = tmpSize + 20; //for log overhead
        if (tmpSize > sizeLimit) {
            //it is unexpected that single message exceeds the sizeLimit
            //here just let it go, otherwise it will block the splitting process
            if (nextIndex - currIndex == 0) {
                //if the next sublist has no element, add this one and then break, otherwise just break
                nextIndex++;
            }
            break;
        }
        if (tmpSize + totalSize > sizeLimit) {
            break;
        } else {
            totalSize += tmpSize;
        }

    }
    List<Message> subList = messages.subList(currIndex, nextIndex);
    currIndex = nextIndex;
    return subList;
}
 
Example 16
Source File: TransactionProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private MsgMeta parseFromMsg(Message msg) {
    byte[] bs = msg.getBody();
    ByteBuffer buf = ByteBuffer.wrap(bs);
    MsgMeta msgMeta = new MsgMeta();
    msgMeta.batchId = buf.getLong();
    msgMeta.msgId = buf.getLong();
    msgMeta.sendResult = LocalTransactionState.values()[buf.get()];
    msgMeta.checkResult = new ArrayList<>();
    for (int i = 0; i < TransactionProducer.MAX_CHECK_RESULT_IN_MSG; i++) {
        msgMeta.checkResult.add(LocalTransactionState.values()[buf.get()]);
    }
    return msgMeta;
}
 
Example 17
Source File: SplitBatchProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public List<Message> next() {
    int nextIndex = currIndex;
    int totalSize = 0;
    for (; nextIndex < messages.size(); nextIndex++) {
        Message message = messages.get(nextIndex);
        int tmpSize = message.getTopic().length() + message.getBody().length;
        Map<String, String> properties = message.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            tmpSize += entry.getKey().length() + entry.getValue().length();
        }
        tmpSize = tmpSize + 20; //for log overhead
        if (tmpSize > sizeLimit) {
            //it is unexpected that single message exceeds the sizeLimit
            //here just let it go, otherwise it will block the splitting process
            if (nextIndex - currIndex == 0) {
                //if the next sublist has no element, add this one and then break, otherwise just break
                nextIndex++;
            }
            break;
        }
        if (tmpSize + totalSize > sizeLimit) {
            break;
        } else {
            totalSize += tmpSize;
        }

    }
    List<Message> subList = messages.subList(currIndex, nextIndex);
    currIndex = nextIndex;
    return subList;
}
 
Example 18
Source File: LocalTranListenerImpl.java    From order-charge-notify with Apache License 2.0 5 votes vote down vote up
/**
 * 扣款本地事务
 * @param msg
 * @param arg
 * @return
 */
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
    String message = new String(msg.getBody());
    LOGGER.info("[扣款本地事务监听回调]执行逻辑--接收到消息, message={}", message);
    OrderStatusUpdateProtocol protocol = new OrderStatusUpdateProtocol();
    protocol.decode(message);
    // 扣款
    String purseId = protocol.getPurseId();
    String merchantName = protocol.getMerchantName();
    LOGGER.info("[扣款本地事务监听回调]反序列化扣款消息成功,开始进行扣款操作,purseId={}", purseId);
    WalletEntity walletEntity = new WalletEntity();
    walletEntity.setChargeMoney(
            new BigDecimal(protocol.getChargeMoney()))
            .setPurseId(purseId)
            .setMerchantName(merchantName);
    // 查询当前账户信息获取版本号,基于乐观锁更新
    WalletEntity realWalletInfo = walletService.queryWalletInfoByPurseId(protocol.getPurseId());
    // 判断是否足够扣减
    BigDecimal currBalanceAccount = realWalletInfo.getBalanceAccount();
    if (currBalanceAccount.subtract(new BigDecimal(protocol.getChargeMoney())).longValue() < 0) {
        LOGGER.error("执行钱包扣款,账户不足扣减,消息回滚.purseId={}", purseId);
        return LocalTransactionState.ROLLBACK_MESSAGE;
    }
    // 足够扣减,进行账户扣减
    int version = realWalletInfo.getVersion();
    walletEntity.setVersion(version);
    try {
        if (!walletService.updateWallet(walletEntity, protocol.getOrderId())) {
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    } catch (Exception e) {
        LOGGER.error("执行钱包扣款本地扣款异常,purseId={},e={}", purseId, LogExceptionWapper.getStackTrace(e));
        return LocalTransactionState.UNKNOW;
    }
    LOGGER.info("[扣款本地事务监听回调]扣款本地操作成功,本地事务提交,purseId={}", purseId);
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 19
Source File: Validators.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
/**
     * Validate message
     */
//
    public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
        throws MQClientException {
//        消息不能为空
        if (null == msg) {
            throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
        }
        // topic
        Validators.checkTopic(msg.getTopic());

        // body 消息内容不能为空
        if (null == msg.getBody()) {
            throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
        }

//        消息内容不能为空
        if (0 == msg.getBody().length) {
            throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
        }

//        消息内容不能大于4m
        if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
            throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
                "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
        }
    }
 
Example 20
Source File: SplitBatchProducer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Override
public List<Message> next() {
    int nextIndex = currIndex;
    int totalSize = 0;
    for (; nextIndex < messages.size(); nextIndex++) {
        Message message = messages.get(nextIndex);
        int tmpSize = message.getTopic().length() + message.getBody().length;
        Map<String, String> properties = message.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            tmpSize += entry.getKey().length() + entry.getValue().length();
        }
        tmpSize = tmpSize + 20; //for log overhead
        if (tmpSize > sizeLimit) {
            //it is unexpected that single message exceeds the sizeLimit
            //here just let it go, otherwise it will block the splitting process
            if (nextIndex - currIndex == 0) {
                //if the next sublist has no element, add this one and then break, otherwise just break
                nextIndex++;
            }
            break;
        }
        if (tmpSize + totalSize > sizeLimit) {
            break;
        } else {
            totalSize += tmpSize;
        }

    }
    List<Message> subList = messages.subList(currIndex, nextIndex);
    currIndex = nextIndex;
    return subList;
}