Search in sources :

Example 1 with LeaderSelectorListenerAdapter

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter in project rocketmq-externals by apache.

the class RocketMQRedisReplicator method open.

@Override
public void open() throws IOException {
    if (this.replicator instanceof RedisSocketReplicator) {
        boolean single = configure.getString(DEPLOY_MODEL).equals(DEPLOY_MODEL_SINGLE);
        if (single) {
            this.replicator.open();
        } else {
            String address = configure.getString(CONFIG_PROP_ZK_ADDRESS);
            final CuratorFramework client = newClient(address, new ExponentialBackoffRetry(1000, 3));
            client.start();
            String path = ROOT_DIR + "/" + uri.getHost() + "-" + uri.getPort();
            final LeaderSelector selector = new LeaderSelector(client, path, new LeaderSelectorListenerAdapter() {

                @Override
                public void takeLeadership(final CuratorFramework client) throws Exception {
                    RocketMQRedisReplicator.this.addCloseListener(new CloseListener() {

                        @Override
                        public void handle(Replicator replicator) {
                            client.close();
                        }
                    });
                    replicator.open();
                }
            });
            selector.start();
        }
    } else {
        this.replicator.open();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 2 with LeaderSelectorListenerAdapter

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter in project xian by happyyangyuan.

the class ZkLeaderElection method start.

/**
 * 启动主节点选举
 */
public static void start() {
    synchronized (lock) {
        if (singleton != null)
            return;
        ZkLeaderElection leaderElection = new ZkLeaderElection();
        LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {

            public void takeLeadership(CuratorFramework client) throws InterruptedException {
                LOG.info("被提升为主节点!");
                try {
                    Thread.sleep(Long.MAX_VALUE);
                } catch (InterruptedException e) {
                    LOG.info("节点主动断开 or  主动让出主节点身份?反正此处是预期的打断!不需要打印堆栈");
                } finally {
                    LOG.info("主节点降级为普通节点!");
                }
            }
        };
        leaderElection.leaderSelector = new LeaderSelector(ZkConnection.client, ZkPathManager.getMyNodeBasePath(), listener);
        // not required, but this is behavior that you will probably expect
        leaderElection.leaderSelector.autoRequeue();
        leaderElection.leaderSelector.start();
        singleton = leaderElection;
        LOG.info("ZkLeaderElection启动完毕.");
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)

Example 3 with LeaderSelectorListenerAdapter

use of org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter in project xian by happyyangyuan.

the class TestResetConnectionWithBackgroundFailure method testConnectionStateListener.

@Test
public void testConnectionStateListener() throws Exception {
    server.stop();
    LeaderSelector selector = null;
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        timing.sleepABit();
        LeaderSelectorListener listenerLeader = new LeaderSelectorListenerAdapter() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                Thread.currentThread().join();
            }
        };
        selector = new LeaderSelector(client, "/leader", listenerLeader);
        selector.autoRequeue();
        selector.start();
        final BlockingQueue<ConnectionState> listenerSequence = Queues.newLinkedBlockingQueue();
        ConnectionStateListener listener1 = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                listenerSequence.add(newState);
            }
        };
        Timing forWaiting = timing.forWaiting();
        client.getConnectionStateListenable().addListener(listener1);
        log.debug("Starting ZK server");
        server.restart();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
        log.debug("Stopping ZK server");
        server.stop();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
        log.debug("Starting ZK server");
        server.restart();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
        log.debug("Stopping ZK server");
        server.close();
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
        Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) Timing(org.apache.curator.test.Timing) LeaderSelectorListenerAdapter(org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)3 LeaderSelector (org.apache.curator.framework.recipes.leader.LeaderSelector)3 LeaderSelectorListenerAdapter (org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter)3 LeaderSelectorListener (org.apache.curator.framework.recipes.leader.LeaderSelectorListener)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ConnectionState (org.apache.curator.framework.state.ConnectionState)1 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)1 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)1 RetryOneTime (org.apache.curator.retry.RetryOneTime)1 Timing (org.apache.curator.test.Timing)1 Test (org.testng.annotations.Test)1