Search in sources :

Example 1 with Message

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

the class BaseCanalServerWithEmbededTest method testGet.

@Test
public void testGet() {
    int maxEmptyCount = 10;
    int emptyCount = 0;
    int totalCount = 0;
    server.subscribe(clientIdentity);
    while (emptyCount < maxEmptyCount) {
        Message message = server.get(clientIdentity, 11);
        if (CollectionUtils.isEmpty(message.getEntries())) {
            emptyCount++;
            try {
                Thread.sleep(emptyCount * 300L);
            } catch (InterruptedException e) {
                Assert.fail();
            }
            System.out.println("empty count : " + emptyCount);
        } else {
            emptyCount = 0;
            totalCount += message.getEntries().size();
        }
    }
    System.out.println("!!!!!! testGet totalCount : " + totalCount);
    server.unsubscribe(clientIdentity);
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) Test(org.junit.Test)

Example 2 with Message

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

the class BaseCanalServerWithEmbededTest method testSwitch.

// @Test
public void testSwitch() {
    int maxEmptyCount = 10;
    int emptyCount = 0;
    int totalCount = 0;
    int thresold = 50;
    int batchSize = 11;
    server.subscribe(clientIdentity);
    while (emptyCount < maxEmptyCount) {
        Message message = server.get(clientIdentity, batchSize);
        if (CollectionUtils.isEmpty(message.getEntries())) {
            emptyCount++;
            try {
                Thread.sleep(emptyCount * 300L);
            } catch (InterruptedException e) {
                Assert.fail();
            }
            System.out.println("empty count : " + emptyCount);
        } else {
            emptyCount = 0;
            totalCount += message.getEntries().size();
            if ((totalCount + 1) % 100 >= thresold && (totalCount + 1) % 100 <= thresold + batchSize) {
                CanalEventParser eventParser = server.getCanalInstances().get(DESTINATION).getEventParser();
                if (eventParser instanceof CanalHASwitchable) {
                    // 执行切换
                    ((CanalHASwitchable) eventParser).doSwitch();
                    try {
                        // 等待parser启动
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                        Assert.fail();
                    }
                }
            }
        }
    }
    System.out.println("!!!!!! testGet totalCount : " + totalCount);
    server.unsubscribe(clientIdentity);
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) CanalEventParser(com.alibaba.otter.canal.parse.CanalEventParser) CanalHASwitchable(com.alibaba.otter.canal.parse.CanalHASwitchable)

Example 3 with Message

use of com.alibaba.otter.canal.protocol.Message in project java-example by 1479005017.

the class CanalMessageHandler method doRun.

public void doRun() throws CanalClientException {
    while (true) {
        Message message = connector.getWithoutAck(100);
        if (message.getId() == -1 || message.getEntries().size() == 0) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            continue;
        }
        TransactionStatus transactionStatus = null;
        if (transactionManager != null) {
            DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
            transactionStatus = transactionManager.getTransaction(transactionDefinition);
        }
        try {
            handleMessage(message);
            if (transactionStatus != null)
                transactionManager.commit(transactionStatus);
            connector.ack(message.getId());
        } catch (Exception e) {
            e.printStackTrace();
            if (transactionStatus != null)
                transactionManager.rollback(transactionStatus);
            connector.rollback(message.getId());
        }
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(com.alibaba.otter.canal.protocol.Message) TransactionStatus(org.springframework.transaction.TransactionStatus) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Example 4 with Message

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

the class KafkaClientRunningTest method testKafkaConsumer.

public void testKafkaConsumer() {
    final ExecutorService executor = Executors.newFixedThreadPool(1);
    final KafkaCanalConnector connector = new KafkaCanalConnector(servers, topic, partition, groupId, null, false);
    executor.submit(() -> {
        connector.connect();
        connector.subscribe();
        while (running) {
            List<Message> messages = connector.getList(3L, TimeUnit.SECONDS);
            if (messages != null) {
                System.out.println(messages);
            }
            connector.ack();
        }
        connector.unsubscribe();
        connector.disconnect();
    });
    sleep(60000);
    running = false;
    executor.shutdown();
    logger.info("shutdown completed");
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) ExecutorService(java.util.concurrent.ExecutorService) KafkaCanalConnector(com.alibaba.otter.canal.client.kafka.KafkaCanalConnector)

Example 5 with Message

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

the class CanalRocketMQClientExample method process.

private void process() {
    while (!running) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    while (running) {
        try {
            connector.connect();
            connector.subscribe();
            while (running) {
                // 获取message
                List<Message> messages = connector.getListWithoutAck(1000L, TimeUnit.MILLISECONDS);
                for (Message message : messages) {
                    long batchId = message.getId();
                    int size = message.getEntries().size();
                    if (batchId == -1 || size == 0) {
                    // try {
                    // Thread.sleep(1000);
                    // } catch (InterruptedException e) {
                    // }
                    } else {
                        printSummary(message, batchId, size);
                        printEntry(message.getEntries());
                    // logger.info(message.toString());
                    }
                }
                // 提交确认
                connector.ack();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    connector.unsubscribe();
// connector.stopRunning();
}
Also used : Message(com.alibaba.otter.canal.protocol.Message)

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