com.alibaba.otter.canal.common.utils.BooleanMutex Java Examples

The following examples show how to use com.alibaba.otter.canal.common.utils.BooleanMutex. 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 File: SyncSwitch.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
private synchronized void initMutex(CuratorFramework curator, String destination, BooleanMutex mutex) {
    try {
        String path = SYN_SWITCH_ZK_NODE + destination;
        Stat stat = curator.checkExists().forPath(path);
        if (stat == null) {
            if (!mutex.state()) {
                mutex.set(true);
            }
        } else {
            String data = new String(curator.getData().forPath(path), StandardCharsets.UTF_8);
            if ("on".equals(data)) {
                if (!mutex.state()) {
                    mutex.set(true);
                }
            } else {
                if (mutex.state()) {
                    mutex.set(false);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #2
Source File: SyncSwitch.java    From canal with Apache License 2.0 6 votes vote down vote up
public synchronized void refresh() {
    for (String destination : adapterCanalConfig.DESTINATIONS) {
        BooleanMutex booleanMutex;
        if (mode == Mode.DISTRIBUTED) {
            CuratorFramework curator = curatorClient.getCurator();
            booleanMutex = DISTRIBUTED_LOCK.get(destination);
            if (booleanMutex == null) {
                BooleanMutex mutex = new BooleanMutex(true);
                initMutex(curator, destination, mutex);
                DISTRIBUTED_LOCK.put(destination, mutex);
                startListen(destination, mutex);
            }
        } else {
            booleanMutex = LOCAL_LOCK.get(destination);
            if (booleanMutex == null) {
                LOCAL_LOCK.put(destination, new BooleanMutex(true));
            }
        }
    }
}
 
Example #3
Source File: SyncSwitch.java    From canal with Apache License 2.0 6 votes vote down vote up
private synchronized void initMutex(CuratorFramework curator, String destination, BooleanMutex mutex) {
    try {
        String path = SYN_SWITCH_ZK_NODE + destination;
        Stat stat = curator.checkExists().forPath(path);
        if (stat == null) {
            if (!mutex.state()) {
                mutex.set(true);
            }
        } else {
            String data = new String(curator.getData().forPath(path), StandardCharsets.UTF_8);
            if ("on".equals(data)) {
                if (!mutex.state()) {
                    mutex.set(true);
                }
            } else {
                if (mutex.state()) {
                    mutex.set(false);
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #4
Source File: SyncSwitch.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("resource")
private synchronized void startListen(String destination, BooleanMutex mutex) {
    try {
        String path = SYN_SWITCH_ZK_NODE + destination;
        CuratorFramework curator = curatorClient.getCurator();
        NodeCache nodeCache = new NodeCache(curator, path);
        nodeCache.start();
        nodeCache.getListenable().addListener(() -> initMutex(curator, destination, mutex));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #5
Source File: SyncSwitch.java    From canal with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("resource")
private synchronized void startListen(String destination, BooleanMutex mutex) {
    try {
        String path = SYN_SWITCH_ZK_NODE + destination;
        CuratorFramework curator = curatorClient.getCurator();
        NodeCache nodeCache = new NodeCache(curator, path);
        nodeCache.start();
        nodeCache.getListenable().addListener(() -> initMutex(curator, destination, mutex));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example #6
Source File: MemoryEventStoreMultiThreadTest.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public Producer(BooleanMutex mutex, int freq){
    this.mutex = mutex;
    this.freq = freq;
}
 
Example #7
Source File: RollBackMonitorFactory.java    From canal-elasticsearch with Apache License 2.0 4 votes vote down vote up
public static BooleanMutex getBooleanMutex() {
    return booleanMutex;
}
 
Example #8
Source File: MemoryEventStoreMultiThreadTest.java    From canal with Apache License 2.0 4 votes vote down vote up
public Producer(BooleanMutex mutex, int freq){
    this.mutex = mutex;
    this.freq = freq;
}