Search in sources :

Example 21 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance in project canal by alibaba.

the class CanalServerWithEmbedded method unsubscribe.

/**
 * 取消订阅
 */
@Override
public void unsubscribe(ClientIdentity clientIdentity) throws CanalServerException {
    CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
    // 执行一下meta订阅
    canalInstance.getMetaManager().unsubscribe(clientIdentity);
    logger.info("unsubscribe successfully, {}", clientIdentity);
}
Also used : CanalInstance(com.alibaba.otter.canal.instance.core.CanalInstance)

Example 22 with CanalInstance

use of com.alibaba.otter.canal.instance.core.CanalInstance 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

CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)22 CanalInstanceGenerator (com.alibaba.otter.canal.instance.core.CanalInstanceGenerator)6 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)5 CanalServerException (com.alibaba.otter.canal.server.exception.CanalServerException)5 CanalException (com.alibaba.otter.canal.common.CanalException)3 CanalInstanceWithManager (com.alibaba.otter.canal.instance.manager.CanalInstanceWithManager)3 Canal (com.alibaba.otter.canal.instance.manager.model.Canal)3 CanalEventParser (com.alibaba.otter.canal.parse.CanalEventParser)3 Message (com.alibaba.otter.canal.protocol.Message)3 Position (com.alibaba.otter.canal.protocol.position.Position)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)2 Event (com.alibaba.otter.canal.store.model.Event)2 List (java.util.List)2 Before (org.junit.Before)2 MQDestination (com.alibaba.otter.canal.connector.core.producer.MQDestination)1 Callback (com.alibaba.otter.canal.connector.core.util.Callback)1 MediaHAController (com.alibaba.otter.canal.extend.ha.MediaHAController)1 CanalMQConfig (com.alibaba.otter.canal.instance.core.CanalMQConfig)1