All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.brandtg.switchboard.MysqlLogIterator Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2015 Greg Brandt ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.brandtg.switchboard;

import com.google.code.or.OpenParser;
import com.google.code.or.binlog.BinlogEventListener;
import com.google.code.or.binlog.BinlogEventV4;
import com.google.code.or.binlog.BinlogParser;

import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class MysqlLogIterator extends LogIterator {
  private final BlockingQueue events;

  public MysqlLogIterator(InputStream inputStream) throws Exception {
    this(new MysqlLogParser(inputStream));
  }

  /**
   * Iterates over a stream of binlog events and provides BinlogEventV4 objects.
   */
  public MysqlLogIterator(BinlogParser binlogParser) throws Exception {
    // TODO: Should be able to set this to daemon
    OpenParser openParser = new OpenParser();
    openParser.setBinlogParser(binlogParser);
    openParser.setBinlogEventListener(new EventListener());
    openParser.start();

    this.events = new ArrayBlockingQueue<>(1);
  }

  @Override
  public BinlogEventV4 next() {
    try {
      return events.take();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private class EventListener implements BinlogEventListener {
    @Override
    public void onEvents(BinlogEventV4 event) {
      boolean success = false;
      do {
        try {
          events.put(event);
          success = true;
        } catch (InterruptedException e) {
          // Retry
        }
      }
      while (!success);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy