Search in sources :

Example 1 with CanalHASwitchable

use of com.alibaba.otter.canal.parse.CanalHASwitchable 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)

Aggregations

CanalEventParser (com.alibaba.otter.canal.parse.CanalEventParser)1 CanalHASwitchable (com.alibaba.otter.canal.parse.CanalHASwitchable)1 Message (com.alibaba.otter.canal.protocol.Message)1