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

The following examples show how to use com.alibaba.otter.canal.common.utils.BooleanMutex. 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: canal-1.1.3   Source File: SyncSwitch.java    License: 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 Project: canal   Source File: SyncSwitch.java    License: 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 Project: canal   Source File: SyncSwitch.java    License: 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 Project: canal-1.1.3   Source File: SyncSwitch.java    License: 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 Project: canal   Source File: SyncSwitch.java    License: 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
public Producer(BooleanMutex mutex, int freq){
    this.mutex = mutex;
    this.freq = freq;
}
 
Example 7
public static BooleanMutex getBooleanMutex() {
    return booleanMutex;
}
 
Example 8
Source Project: canal   Source File: MemoryEventStoreMultiThreadTest.java    License: Apache License 2.0 4 votes vote down vote up
public Producer(BooleanMutex mutex, int freq){
    this.mutex = mutex;
    this.freq = freq;
}