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();
}
}
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启动完毕.");
}
}
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);
}
}
Aggregations