Search in sources :

Example 1 with Callback

use of com.alibaba.otter.canal.connector.core.util.Callback in project canal by alibaba.

the class CanalMQStarter method worker.

private void worker(String destination, AtomicBoolean destinationRunning) {
    while (!running || !destinationRunning.get()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        // ignore
        }
    }
    logger.info("## start the MQ producer: {}.", destination);
    MDC.put("destination", destination);
    final ClientIdentity clientIdentity = new ClientIdentity(destination, (short) 1001, "");
    while (running && destinationRunning.get()) {
        try {
            CanalInstance canalInstance = canalServer.getCanalInstances().get(destination);
            if (canalInstance == null) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                // ignore
                }
                continue;
            }
            MQDestination canalDestination = new MQDestination();
            canalDestination.setCanalDestination(destination);
            CanalMQConfig mqConfig = canalInstance.getMqConfig();
            canalDestination.setTopic(mqConfig.getTopic());
            canalDestination.setPartition(mqConfig.getPartition());
            canalDestination.setDynamicTopic(mqConfig.getDynamicTopic());
            canalDestination.setPartitionsNum(mqConfig.getPartitionsNum());
            canalDestination.setPartitionHash(mqConfig.getPartitionHash());
            canalDestination.setDynamicTopicPartitionNum(mqConfig.getDynamicTopicPartitionNum());
            canalDestination.setEnableDynamicQueuePartition(mqConfig.getEnableDynamicQueuePartition());
            canalServer.subscribe(clientIdentity);
            logger.info("## the MQ producer: {} is running now ......", destination);
            Integer getTimeout = mqProperties.getFetchTimeout();
            Integer getBatchSize = mqProperties.getBatchSize();
            while (running && destinationRunning.get()) {
                Message message;
                if (getTimeout != null && getTimeout > 0) {
                    message = canalServer.getWithoutAck(clientIdentity, getBatchSize, getTimeout.longValue(), TimeUnit.MILLISECONDS);
                } else {
                    message = canalServer.getWithoutAck(clientIdentity, getBatchSize);
                }
                final long batchId = message.getId();
                try {
                    int size = message.isRaw() ? message.getRawEntries().size() : message.getEntries().size();
                    if (batchId != -1 && size != 0) {
                        canalMQProducer.send(canalDestination, message, new Callback() {

                            @Override
                            public void commit() {
                                // 提交确认
                                canalServer.ack(clientIdentity, batchId);
                            }

                            @Override
                            public void rollback() {
                                canalServer.rollback(clientIdentity, batchId);
                            }
                        });
                    // 发送message到topic
                    } else {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                        // ignore
                        }
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            logger.error("process error!", e);
        }
    }
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance) Callback(com.alibaba.otter.canal.connector.core.util.Callback) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) MQDestination(com.alibaba.otter.canal.connector.core.producer.MQDestination) Message(com.alibaba.otter.canal.protocol.Message) CanalMQConfig(com.alibaba.otter.canal.instance.core.CanalMQConfig)

Aggregations

MQDestination (com.alibaba.otter.canal.connector.core.producer.MQDestination)1 Callback (com.alibaba.otter.canal.connector.core.util.Callback)1 CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)1 CanalMQConfig (com.alibaba.otter.canal.instance.core.CanalMQConfig)1 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)1 Message (com.alibaba.otter.canal.protocol.Message)1