Java Code Examples for org.postgresql.replication.PGReplicationStream

The following examples show how to use org.postgresql.replication.PGReplicationStream. These examples are extracted from open source projects. 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 Project: shardingsphere   Source File: PostgreSQLWalDumper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void dump(final Channel channel) {
    try {
        PGConnection pgConnection = logicalReplication.createPgConnection((JDBCDataSourceConfiguration) rdbmsConfiguration.getDataSourceConfiguration());
        decodingPlugin = new TestDecodingPlugin(((Connection) pgConnection).unwrap(PgConnection.class).getTimestampUtils());
        PGReplicationStream stream = logicalReplication.createReplicationStream(pgConnection,
                PostgreSQLLogPositionManager.SLOT_NAME, walPosition.getLogSequenceNumber());
        while (isRunning()) {
            ByteBuffer msg = stream.readPending();
            if (msg == null) {
                try {
                    Thread.sleep(10L);
                    continue;
                } catch (InterruptedException ignored) {
                
                }
            }
            AbstractWalEvent event = decodingPlugin.decode(msg, stream.getLastReceiveLSN());
            pushRecord(channel, walEventConverter.convert(event));
        }
    } catch (SQLException ex) {
        throw new SyncTaskExecuteException(ex);
    }
}
 
Example 2
Source Project: LogicalDecode   Source File: App.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void receiveChangesOccursBeforStartReplication() throws Exception {
    PGConnection pgConnection = (PGConnection) replicationConnection;

    LogSequenceNumber lsn = getCurrentLSN();

    Statement st = connection.createStatement();
    st.execute("insert into test_logical_table(name) values('previous value')");
    st.execute("insert into test_logical_table(name) values('previous value')");
    st.execute("insert into test_logical_table(name) values('previous value')");
    st.close();

    PGReplicationStream stream =
            pgConnection
                    .getReplicationAPI()
                    .replicationStream()
                    .logical()
                    .withSlotName(SLOT_NAME)
                    .withStartPosition(lsn)
                //    .withSlotOption("proto_version",1)
                //    .withSlotOption("publication_names", "pub1")
                   .withSlotOption("include-xids", true)
                //    .withSlotOption("skip-empty-xacts", true)
                    .withStatusInterval(10, TimeUnit.SECONDS)
                    .start();
    ByteBuffer buffer;
    while(true)
    {
        buffer = stream.readPending();
        if (buffer == null) {
            TimeUnit.MILLISECONDS.sleep(10L);
            continue;
        }

        System.out.println( toString(buffer));
        //feedback
        stream.setAppliedLSN(stream.getLastReceiveLSN());
        stream.setFlushedLSN(stream.getLastReceiveLSN());
    }

}
 
Example 3
Source Project: shardingsphere   Source File: LogicalReplication.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create PostgreSQL replication stream.
 *
 * @param pgConnection PostgreSQL connection
 * @param slotName slot name
 * @param startPosition start position
 * @return replication stream
 * @throws SQLException sql exception
 */
public PGReplicationStream createReplicationStream(final PGConnection pgConnection, final String slotName, final LogSequenceNumber startPosition) throws SQLException {
    return pgConnection.getReplicationAPI()
            .replicationStream()
            .logical()
            .withStartPosition(startPosition)
            .withSlotName(slotName)
            .withSlotOption("include-xids", true)
            .withSlotOption("skip-empty-xacts", true)
            .start();
}