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

The following examples show how to use org.apache.rocketmq.common.message.MessageBatch. 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 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 #2
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 #3
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, this);
            MessageClientIDSetter.setUniqID(message);
            message.setTopic(withNamespace(message.getTopic()));
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    msgBatch.setTopic(withNamespace(msgBatch.getTopic()));
    return msgBatch;
}
 
Example #4
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 #5
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 #6
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 #7
Source File: DefaultMQProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, null);
            MessageClientIDSetter.setUniqID(message);
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    return msgBatch;
}
 
Example #8
Source File: DefaultMQProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, this);
            MessageClientIDSetter.setUniqID(message);
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    return msgBatch;
}
 
Example #9
Source File: BatchMQProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * refer: {@link DefaultMQProducer#batch(java.util.Collection)}
 */
private MessageBatch buildBatchRmqMessage(List<CarreraRequest> batchRequest) {
    Map<String, List<CarreraRequest>> topicRequestMap = batchRequest.stream().collect(Collectors.groupingBy(CarreraRequest::getTopic));
    List<String> topics = new ArrayList<>();
    ByteBuffer buffer = getEncodeBuffer();
    topicRequestMap.forEach((topic, requestList) -> {
        int topicIdx = topics.size();
        topics.add(topic);
        requestList.forEach(request -> {
            assert request.getMessageQueue() != null;
            if (!encode(buffer, request, topicIdx)) {
                LOGGER.debug("encode error. cur request={},batch={}", request, batchRequest);
                request.onException(new MQClientException(-1, "encode batch message error."));
            }
        });

    });

    MessageBatch messageBatch = new MessageBatch();
    buffer.flip();
    byte[] body = new byte[buffer.remaining()];
    System.arraycopy(buffer.array(), 0, body, 0, body.length);
    messageBatch.setBody(body);
    messageBatch.setMultiTopic(true);
    messageBatch.setTopic(String.join(MixAll.BATCH_TOPIC_SPLITTER, topics));
    messageBatch.setWaitStoreMsgOK(true);
    return messageBatch;
}
 
Example #10
Source File: DefaultMQProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, null);
            MessageClientIDSetter.setUniqID(message);
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    return msgBatch;
}
 
Example #11
Source File: DefaultMQProducerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 尝试来压缩Message
 * @param msg msg
 * @return ;
 */
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) {
        //如果消息大于4kb就要压缩
        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 #12
Source File: DefaultMQProducer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, this);
            MessageClientIDSetter.setUniqID(message);
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    return msgBatch;
}
 
Example #13
Source File: DeFiBusProducerImpl.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, deFiBusProducer.getDefaultMQProducer());
            MessageClientIDSetter.setUniqID(message);
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    return msgBatch;
}
 
Example #14
Source File: BatchMQProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * refer: {@link DefaultMQProducer#batch(java.util.Collection)}
 */
private MessageBatch buildBatchRmqMessage(List<CarreraRequest> batchRequest) {
    Map<String, List<CarreraRequest>> topicRequestMap = batchRequest.stream().collect(Collectors.groupingBy(CarreraRequest::getTopic));
    List<String> topics = new ArrayList<>();
    ByteBuffer buffer = getEncodeBuffer();
    topicRequestMap.forEach((topic, requestList) -> {
        int topicIdx = topics.size();
        topics.add(topic);
        requestList.forEach(request -> {
            assert request.getMessageQueue() != null;
            if (!encode(buffer, request, topicIdx)) {
                LOGGER.debug("encode error. cur request={},batch={}", request, batchRequest);
                request.onException(new MQClientException(-1, "encode batch message error."));
            }
        });

    });

    MessageBatch messageBatch = new MessageBatch();
    buffer.flip();
    byte[] body = new byte[buffer.remaining()];
    System.arraycopy(buffer.array(), 0, body, 0, body.length);
    messageBatch.setBody(body);
    messageBatch.setMultiTopic(true);
    messageBatch.setTopic(String.join(MixAll.BATCH_TOPIC_SPLITTER, topics));
    messageBatch.setWaitStoreMsgOK(true);
    return messageBatch;
}
 
Example #15
Source File: DefaultMQProducer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private MessageBatch batch(Collection<Message> msgs) throws MQClientException {
    MessageBatch msgBatch;
    try {
        msgBatch = MessageBatch.generateFromList(msgs);
        for (Message message : msgBatch) {
            Validators.checkMessage(message, this);
            MessageClientIDSetter.setUniqID(message);
        }
        msgBatch.setBody(msgBatch.encode());
    } catch (Exception e) {
        throw new MQClientException("Failed to initiate the MessageBatch", e);
    }
    return msgBatch;
}
 
Example #16
Source File: MessageBatchTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_Retry() throws Exception{
    List<Message> messages = generateMessages();
    messages.get(1).setTopic(MixAll.RETRY_GROUP_TOPIC_PREFIX + "topic");
    MessageBatch.generateFromList(messages);
}
 
Example #17
Source File: MessageBatchTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_Delay() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setDelayTimeLevel(1);
    MessageBatch.generateFromList(messages);
}
 
Example #18
Source File: MessageBatchTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_DiffWaitOK() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setWaitStoreMsgOK(false);
    MessageBatch.generateFromList(messages);
}
 
Example #19
Source File: MessageBatchTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test
public void testGenerate_OK() throws Exception{
    List<Message> messages = generateMessages();
    MessageBatch.generateFromList(messages);
}
 
Example #20
Source File: MessageBatchTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_DiffTopic() throws Exception{
    List<Message> messages = generateMessages();
    messages.get(1).setTopic("topic2");
    MessageBatch.generateFromList(messages);
}
 
Example #21
Source File: MessageBatchTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_DiffWaitOK() throws Exception{
    List<Message> messages = generateMessages();
    messages.get(1).setWaitStoreMsgOK(false);
    MessageBatch.generateFromList(messages);
}
 
Example #22
Source File: MessageBatchTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_Delay() throws Exception{
    List<Message> messages = generateMessages();
    messages.get(1).setDelayTimeLevel(1);
    MessageBatch.generateFromList(messages);
}
 
Example #23
Source File: MessageBatchTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_Retry() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setTopic(MixAll.RETRY_GROUP_TOPIC_PREFIX + "topic");
    MessageBatch.generateFromList(messages);
}
 
Example #24
Source File: MessageBatchTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_DiffTopic() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setTopic("topic2");
    MessageBatch.generateFromList(messages);
}
 
Example #25
Source File: MessageBatchTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
public void testGenerate_OK() throws Exception {
    List<Message> messages = generateMessages();
    MessageBatch.generateFromList(messages);
}
 
Example #26
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private SendResult processSendResponse(
    final String brokerName,
    final Message msg,
    final RemotingCommand response
) throws MQBrokerException, RemotingCommandException {
    SendStatus sendStatus;
    switch (response.getCode()) {
        case ResponseCode.FLUSH_DISK_TIMEOUT: {
            sendStatus = SendStatus.FLUSH_DISK_TIMEOUT;
            break;
        }
        case ResponseCode.FLUSH_SLAVE_TIMEOUT: {
            sendStatus = SendStatus.FLUSH_SLAVE_TIMEOUT;
            break;
        }
        case ResponseCode.SLAVE_NOT_AVAILABLE: {
            sendStatus = SendStatus.SLAVE_NOT_AVAILABLE;
            break;
        }
        case ResponseCode.SUCCESS: {
            sendStatus = SendStatus.SEND_OK;
            break;
        }
        default: {
            throw new MQBrokerException(response.getCode(), response.getRemark());
        }
    }

    SendMessageResponseHeader responseHeader =
            (SendMessageResponseHeader) response.decodeCommandCustomHeader(SendMessageResponseHeader.class);

    //If namespace not null , reset Topic without namespace.
    String topic = msg.getTopic();
    if (StringUtils.isNotEmpty(this.clientConfig.getNamespace())) {
        topic = NamespaceUtil.withoutNamespace(topic, this.clientConfig.getNamespace());
    }

    MessageQueue messageQueue = new MessageQueue(topic, brokerName, responseHeader.getQueueId());

    String uniqMsgId = MessageClientIDSetter.getUniqID(msg);
    if (msg instanceof MessageBatch) {
        StringBuilder sb = new StringBuilder();
        for (Message message : (MessageBatch) msg) {
            sb.append(sb.length() == 0 ? "" : ",").append(MessageClientIDSetter.getUniqID(message));
        }
        uniqMsgId = sb.toString();
    }
    SendResult sendResult = new SendResult(sendStatus,
            uniqMsgId,
            responseHeader.getMsgId(), messageQueue, responseHeader.getQueueOffset());
    sendResult.setTransactionId(responseHeader.getTransactionId());
    String regionId = response.getExtFields().get(MessageConst.PROPERTY_MSG_REGION);
    String traceOn = response.getExtFields().get(MessageConst.PROPERTY_TRACE_SWITCH);
    if (regionId == null || regionId.isEmpty()) {
        regionId = MixAll.DEFAULT_TRACE_REGION_ID;
    }
    if (traceOn != null && traceOn.equals("false")) {
        sendResult.setTraceOn(false);
    } else {
        sendResult.setTraceOn(true);
    }
    sendResult.setRegionId(regionId);
    return sendResult;
}
 
Example #27
Source File: MessageBatchTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testGenerate_OK() throws Exception {
    List<Message> messages = generateMessages();
    MessageBatch.generateFromList(messages);
}
 
Example #28
Source File: MessageBatchTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_DiffTopic() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setTopic("topic2");
    MessageBatch.generateFromList(messages);
}
 
Example #29
Source File: MessageBatchTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_DiffWaitOK() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setWaitStoreMsgOK(false);
    MessageBatch.generateFromList(messages);
}
 
Example #30
Source File: MessageBatchTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testGenerate_Delay() throws Exception {
    List<Message> messages = generateMessages();
    messages.get(1).setDelayTimeLevel(1);
    MessageBatch.generateFromList(messages);
}