use of io.eventuate.local.common.BinlogFileOffset in project eventuate-local by eventuate-local.
the class PostgresWalClient method connectAndRun.
private void connectAndRun(Optional<BinlogFileOffset> binlogFileOffset, Consumer<EVENT> eventConsumer) throws SQLException, InterruptedException, IOException {
countDownLatchForStop = new CountDownLatch(1);
Properties props = new Properties();
PGProperty.USER.set(props, user);
PGProperty.PASSWORD.set(props, password);
PGProperty.ASSUME_MIN_SERVER_VERSION.set(props, "9.4");
PGProperty.REPLICATION.set(props, "database");
PGProperty.PREFER_QUERY_MODE.set(props, "simple");
connection = DriverManager.getConnection(url, props);
PGConnection replConnection = connection.unwrap(PGConnection.class);
LogSequenceNumber lsn = binlogFileOffset.flatMap(offset -> Optional.ofNullable(offset.getOffset()).map(LogSequenceNumber::valueOf)).orElse(LogSequenceNumber.valueOf("0/0"));
stream = replConnection.getReplicationAPI().replicationStream().logical().withSlotName(replicationSlotName).withSlotOption("include-xids", false).withStatusInterval(replicationStatusIntervalInMilliseconds, TimeUnit.MILLISECONDS).withStartPosition(lsn).start();
logger.info("connection to postgres wal succeed");
while (running) {
ByteBuffer messageBuffer = stream.readPending();
if (messageBuffer == null) {
logger.info("Got empty message, sleeping");
TimeUnit.MILLISECONDS.sleep(walIntervalInMilliseconds);
continue;
}
String messageString = extractStringFromBuffer(messageBuffer);
logger.info("Got message: " + messageString);
postgresWalMessageParser.parse(JSonMapper.fromJson(messageString, PostgresWalMessage.class), stream.getLastReceiveLSN().asLong(), replicationSlotName).forEach(eventConsumer);
stream.setAppliedLSN(stream.getLastReceiveLSN());
stream.setFlushedLSN(stream.getLastReceiveLSN());
}
try {
stream.close();
connection.close();
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
countDownLatchForStop.countDown();
}
use of io.eventuate.local.common.BinlogFileOffset in project eventuate-tram-core by eventuate-tram.
the class WriteRowsEventDataParser method parseEventData.
@Override
public MessageWithDestination parseEventData(WriteRowsEventData eventData, String binlogFilename, long position) throws IOException {
if (columnOrders.isEmpty()) {
try {
getColumnOrders();
} catch (SQLException e) {
logger.error("Error getting metadata", e);
throw new RuntimeException(e);
}
}
String id = (String) getValue(eventData, ID);
String destination = (String) getValue(eventData, DESTINATION);
String payload = (String) getValue(eventData, PAYLOAD);
Map<String, String> headers = JSonMapper.fromJson((String) getValue(eventData, HEADERS), Map.class);
headers.put(Message.ID, id);
headers.put("binlogfile", binlogFilename);
headers.put("binlogposition", Long.toString(position));
return new MessageWithDestination(destination, new MessageImpl(payload, headers), new BinlogFileOffset(binlogFilename, position));
}
Aggregations