Search in sources :

Example 1 with TransactionFlushCallback

use of com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback in project canal by alibaba.

the class EventTransactionBufferTest method testForceFlush.

@Test
public void testForceFlush() {
    final int bufferSize = 64;
    EventTransactionBuffer buffer = new EventTransactionBuffer();
    buffer.setBufferSize(bufferSize);
    buffer.setFlushCallback(new TransactionFlushCallback() {

        public void flush(List<Entry> transaction) throws InterruptedException {
            Assert.assertEquals(bufferSize, transaction.size());
            System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            for (Entry data : transaction) {
                CanalEntry.Header header = data.getHeader();
                Date date = new Date(header.getExecuteTime());
                SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
                if (data.getEntryType() == EntryType.TRANSACTIONBEGIN || data.getEntryType() == EntryType.TRANSACTIONEND) {
                    // System.out.println(MessageFormat.format(messgae, new
                    // Object[] {
                    // Thread.currentThread().getName(),
                    // header.getLogfilename(), header.getLogfileoffset(),
                    // format.format(date),
                    // data.getEntry().getEntryType(), "" }));
                    System.out.println(data.getEntryType());
                } else {
                    System.out.println(MessageFormat.format(messgae, new Object[] { Thread.currentThread().getName(), header.getLogfileName(), header.getLogfileOffset(), format.format(date), header.getSchemaName(), header.getTableName() }));
                }
            }
            System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
        }
    });
    buffer.start();
    try {
        for (int i = 0; i < bufferSize * 2 + 1; i++) {
            buffer.add(buildEntry("1", 1L + i, 40L + i));
        }
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    buffer.stop();
}
Also used : CanalEntry(com.alibaba.otter.canal.protocol.CanalEntry) Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry) Header(com.alibaba.otter.canal.protocol.CanalEntry.Header) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) TransactionFlushCallback(com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback) Test(org.junit.Test)

Example 2 with TransactionFlushCallback

use of com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback in project canal by alibaba.

the class EventTransactionBufferTest method testTransactionFlush.

@Test
public void testTransactionFlush() {
    final int bufferSize = 64;
    final int transactionSize = 5;
    EventTransactionBuffer buffer = new EventTransactionBuffer();
    buffer.setBufferSize(bufferSize);
    buffer.setFlushCallback(new TransactionFlushCallback() {

        public void flush(List<Entry> transaction) throws InterruptedException {
            Assert.assertEquals(transactionSize, transaction.size());
            System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            for (Entry data : transaction) {
                CanalEntry.Header header = data.getHeader();
                Date date = new Date(header.getExecuteTime());
                SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
                if (data.getEntryType() == EntryType.TRANSACTIONBEGIN || data.getEntryType() == EntryType.TRANSACTIONEND) {
                    System.out.println(data.getEntryType());
                } else {
                    System.out.println(MessageFormat.format(messgae, new Object[] { Thread.currentThread().getName(), header.getLogfileName(), header.getLogfileOffset(), format.format(date), header.getSchemaName(), header.getTableName() }));
                }
            }
            System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
        }
    });
    buffer.start();
    try {
        for (int i = 0; i < transactionSize * 10; i++) {
            if (i % transactionSize == 0) {
                buffer.add(buildEntry("1", 1L + i, 40L + i, EntryType.TRANSACTIONBEGIN));
            } else if ((i + 1) % transactionSize == 0) {
                buffer.add(buildEntry("1", 1L + i, 40L + i, EntryType.TRANSACTIONEND));
            } else {
                buffer.add(buildEntry("1", 1L + i, 40L + i));
            }
        }
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    buffer.stop();
}
Also used : CanalEntry(com.alibaba.otter.canal.protocol.CanalEntry) Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry) Header(com.alibaba.otter.canal.protocol.CanalEntry.Header) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) TransactionFlushCallback(com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback) Test(org.junit.Test)

Aggregations

TransactionFlushCallback (com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback)2 CanalEntry (com.alibaba.otter.canal.protocol.CanalEntry)2 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)2 Header (com.alibaba.otter.canal.protocol.CanalEntry.Header)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Test (org.junit.Test)2