本文整理汇总了Java中org.I0Itec.zkclient.ZkClient.close方法的典型用法代码示例。如果您正苦于以下问题:Java ZkClient.close方法的具体用法?Java ZkClient.close怎么用?Java ZkClient.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.I0Itec.zkclient.ZkClient
的用法示例。
在下文中一共展示了ZkClient.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createTopic
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(final String topic,
final int partitions,
final int replication,
final Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
final ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例2: createTopic
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* Create a Kafka topic with the given parameters.
*
* @param topic The name of the topic.
* @param partitions The number of partitions for this topic.
* @param replication The replication factor for (partitions of) this topic.
* @param topicConfig Additional topic-level configuration settings.
*/
public void createTopic(String topic,
int partitions,
int replication,
Properties topicConfig) {
log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
topic, partitions, replication, topicConfig);
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopic() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
boolean isSecure = false;
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
zkClient.close();
}
示例3: close
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void close() {
closed = true;
// 停止心跳
this.heartbeatStop();
// 关闭KafkaProuder
if (LazySingletonProducer.isInstanced()) {
// producer实际上已经初始化
LazySingletonProducer.getInstance(this.config).close();
}
// 关闭client,临时节点消失,监控系统进行感知报警
ZkClient client = this.zkRegister == null ? null : this.zkRegister.getClient();
if (null != client) {
client.close();
}
}
示例4: stop
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void stop() {
super.stop();
// 停止心跳
this.heartbeatStop();
// 关闭KafkaProuder
if (LazySingletonProducer.isInstanced()) {
// producer实际上已经初始化
LazySingletonProducer.getInstance(this.config).close();
}
// 关闭client,临时节点消失,监控系统进行感知报警
ZkClient client = this.zkRegister == null ? null : this.zkRegister.getClient();
if (null != client) {
client.close();
}
}
示例5: startServer
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public static KafkaServerStartable startServer(final int port, final int brokerId,
final String zkStr, final Properties configuration) {
// Create the ZK nodes for Kafka, if needed
int indexOfFirstSlash = zkStr.indexOf('/');
if (indexOfFirstSlash != -1) {
String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
String zkNodePath = zkStr.substring(indexOfFirstSlash);
ZkClient client = new ZkClient(bareZkUrl);
client.createPersistent(zkNodePath, true);
client.close();
}
File logDir = new File("/tmp/kafka-" + Double.toHexString(Math.random()));
logDir.mkdirs();
configureKafkaPort(configuration, port);
configureZkConnectionString(configuration, zkStr);
configureBrokerId(configuration, brokerId);
configureKafkaLogDirectory(configuration, logDir);
KafkaConfig config = new KafkaConfig(configuration);
KafkaServerStartable serverStartable = new KafkaServerStartable(config);
serverStartable.startup();
return serverStartable;
}
示例6: createTopic
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* Creates a Topic.
*
* @param topicName Topic name.
* @param partitions Number of partitions for the topic.
* @param replicationFactor Replication factor.
* @param curatorFramework CuratorFramework.
*/
public static void createTopic(String topicName, int partitions, int replicationFactor, CuratorFramework curatorFramework) {
if (partitions <= 0)
throw new AdminOperationException("number of partitions must be larger than 0");
if (replicationFactor <= 0)
throw new AdminOperationException("replication factor must be larger than 0");
if (!topicExists(topicName, curatorFramework)) {
m_logger.info(String.format("Topic %s not found, creating...", topicName));
ZkClient zkClient = fromCurator(curatorFramework);
try {
AdminUtils.createTopic(zkClient, topicName, partitions, replicationFactor, new Properties());
m_logger.info("Topic created. name: {}, partitions: {}, replicationFactor: {}", topicName,
partitions, replicationFactor);
} catch (TopicExistsException ignore) {
m_logger.info("Topic exists. name: {}", topicName);
} finally {
if (zkClient != null) {
zkClient.close();
}
}
} else {
m_logger.info(String.format("Topic %s found!", topicName));
}
}
示例7: getLeaderToShutDown
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public int getLeaderToShutDown(String topic) throws Exception {
ZkClient zkClient = createZkClient();
PartitionMetadata firstPart = null;
do {
if (firstPart != null) {
LOG.info("Unable to find leader. error code {}", firstPart.errorCode());
// not the first try. Sleep a bit
Thread.sleep(150);
}
Seq<PartitionMetadata> partitionMetadata = AdminUtils.fetchTopicMetadataFromZk(topic, zkClient).partitionsMetadata();
firstPart = partitionMetadata.head();
}
while (firstPart.errorCode() != 0);
zkClient.close();
return firstPart.leader().get().id();
}
示例8: deleteTestTopic
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void deleteTestTopic(String topic) {
ZkUtils zkUtils = getZkUtils();
try {
LOG.info("Deleting topic {}", topic);
ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());
AdminUtils.deleteTopic(zkUtils, topic);
zk.close();
} finally {
zkUtils.close();
}
}
示例9: assertConnect
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
private void assertConnect(RegistryConfig registryConfig) {
try {
ZkClient zk = new ZkClient(registryConfig.getRegistryAddress(), registryConfig.getRegistryTimeout());
zk.close();
} catch (Exception e) {
throw new RuntimeException("注册中心 " + registryConfig.getRegistryAddress() + "无法连接", e);
}
}
示例10: deleteTopic
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public void deleteTopic(final String topic) {
log.debug("Deleting topic { name: {} }", topic);
final ZkClient zkClient = new ZkClient(
zookeeperConnect(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), isSecure);
AdminUtils.deleteTopic(zkUtils, topic);
zkClient.close();
}
示例11: getTopicConfigProperties
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
private Properties getTopicConfigProperties(final String changelog) {
// Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then
// createTopics() will only seem to work (it will return without error). The topic will exist in
// only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the
// topic.
final ZkClient zkClient = new ZkClient(
CLUSTER.zKConnectString(),
DEFAULT_ZK_SESSION_TIMEOUT_MS,
DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
ZKStringSerializer$.MODULE$);
try {
final boolean isSecure = false;
final ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(CLUSTER.zKConnectString()), isSecure);
final Map<String, Properties> topicConfigs = AdminUtils.fetchAllTopicConfigs(zkUtils);
final Iterator it = topicConfigs.iterator();
while (it.hasNext()) {
final Tuple2<String, Properties> topicConfig = (Tuple2<String, Properties>) it.next();
final String topic = topicConfig._1;
final Properties prop = topicConfig._2;
if (topic.equals(changelog)) {
return prop;
}
}
return new Properties();
} finally {
zkClient.close();
}
}
示例12: closeResources
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* 关闭zk和kafka、心跳检测
*/
public void closeResources() {
if (null != this.timer) {
this.timer.cancel();
}
if (LazySingletonProducer.isInstanced()) {
LazySingletonProducer.getInstance(KafkaManager.this.config).close();
}
ZkClient client = this.zkRegister == null ? null : this.zkRegister.getClient();
if (null != client) {
client.close();
}
}
示例13: validate
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void validate() throws ValidationException {
super.validate();
if (zkConnect == null || zkConnect.isEmpty())
throw new ValidationException(LOGGER.translate("ZKCONNECT_VALIDATE_ERROR"));
if (bootstrap == null || bootstrap.isEmpty())
throw new ValidationException(LOGGER.translate("BOOTSTRAP_VALIDATE_ERROR"));
if (topic == null || topic.isEmpty())
throw new ValidationException(LOGGER.translate("TOPIC_VALIDATE_ERROR"));
ZkClient zkClient = new ZkClient(zkConnect, 10000, 8000, ZKStringSerializer$.MODULE$);
// Security for Kafka was added in Kafka 0.9.0.0 -> isSecureKafkaCluster = false
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), false);
if (AdminUtils.topicExists(zkUtils, topic))
zkClient.deleteRecursive(ZkUtils.getTopicPath(topic));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(null);
AdminUtils.createTopic(zkUtils, topic, partitions, replicas, new Properties(), RackAwareMode.Disabled$.MODULE$);
}
catch (Throwable th) {
LOGGER.error(th.getMessage(), th);
throw new ValidationException(th.getMessage());
}
finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
zkClient.close();
}
示例14: validate
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void validate() throws ValidationException {
super.validate();
if (zkConnect.isEmpty())
throw new ValidationException(LOGGER.translate("ZKCONNECT_VALIDATE_ERROR"));
if (topic.isEmpty())
throw new ValidationException(LOGGER.translate("TOPIC_VALIDATE_ERROR"));
if (groupId.isEmpty())
throw new ValidationException(LOGGER.translate("GROUP_ID_VALIDATE_ERROR"));
if (numThreads < 1)
throw new ValidationException(LOGGER.translate("NUM_THREADS_VALIDATE_ERROR"));
ZkClient zkClient = new ZkClient(zkConnect, 10000, 8000, ZKStringSerializer$.MODULE$);
// Security for Kafka was added in Kafka 0.9.0.0 -> isSecureKafkaCluster = false
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), false);
Boolean topicExists = AdminUtils.topicExists(zkUtils, topic);
zkClient.close();
if (!topicExists)
throw new ValidationException(LOGGER.translate("TOPIC_VALIDATE_ERROR"));
// Init Consumer Config
Properties props = new Properties()
{
{ put("zookeeper.connect", zkConnect); }
{ put("group.id", groupId); }
{ put("zookeeper.session.timeout.ms", "400"); }
{ put("zookeeper.sync.time.ms", "200"); }
{ put("auto.commit.interval.ms", "1000"); }
};
consumerConfig = new ConsumerConfig(props);
}
示例15: createTestTopic
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void createTestTopic(String topic, int numberOfPartitions, int replicationFactor, Properties topicConfig) {
// create topic with one client
LOG.info("Creating topic {}", topic);
ZkClient creator = createZkClient();
AdminUtils.createTopic(creator, topic, numberOfPartitions, replicationFactor, topicConfig);
creator.close();
// validate that the topic has been created
final long deadline = System.currentTimeMillis() + 30000;
do {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
// restore interrupted state
}
List<KafkaTopicPartitionLeader> partitions = FlinkKafkaConsumer08.getPartitionsForTopic(Collections.singletonList(topic), standardProps);
if (partitions != null && partitions.size() > 0) {
return;
}
}
while (System.currentTimeMillis() < deadline);
fail ("Test topic could not be created");
}