use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.
the class BaseCanalServerWithEmbededTest method testGet.
@Test
public void testGet() {
int maxEmptyCount = 10;
int emptyCount = 0;
int totalCount = 0;
server.subscribe(clientIdentity);
while (emptyCount < maxEmptyCount) {
Message message = server.get(clientIdentity, 11);
if (CollectionUtils.isEmpty(message.getEntries())) {
emptyCount++;
try {
Thread.sleep(emptyCount * 300L);
} catch (InterruptedException e) {
Assert.fail();
}
System.out.println("empty count : " + emptyCount);
} else {
emptyCount = 0;
totalCount += message.getEntries().size();
}
}
System.out.println("!!!!!! testGet totalCount : " + totalCount);
server.unsubscribe(clientIdentity);
}
use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.
the class BaseCanalServerWithEmbededTest method testSwitch.
// @Test
public void testSwitch() {
int maxEmptyCount = 10;
int emptyCount = 0;
int totalCount = 0;
int thresold = 50;
int batchSize = 11;
server.subscribe(clientIdentity);
while (emptyCount < maxEmptyCount) {
Message message = server.get(clientIdentity, batchSize);
if (CollectionUtils.isEmpty(message.getEntries())) {
emptyCount++;
try {
Thread.sleep(emptyCount * 300L);
} catch (InterruptedException e) {
Assert.fail();
}
System.out.println("empty count : " + emptyCount);
} else {
emptyCount = 0;
totalCount += message.getEntries().size();
if ((totalCount + 1) % 100 >= thresold && (totalCount + 1) % 100 <= thresold + batchSize) {
CanalEventParser eventParser = server.getCanalInstances().get(DESTINATION).getEventParser();
if (eventParser instanceof CanalHASwitchable) {
// 执行切换
((CanalHASwitchable) eventParser).doSwitch();
try {
// 等待parser启动
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
Assert.fail();
}
}
}
}
}
System.out.println("!!!!!! testGet totalCount : " + totalCount);
server.unsubscribe(clientIdentity);
}
use of com.alibaba.otter.canal.protocol.Message in project java-example by 1479005017.
the class CanalMessageHandler method doRun.
public void doRun() throws CanalClientException {
while (true) {
Message message = connector.getWithoutAck(100);
if (message.getId() == -1 || message.getEntries().size() == 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
TransactionStatus transactionStatus = null;
if (transactionManager != null) {
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
transactionStatus = transactionManager.getTransaction(transactionDefinition);
}
try {
handleMessage(message);
if (transactionStatus != null)
transactionManager.commit(transactionStatus);
connector.ack(message.getId());
} catch (Exception e) {
e.printStackTrace();
if (transactionStatus != null)
transactionManager.rollback(transactionStatus);
connector.rollback(message.getId());
}
}
}
use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.
the class KafkaClientRunningTest method testKafkaConsumer.
public void testKafkaConsumer() {
final ExecutorService executor = Executors.newFixedThreadPool(1);
final KafkaCanalConnector connector = new KafkaCanalConnector(servers, topic, partition, groupId, null, false);
executor.submit(() -> {
connector.connect();
connector.subscribe();
while (running) {
List<Message> messages = connector.getList(3L, TimeUnit.SECONDS);
if (messages != null) {
System.out.println(messages);
}
connector.ack();
}
connector.unsubscribe();
connector.disconnect();
});
sleep(60000);
running = false;
executor.shutdown();
logger.info("shutdown completed");
}
use of com.alibaba.otter.canal.protocol.Message in project canal by alibaba.
the class CanalRocketMQClientExample method process.
private void process() {
while (!running) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
while (running) {
try {
connector.connect();
connector.subscribe();
while (running) {
// 获取message
List<Message> messages = connector.getListWithoutAck(1000L, TimeUnit.MILLISECONDS);
for (Message message : messages) {
long batchId = message.getId();
int size = message.getEntries().size();
if (batchId == -1 || size == 0) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// }
} else {
printSummary(message, batchId, size);
printEntry(message.getEntries());
// logger.info(message.toString());
}
}
// 提交确认
connector.ack();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
connector.unsubscribe();
// connector.stopRunning();
}
Aggregations