Search in sources :

Example 26 with Message

use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.

the class CanalPulsarMQConsumer method getMessage.

@SuppressWarnings("unchecked")
@Override
public List<CommonMessage> getMessage(Long timeout, TimeUnit unit) {
    List<CommonMessage> messageList = Lists.newArrayList();
    try {
        Messages<byte[]> messages = pulsarMQConsumer.batchReceive();
        if (null == messages || messages.size() == 0) {
            return messageList;
        }
        // 保存当前消费记录,用于ack和rollback
        this.lastGetBatchMessage = messages;
        for (org.apache.pulsar.client.api.Message<byte[]> msg : messages) {
            byte[] data = msg.getData();
            if (!this.flatMessage) {
                Message message = CanalMessageSerializerUtil.deserializer(data);
                List<CommonMessage> list = MessageUtil.convert(message);
                messageList.addAll(list);
            } else {
                CommonMessage commonMessage = JSON.parseObject(data, CommonMessage.class);
                messageList.add(commonMessage);
            }
        }
    } catch (PulsarClientException e) {
        throw new CanalClientException("Receive pulsar batch message error", e);
    }
    return messageList;
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) org.apache.pulsar.client.api(org.apache.pulsar.client.api)

Example 27 with Message

use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.

the class CanalRabbitMQProducer method send.

@Override
public void send(final MQDestination destination, Message message, Callback callback) {
    ExecutorTemplate template = new ExecutorTemplate(sendExecutor);
    try {
        if (!StringUtils.isEmpty(destination.getDynamicTopic())) {
            // 动态topic
            Map<String, Message> messageMap = MQMessageUtils.messageTopics(message, destination.getTopic(), destination.getDynamicTopic());
            for (Map.Entry<String, com.alibaba.otter.canal.protocol.Message> entry : messageMap.entrySet()) {
                final String topicName = entry.getKey().replace('.', '_');
                final com.alibaba.otter.canal.protocol.Message messageSub = entry.getValue();
                template.submit(() -> send(destination, topicName, messageSub));
            }
            template.waitForResult();
        } else {
            send(destination, destination.getTopic(), message);
        }
        callback.commit();
    } catch (Throwable e) {
        logger.error(e.getMessage(), e);
        callback.rollback();
    } finally {
        template.clear();
    }
}
Also used : ExecutorTemplate(com.alibaba.otter.canal.common.utils.ExecutorTemplate) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Message(com.alibaba.otter.canal.protocol.Message) Message(com.alibaba.otter.canal.protocol.Message) Map(java.util.Map)

Example 28 with Message

use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.

the class CanalRabbitMQConsumer method process.

private boolean process(byte[] messageData) {
    if (logger.isDebugEnabled()) {
        logger.debug("Get Message: {}", new String(messageData));
    }
    List<CommonMessage> messageList = new ArrayList<>();
    if (!flatMessage) {
        Message message = CanalMessageSerializerUtil.deserializer(messageData);
        messageList.addAll(MessageUtil.convert(message));
    } else {
        CommonMessage commonMessage = JSON.parseObject(messageData, CommonMessage.class);
        messageList.add(commonMessage);
    }
    ConsumerBatchMessage<CommonMessage> batchMessage = new ConsumerBatchMessage<>(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 : Message(com.alibaba.otter.canal.protocol.Message) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) ArrayList(java.util.ArrayList) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage)

Example 29 with Message

use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.

the class KafkaCanalConnector method getListWithoutAck.

@Override
public List<Message> getListWithoutAck(Long timeout, TimeUnit unit) throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return Lists.newArrayList();
    }
    ConsumerRecords<String, Message> records = kafkaConsumer.poll(unit.toMillis(timeout));
    currentOffsets.clear();
    for (TopicPartition topicPartition : records.partitions()) {
        currentOffsets.put(topicPartition.partition(), kafkaConsumer.position(topicPartition));
    }
    if (!records.isEmpty()) {
        List<Message> messages = new ArrayList<>();
        for (ConsumerRecord<String, Message> record : records) {
            messages.add(record.value());
        }
        return messages;
    }
    return Lists.newArrayList();
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) TopicPartition(org.apache.kafka.common.TopicPartition)

Example 30 with Message

use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.

the class KafkaOffsetCanalConnector method getListWithoutAck.

/**
 * 获取Kafka消息,不确认
 *
 * @param timeout
 * @param unit
 * @param offset  消息偏移地址(-1为不偏移)
 * @return
 * @throws CanalClientException
 */
public List<KafkaMessage> getListWithoutAck(Long timeout, TimeUnit unit, long offset) throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return new ArrayList<>();
    }
    if (offset > -1) {
        TopicPartition tp = new TopicPartition(topic, partition == null ? 0 : partition);
        kafkaConsumer.seek(tp, offset);
    }
    ConsumerRecords<String, Message> records = kafkaConsumer.poll(unit.toMillis(timeout));
    if (!records.isEmpty()) {
        List<KafkaMessage> messages = new ArrayList<>();
        for (ConsumerRecord<String, Message> record : records) {
            KafkaMessage message = new KafkaMessage(record.value(), record.offset());
            messages.add(message);
        }
        return messages;
    }
    return new ArrayList<>();
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) KafkaMessage(com.alibaba.otter.canal.client.kafka.protocol.KafkaMessage) KafkaFlatMessage(com.alibaba.otter.canal.client.kafka.protocol.KafkaFlatMessage) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaMessage(com.alibaba.otter.canal.client.kafka.protocol.KafkaMessage) ArrayList(java.util.ArrayList)

Aggregations

Message (com.alibaba.otter.canal.protocol.Message)37 ArrayList (java.util.ArrayList)12 FlatMessage (com.alibaba.otter.canal.protocol.FlatMessage)11 List (java.util.List)7 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)6 Test (org.junit.Test)6 CommonMessage (com.alibaba.otter.canal.connector.core.consumer.CommonMessage)5 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)4 ByteString (com.google.protobuf.ByteString)4 CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)3 CanalConnector (com.alibaba.otter.canal.client.CanalConnector)2 ConsumerBatchMessage (com.alibaba.otter.canal.client.ConsumerBatchMessage)2 ExecutorTemplate (com.alibaba.otter.canal.common.utils.ExecutorTemplate)2 CanalEventParser (com.alibaba.otter.canal.parse.CanalEventParser)2 CanalHASwitchable (com.alibaba.otter.canal.parse.CanalHASwitchable)2 CanalEntry (com.alibaba.otter.canal.protocol.CanalEntry)2 Messages (com.alibaba.otter.canal.protocol.CanalPacket.Messages)2 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)2 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)2 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)2