Java Code Examples for com.github.shyiko.mysql.binlog.BinaryLogClient#registerEventListener()

The following examples show how to use com.github.shyiko.mysql.binlog.BinaryLogClient#registerEventListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinlogStream.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private synchronized BinaryLogClient allocateBinaryLogClient() {
    if (isConnected()) {
        throw new IllegalStateException("MySQL replication stream is already open");
    }
    binaryLogClient = new BinaryLogClient(hostname, port, username, password);
    binaryLogClient.setBinlogFilename(getBinglogFile());
    binaryLogClient.setBinlogPosition(getBinlogPos());
    binaryLogClient.setServerId(getSlaveID());
    binaryLogClient.registerEventListener(new DelegatingEventListener());
    return binaryLogClient;
}
 
Example 2
Source File: BinlogServiceTest.java    From ad with Apache License 2.0 4 votes vote down vote up
@Test
public void testBinlog() throws IOException {
    String hostname = "127.0.0.1", username = "yuwen", password = "lyp82nlf";
    int port = 3306;
    // BinaryLogClient其实就是一个连接数据库的客户端,
    // 它加自己伪装成slave 连接到master上
    BinaryLogClient client = new BinaryLogClient(
            hostname, port, username, password
    );
    // 设置监听的Binlog, 如果不设置则监听最新的Binlog
    //client.setBinlogFilename();
    // 设置监听的binlog位置, 如果不设置则监听最新的位置
    //client.setBinlogPosition();
    // 注册事件监听器, 监听期间MySQL发生的一些变化, Event代表已经发生的事件
    client.registerEventListener(event -> {
        // MySQL 数据表发生变化的一些数据
        EventData eventData = event.getData();
        if (eventData instanceof UpdateRowsEventData) {
            log.info("update event");
            log.debug("{}", eventData);
        } else if (eventData instanceof WriteRowsEventData) {
            log.info("write event");
            log.debug("{}", eventData);
        } else if (eventData instanceof DeleteRowsEventData) {
            log.info("delete event");
            log.debug("{}", eventData);
        }
    });
    // 连接到 master 开始监听
    client.connect();


    // 启动后手动连接到 MySQL执行
    // insert into `ad_unit_keyword` (`unit_id`, `keyword`) values (10, '标志');
    // 控制台得到日志
    // 15:39:17.410 [main] INFO top.ezttf.ad.service.BinlogServiceTest - write event
    // 15:39:17.459 [main] DEBUG top.ezttf.ad.service.BinlogServiceTest - WriteRowsEventData{tableId=122, includedColumns={0, 1, 2}, rows=[
    //     [13, 10, 标志]
    // ]}

    // WriteRowsEventData{tableId=118, includedColumns={0, 1, 2, 3, 4, 5, 6, 7}, rows=[
    //    [11, 666, plan, 1, 2019-01-01, 2019-01-01, Tue Jan 01 08:00:00 CST 2019, Tue Jan 01 08:00:00 CST 2019]
    //]}
}
 
Example 3
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 4 votes vote down vote up
public void start() throws Exception {

        initDataSource();

        binlogPositionManager = new BinlogPositionManager(config, dataSource);
        binlogPositionManager.initBeginPosition();

        schema = new Schema(dataSource);
        String whiteDataBases = config.getWhiteDataBase();
        String whiteTables = config.getWhiteTable();

        if (!StringUtils.isEmpty(whiteDataBases)){
            Arrays.asList(whiteDataBases.trim().split(",")).forEach(whiteDataBase ->{
                Collections.addAll(schema.dataBaseWhiteList, whiteDataBase);
            });
        }

        if (!StringUtils.isEmpty(whiteTables)){
            Arrays.asList(whiteTables.trim().split(",")).forEach(whiteTable ->{
                Collections.addAll(schema.tableWhiteList, whiteTable);
            });
        }
        schema.load();

        eventListener = new EventListener(queue);
        binaryLogClient = new BinaryLogClient(config.mysqlAddr,
            config.mysqlPort,
            config.mysqlUsername,
            config.mysqlPassword);
        binaryLogClient.setBlocking(true);
        binaryLogClient.setServerId(1001);

        EventDeserializer eventDeserializer = new EventDeserializer();
        eventDeserializer.setCompatibilityMode(EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
            EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY);
        binaryLogClient.setEventDeserializer(eventDeserializer);
        binaryLogClient.registerEventListener(eventListener);
        binaryLogClient.setBinlogFilename(binlogPositionManager.getBinlogFilename());
        binaryLogClient.setBinlogPosition(binlogPositionManager.getPosition());

        binaryLogClient.connect(3000);

        LOGGER.info("Started.");

        stopped = false;

        doProcess();
    }