开发者社区> 问答> 正文

canal 支持多线程消费吗?

我目前的程序是单线程消费,日志来不及消费,有很大的延后,可以支持多线程消费吗?目前的Java代码如下:

while(true) { CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("127.0.0.1", 11111), "example", "", ""); int batchSize = 100; long batchId = 0; try { connector.connect(); connector.subscribe(); while (true) { try { Message message = connector.getWithoutAck(batchSize); batchId = message.getId(); int size = message.getEntries().size(); if (batchId == -1 || size == 0) { try { Thread.sleep(1000); } catch (Exception e) {} } else { printEntry(message.getEntries()); }

                connector.ack(batchId);
        	} catch(Exception ex) {
        		app.logger.error("main", ex);
        		connector.rollback(batchId);
        	}
        }
    } catch(Exception ex) {
    	app.logger.error("main", ex);
    } finally {
    	try {
    		connector.disconnect();
    	} catch(Exception ex) {}
    }

原提问者GitHub用户hezhiqiang

展开
收起
Java工程师 2023-05-03 10:44:40 174 0
1 条回答
写回答
取消 提交回答
  • 支持的,adapter的源码就是使用的多线程模式。AbstractCanalAdapterWorker这个类,ExecutorService的submit方法。

    原回答者GitHub用户wedog

    2023-05-04 14:45:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载