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

The following examples show how to use com.alibaba.otter.canal.common.utils.AddressUtils. 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: SimpleCanalClientTest.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
        // 根据ip,直接创建链接,无HA的功能
        String destination = "example";
        String ip = AddressUtils.getHostIp();
//        创建单机canal connector
        CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(ip, 11111),
            destination,
            "",
            "");

        final SimpleCanalClientTest clientTest = new SimpleCanalClientTest(destination);
        clientTest.setConnector(connector);
        clientTest.start();
        Runtime.getRuntime().addShutdownHook(new Thread() {

            public void run() {
                try {
                    logger.info("## stop the canal client");
                    clientTest.stop();
                } catch (Throwable e) {
                    logger.warn("##something goes wrong when stopping canal:", e);
                } finally {
                    logger.info("## canal client is down.");
                }
            }

        });
    }
 
Example #2
Source File: SimpleCanalConnector.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private synchronized void initClientRunningMonitor(ClientIdentity clientIdentity) {
        if (zkClientx != null && clientIdentity != null && runningMonitor == null) {
            ClientRunningData clientData = new ClientRunningData();
            clientData.setClientId(clientIdentity.getClientId());
            clientData.setAddress(AddressUtils.getHostIp());

//            创建client运行监听器
            runningMonitor = new ClientRunningMonitor();
            runningMonitor.setDestination(clientIdentity.getDestination());
            runningMonitor.setZkClient(zkClientx);
            runningMonitor.setClientData(clientData);
            runningMonitor.setListener(new ClientRunningListener() {

                public InetSocketAddress processActiveEnter() {
                    InetSocketAddress address = doConnect();
                    mutex.set(true);
                    if (filter != null) { // 如果存在条件,说明是自动切换,基于上一次的条件订阅一次
                        subscribe(filter);
                    }

                    if (rollbackOnConnect) {
                        rollback();
                    }

                    return address;
                }

                public void processActiveExit() {
                    mutex.set(false);
                    doDisconnect();
                }

            });
        }
    }
 
Example #3
Source File: ClientRunningTest.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private ClientRunningMonitor buildClientRunning(final CountDownLatch countLatch, final short clientId,
                                                final int port) {
    ClientRunningData clientData = new ClientRunningData();
    clientData.setClientId(clientId);
    clientData.setAddress(AddressUtils.getHostIp());

    ClientRunningMonitor runningMonitor = new ClientRunningMonitor();
    runningMonitor.setDestination(destination);
    runningMonitor.setZkClient(zkclientx);
    runningMonitor.setClientData(clientData);
    runningMonitor.setListener(new ClientRunningListener() {

        public InetSocketAddress processActiveEnter() {
            System.out.println(String.format("clientId:%s port:%s has start", clientId, port));
            countLatch.countDown();
            return new InetSocketAddress(AddressUtils.getHostIp(), port);
        }

        public void processActiveExit() {
            countLatch.countDown();
            System.out.println(String.format("clientId:%s port:%s has stop", clientId, port));
        }

    });
    runningMonitor.setDelayTime(1);
    return runningMonitor;
}
 
Example #4
Source File: SimpleCanalClientTest.java    From canal with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    // 根据ip,直接创建链接,无HA的功能
    String destination = "example";
    String ip = AddressUtils.getHostIp();
    CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(ip, 11111),
            destination,
            "canal",
            "canal");

    final SimpleCanalClientTest clientTest = new SimpleCanalClientTest(destination);
    clientTest.setConnector(connector);
    clientTest.start();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            try {
                logger.info("## stop the canal client");
                clientTest.stop();
            } catch (Throwable e) {
                logger.warn("##something goes wrong when stopping canal:", e);
            } finally {
                logger.info("## canal client is down.");
            }
        }

    });
}
 
Example #5
Source File: SimpleCanalConnector.java    From canal with Apache License 2.0 5 votes vote down vote up
private synchronized void initClientRunningMonitor(ClientIdentity clientIdentity) {
    if (zkClientx != null && clientIdentity != null && runningMonitor == null) {
        ClientRunningData clientData = new ClientRunningData();
        clientData.setClientId(clientIdentity.getClientId());
        clientData.setAddress(AddressUtils.getHostIp());

        runningMonitor = new ClientRunningMonitor();
        runningMonitor.setDestination(clientIdentity.getDestination());
        runningMonitor.setZkClient(zkClientx);
        runningMonitor.setClientData(clientData);
        runningMonitor.setListener(new ClientRunningListener() {

            public InetSocketAddress processActiveEnter() {
                InetSocketAddress address = doConnect();
                mutex.set(true);
                if (filter != null) { // 如果存在条件,说明是自动切换,基于上一次的条件订阅一次
                    subscribe(filter);
                }

                if (rollbackOnConnect) {
                    rollback();
                }

                return address;
            }

            public void processActiveExit() {
                mutex.set(false);
                doDisconnect();
            }

        });
    }
}
 
Example #6
Source File: ClientRunningTest.java    From canal with Apache License 2.0 5 votes vote down vote up
private ClientRunningMonitor buildClientRunning(final CountDownLatch countLatch, final short clientId,
                                                final int port) {
    ClientRunningData clientData = new ClientRunningData();
    clientData.setClientId(clientId);
    clientData.setAddress(AddressUtils.getHostIp());

    ClientRunningMonitor runningMonitor = new ClientRunningMonitor();
    runningMonitor.setDestination(destination);
    runningMonitor.setZkClient(zkclientx);
    runningMonitor.setClientData(clientData);
    runningMonitor.setListener(new ClientRunningListener() {

        public InetSocketAddress processActiveEnter() {
            System.out.println(String.format("clientId:%s port:%s has start", clientId, port));
            countLatch.countDown();
            return new InetSocketAddress(AddressUtils.getHostIp(), port);
        }

        public void processActiveExit() {
            countLatch.countDown();
            System.out.println(String.format("clientId:%s port:%s has stop", clientId, port));
        }

    });
    runningMonitor.setDelayTime(1);
    return runningMonitor;
}