Search in sources :

Example 1 with ConsumerBatchMessage

use of com.alibaba.otter.canal.client.ConsumerBatchMessage in project canal by alibaba.

the class RocketMQCanalConnector method process.

private boolean process(List<MessageExt> messageExts) {
    if (logger.isDebugEnabled()) {
        logger.debug("Get Message: {}", messageExts);
    }
    List messageList = new ArrayList<>();
    for (MessageExt messageExt : messageExts) {
        byte[] data = messageExt.getBody();
        if (data != null) {
            try {
                if (!flatMessage) {
                    Message message = CanalMessageDeserializer.deserializer(data);
                    messageList.add(message);
                } else {
                    FlatMessage flatMessage = JSON.parseObject(data, FlatMessage.class);
                    messageList.add(flatMessage);
                }
            } catch (Exception ex) {
                logger.error("Add message error", ex);
                throw new CanalClientException(ex);
            }
        } else {
            logger.warn("Received message data is null");
        }
    }
    ConsumerBatchMessage batchMessage;
    if (!flatMessage) {
        batchMessage = new ConsumerBatchMessage<Message>(messageList);
    } else {
        batchMessage = new ConsumerBatchMessage<FlatMessage>(messageList);
    }
    try {
        messageBlockingQueue.put(batchMessage);
    } catch (InterruptedException e) {
        logger.error("Put message to queue error", e);
        throw new RuntimeException(e);
    }
    boolean isCompleted;
    try {
        isCompleted = batchMessage.waitFinish(batchProcessTimeout);
    } catch (InterruptedException e) {
        logger.error("Interrupted when waiting messages to be finished.", e);
        throw new RuntimeException(e);
    }
    boolean isSuccess = batchMessage.isSuccess();
    return isCompleted && isSuccess;
}
Also used : ConsumerBatchMessage(com.alibaba.otter.canal.client.ConsumerBatchMessage) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Message(com.alibaba.otter.canal.protocol.Message) ArrayList(java.util.ArrayList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) MessageExt(org.apache.rocketmq.common.message.MessageExt) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerBatchMessage(com.alibaba.otter.canal.client.ConsumerBatchMessage)

Example 2 with ConsumerBatchMessage

use of com.alibaba.otter.canal.client.ConsumerBatchMessage in project canal by alibaba.

the class RabbitMQCanalConnector method process.

private boolean process(byte[] messageData) {
    if (logger.isDebugEnabled()) {
        logger.debug("Get Message: {}", new String(messageData));
    }
    List messageList = new ArrayList<>();
    if (!flatMessage) {
        Message message = CanalMessageDeserializer.deserializer(messageData);
        messageList.add(message);
    } else {
        FlatMessage flatMessage = JSON.parseObject(messageData, FlatMessage.class);
        messageList.add(flatMessage);
    }
    ConsumerBatchMessage batchMessage;
    if (!flatMessage) {
        batchMessage = new ConsumerBatchMessage<Message>(messageList);
    } else {
        batchMessage = new ConsumerBatchMessage<FlatMessage>(messageList);
    }
    try {
        messageBlockingQueue.put(batchMessage);
    } catch (InterruptedException e) {
        logger.error("Put message to queue error", e);
        throw new RuntimeException(e);
    }
    boolean isCompleted;
    try {
        isCompleted = batchMessage.waitFinish(batchProcessTimeout);
    } catch (InterruptedException e) {
        logger.error("Interrupted when waiting messages to be finished.", e);
        throw new RuntimeException(e);
    }
    boolean isSuccess = batchMessage.isSuccess();
    return isCompleted && isSuccess;
}
Also used : FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Message(com.alibaba.otter.canal.protocol.Message) ConsumerBatchMessage(com.alibaba.otter.canal.client.ConsumerBatchMessage) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerBatchMessage(com.alibaba.otter.canal.client.ConsumerBatchMessage)

Aggregations

ConsumerBatchMessage (com.alibaba.otter.canal.client.ConsumerBatchMessage)2 FlatMessage (com.alibaba.otter.canal.protocol.FlatMessage)2 Message (com.alibaba.otter.canal.protocol.Message)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 MessageExt (org.apache.rocketmq.common.message.MessageExt)1