本文整理匯總了Java中com.alibaba.otter.canal.instance.core.CanalInstance類的典型用法代碼示例。如果您正苦於以下問題:Java CanalInstance類的具體用法?Java CanalInstance怎麽用?Java CanalInstance使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CanalInstance類屬於com.alibaba.otter.canal.instance.core包,在下文中一共展示了CanalInstance類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: stop
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
public void stop() {
super.stop();
for (Map.Entry<String, CanalInstance> entry : canalInstances.entrySet()) {
try {
CanalInstance instance = entry.getValue();
if (instance.isStart()) {
try {
String destination = entry.getKey();
MDC.put("destination", destination);
entry.getValue().stop();
logger.info("stop CanalInstances[{}] successfully", destination);
} finally {
MDC.remove("destination");
}
}
} catch (Exception e) {
logger.error(String.format("stop CanalInstance[%s] has an error", entry.getKey()), e);
}
}
}
示例2: subscribe
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 客戶端訂閱,重複訂閱時會更新對應的filter信息
*/
@Override
public void subscribe(ClientIdentity clientIdentity) throws CanalServerException {
checkStart(clientIdentity.getDestination());
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
if (!canalInstance.getMetaManager().isStart()) {
canalInstance.getMetaManager().start();
}
canalInstance.getMetaManager().subscribe(clientIdentity); // 執行一下meta訂閱
Position position = canalInstance.getMetaManager().getCursor(clientIdentity);
if (position == null) {
position = canalInstance.getEventStore().getFirstPosition();// 獲取一下store中的第一條
if (position != null) {
canalInstance.getMetaManager().updateCursor(clientIdentity, position); // 更新一下cursor
}
logger.info("subscribe successfully, {} with first position:{} ", clientIdentity, position);
} else {
logger.info("subscribe successfully, use last cursor position:{} ", clientIdentity, position);
}
// 通知下訂閱關係變化
canalInstance.subscribeChange(clientIdentity);
}
示例3: rollback
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 回滾到未進行 {@link #ack} 的地方,下次fetch的時候,可以從最後一個沒有 {@link #ack} 的地方開始拿
*/
@Override
public void rollback(ClientIdentity clientIdentity) throws CanalServerException {
checkStart(clientIdentity.getDestination());
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
// 因為存在第一次鏈接時自動rollback的情況,所以需要忽略未訂閱
boolean hasSubscribe = canalInstance.getMetaManager().hasSubscribe(clientIdentity);
if (!hasSubscribe) {
return;
}
synchronized (canalInstance) {
// 清除batch信息
canalInstance.getMetaManager().clearAllBatchs(clientIdentity);
// rollback eventStore中的狀態信息
canalInstance.getEventStore().rollback();
logger.info("rollback successfully, clientId:{}", new Object[] { clientIdentity.getClientId() });
}
}
示例4: setUp
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
@Before
public void setUp() {
CanalServerWithEmbedded embeddedServer = new CanalServerWithEmbedded();
embeddedServer.setCanalInstanceGenerator(new CanalInstanceGenerator() {
public CanalInstance generate(String destination) {
Canal canal = buildCanal();
return new CanalInstanceWithManager(canal, FILTER);
}
});
nettyServer = CanalServerWithNetty.instance();
nettyServer.setEmbeddedServer(embeddedServer);
nettyServer.setPort(1088);
nettyServer.start();
}
示例5: generate
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
public CanalInstance generate(String destination) {
String beanName = destination;
if (!beanFactory.containsBean(beanName)) {
beanName = defaultName;
}
return (CanalInstance) beanFactory.getBean(beanName);
}
示例6: testInstance
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
@Test
public void testInstance() {
CanalInstanceGenerator generator = (CanalInstanceGenerator) context.getBean("canalInstanceGenerator");
CanalInstance canalInstance = generator.generate("instance");
Assert.notNull(canalInstance);
canalInstance.start();
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
}
canalInstance.stop();
}
示例7: start
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
public void start() {
if (!isStart()) {
super.start();
canalInstances = MigrateMap.makeComputingMap(new Function<String, CanalInstance>() {
public CanalInstance apply(String destination) {
return canalInstanceGenerator.generate(destination);
}
});
// lastRollbackPostions = new MapMaker().makeMap();
}
}
示例8: unsubscribe
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 取消訂閱
*/
@Override
public void unsubscribe(ClientIdentity clientIdentity) throws CanalServerException {
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
canalInstance.getMetaManager().unsubscribe(clientIdentity); // 執行一下meta訂閱
logger.info("unsubscribe successfully, {}", clientIdentity);
}
示例9: listBatchIds
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 查詢當前未被ack的batch列表,batchId會按照從小到大進行返回
*/
public List<Long> listBatchIds(ClientIdentity clientIdentity) throws CanalServerException {
checkStart(clientIdentity.getDestination());
checkSubscribe(clientIdentity);
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
Map<Long, PositionRange> batchs = canalInstance.getMetaManager().listAllBatchs(clientIdentity);
List<Long> result = new ArrayList<Long>(batchs.keySet());
Collections.sort(result);
return result;
}
示例10: checkSubscribe
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
private void checkSubscribe(ClientIdentity clientIdentity) {
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
boolean hasSubscribe = canalInstance.getMetaManager().hasSubscribe(clientIdentity);
if (!hasSubscribe) {
throw new CanalServerException(String.format("ClientIdentity:%s should subscribe first",
clientIdentity.toString()));
}
}
示例11: setUp
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
@Before
public void setUp() {
server = CanalServerWithEmbedded.instance();
server.setCanalInstanceGenerator(new CanalInstanceGenerator() {
public CanalInstance generate(String destination) {
Canal canal = buildCanal();
return new CanalInstanceWithManager(canal, FILTER);
}
});
server.start();
server.start(DESTINATION);
}
示例12: generate
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
public CanalInstance generate(String destination) {
Canal canal = canalConfigClient.findCanal(destination);
String filter = canalConfigClient.findFilter(destination);
return new CanalInstanceWithManager(canal, filter);
}
示例13: listAllSubscribe
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 查詢所有的訂閱信息
*/
public List<ClientIdentity> listAllSubscribe(String destination) throws CanalServerException {
CanalInstance canalInstance = canalInstances.get(destination);
return canalInstance.getMetaManager().listAllSubscribeInfo(destination);
}
示例14: get
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 獲取數據,可以指定超時時間.
*
* <pre>
* 幾種case:
* a. 如果timeout為null,則采用tryGet方式,即時獲取
* b. 如果timeout不為null
* 1. timeout為0,則采用get阻塞方式,獲取數據,不設置超時,直到有足夠的batchSize數據才返回
* 2. timeout不為0,則采用get+timeout方式,獲取數據,超時還沒有batchSize足夠的數據,有多少返回多少
*
* 注意: meta獲取和數據的獲取需要保證順序性,優先拿到meta的,一定也會是優先拿到數據,所以需要加同步. (不能出現先拿到meta,拿到第二批數據,這樣就會導致數據順序性出現問題)
* </pre>
*/
@Override
public Message get(ClientIdentity clientIdentity, int batchSize, Long timeout, TimeUnit unit)
throws CanalServerException {
checkStart(clientIdentity.getDestination());
checkSubscribe(clientIdentity);
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
synchronized (canalInstance) {
// 獲取到流式數據中的最後一批獲取的位置
PositionRange<LogPosition> positionRanges = canalInstance.getMetaManager().getLastestBatch(clientIdentity);
if (positionRanges != null) {
throw new CanalServerException(String.format("clientId:%s has last batch:[%s] isn't ack , maybe loss data",
clientIdentity.getClientId(),
positionRanges));
}
Events<Event> events = null;
Position start = canalInstance.getMetaManager().getCursor(clientIdentity);
events = getEvents(canalInstance.getEventStore(), start, batchSize, timeout, unit);
if (CollectionUtils.isEmpty(events.getEvents())) {
logger.debug("get successfully, clientId:{} batchSize:{} but result is null",
clientIdentity.getClientId(), batchSize);
return new Message(-1, new ArrayList<Entry>()); // 返回空包,避免生成batchId,浪費性能
} else {
// 記錄到流式信息
Long batchId = canalInstance.getMetaManager().addBatch(clientIdentity, events.getPositionRange());
List<Entry> entrys = Lists.transform(events.getEvents(), new Function<Event, Entry>() {
public Entry apply(Event input) {
return input.getEntry();
}
});
if (logger.isInfoEnabled()) {
logger.info("get successfully, clientId:{} batchSize:{} real size is {} and result is [batchId:{} , position:{}]",
clientIdentity.getClientId(),
batchSize,
entrys.size(),
batchId,
events.getPositionRange());
}
// 直接提交ack
ack(clientIdentity, batchId);
return new Message(batchId, entrys);
}
}
}
示例15: getWithoutAck
import com.alibaba.otter.canal.instance.core.CanalInstance; //導入依賴的package包/類
/**
* 不指定 position 獲取事件。canal 會記住此 client 最新的 position。 <br/>
* 如果是第一次 fetch,則會從 canal 中保存的最老一條數據開始輸出。
*
* <pre>
* 幾種case:
* a. 如果timeout為null,則采用tryGet方式,即時獲取
* b. 如果timeout不為null
* 1. timeout為0,則采用get阻塞方式,獲取數據,不設置超時,直到有足夠的batchSize數據才返回
* 2. timeout不為0,則采用get+timeout方式,獲取數據,超時還沒有batchSize足夠的數據,有多少返回多少
*
* 注意: meta獲取和數據的獲取需要保證順序性,優先拿到meta的,一定也會是優先拿到數據,所以需要加同步. (不能出現先拿到meta,拿到第二批數據,這樣就會導致數據順序性出現問題)
* </pre>
*/
@Override
public Message getWithoutAck(ClientIdentity clientIdentity, int batchSize, Long timeout, TimeUnit unit)
throws CanalServerException {
checkStart(clientIdentity.getDestination());
checkSubscribe(clientIdentity);
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
synchronized (canalInstance) {
// 獲取到流式數據中的最後一批獲取的位置
PositionRange<LogPosition> positionRanges = canalInstance.getMetaManager().getLastestBatch(clientIdentity);
Events<Event> events = null;
if (positionRanges != null) { // 存在流數據
events = getEvents(canalInstance.getEventStore(), positionRanges.getStart(), batchSize, timeout, unit);
} else {// ack後第一次獲取
Position start = canalInstance.getMetaManager().getCursor(clientIdentity);
if (start == null) { // 第一次,還沒有過ack記錄,則獲取當前store中的第一條
start = canalInstance.getEventStore().getFirstPosition();
}
events = getEvents(canalInstance.getEventStore(), start, batchSize, timeout, unit);
}
if (CollectionUtils.isEmpty(events.getEvents())) {
logger.debug("getWithoutAck successfully, clientId:{} batchSize:{} but result is null",
clientIdentity.getClientId(), batchSize);
return new Message(-1, new ArrayList<Entry>()); // 返回空包,避免生成batchId,浪費性能
} else {
// 記錄到流式信息
Long batchId = canalInstance.getMetaManager().addBatch(clientIdentity, events.getPositionRange());
List<Entry> entrys = Lists.transform(events.getEvents(), new Function<Event, Entry>() {
public Entry apply(Event input) {
return input.getEntry();
}
});
if (logger.isInfoEnabled()) {
logger.info("getWithoutAck successfully, clientId:{} batchSize:{} real size is {} and result is [batchId:{} , position:{}]",
clientIdentity.getClientId(),
batchSize,
entrys.size(),
batchId,
events.getPositionRange());
}
return new Message(batchId, entrys);
}
}
}