org.I0Itec.zkclient.ZkConnection Java Examples

The following examples show how to use org.I0Itec.zkclient.ZkConnection. 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: KafkaRpcTopicService.java    From devicehive-java-server with Apache License 2.0 6 votes vote down vote up
public void createTopic(String topic) {
    ZkClient zkClient = new ZkClient(
            kafkaRpcConfig.getZookeeperConnect(),
            kafkaRpcConfig.getSessionTimeout(),
            kafkaRpcConfig.getConnectionTimeout(),
            ZKStringSerializer$.MODULE$);
    try {
        ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(kafkaRpcConfig.getZookeeperConnect()), false);
        Properties topicConfig = kafkaRpcConfig.topicProps();
        if (!AdminUtils.topicExists(zkUtils, topic)) {
            AdminUtils.createTopic(zkUtils, topic, kafkaRpcConfig.getNumPartitions(), 
                    kafkaRpcConfig.getReplicationFactor(), topicConfig, RackAwareMode.Enforced$.MODULE$);
        }
    } finally {
        zkClient.close();
    }
}
 
Example #2
Source File: KafkaStoreUtils.java    From data-highway with Apache License 2.0 6 votes vote down vote up
public static void checkAndCreateTopic(String zkConnect, String topic, int replicas) {
  ZkClient zkClient = new ZkClient(zkConnect, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), IS_SECURE_KAFKA_CLUSTER);

  if (AdminUtils.topicExists(zkUtils, topic)) {
    verifyTopic(zkUtils, topic);
    return;
  }

  int partitions = 1;
  Properties topicConfig = new Properties();
  topicConfig.put(LogConfig.CleanupPolicyProp(), "compact");

  AdminUtils.createTopic(zkUtils, topic, partitions, replicas, topicConfig, RackAwareMode.Enforced$.MODULE$);

  zkClient.close();
  zkUtils.close();
}
 
Example #3
Source File: KafkaEmbedded.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
/**
 * 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
  // registerTopic() 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$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), false);
  AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
  zkClient.close();
}
 
Example #4
Source File: TopicTest.java    From hermes with Apache License 2.0 6 votes vote down vote up
@Test
public void createTopicInTestEnv() {
	String ZOOKEEPER_CONNECT = "";
	ZkClient zkClient = new ZkClient(new ZkConnection(ZOOKEEPER_CONNECT));
	ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(ZOOKEEPER_CONNECT), false);
	zkClient.setZkSerializer(new ZKStringSerializer());
	int partition = 1;
	int replication = 1;
	String topic = String.format("kafka.test_create_topic_p%s_r%s", partition, replication);
	if (AdminUtils.topicExists(zkUtils, topic)) {
		TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk(topic, zkUtils);
		System.out.println(topicMetadata);
		AdminUtils.deleteTopic(zkUtils, topic);
	}
	AdminUtils.createTopic(zkUtils, topic, partition, replication, new Properties());
}
 
Example #5
Source File: BrokerFailureDetector.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public BrokerFailureDetector(Queue<Anomaly> anomalies,
                             KafkaCruiseControl kafkaCruiseControl) {
  KafkaCruiseControlConfig config = kafkaCruiseControl.config();
  String zkUrl = config.getString(ExecutorConfig.ZOOKEEPER_CONNECT_CONFIG);
  boolean zkSecurityEnabled = config.getBoolean(ExecutorConfig.ZOOKEEPER_SECURITY_ENABLED_CONFIG);
  ZkConnection zkConnection = new ZkConnection(zkUrl, ZK_SESSION_TIMEOUT);
  _zkClient = new ZkClient(zkConnection, ZK_CONNECTION_TIMEOUT, new ZkStringSerializer());
  // Do not support secure ZK at this point.
  _kafkaZkClient = KafkaCruiseControlUtils.createKafkaZkClient(zkUrl, ZK_BROKER_FAILURE_METRIC_GROUP, ZK_BROKER_FAILURE_METRIC_TYPE,
                                                               zkSecurityEnabled);
  _failedBrokers = new HashMap<>();
  _failedBrokersZkPath = config.getString(AnomalyDetectorConfig.FAILED_BROKERS_ZK_PATH_CONFIG);
  _anomalies = anomalies;
  _kafkaCruiseControl = kafkaCruiseControl;
  _fixableFailedBrokerCountThreshold = config.getShort(AnomalyDetectorConfig.FIXABLE_FAILED_BROKER_COUNT_THRESHOLD_CONFIG);
  _fixableFailedBrokerPercentageThreshold = config.getDouble(AnomalyDetectorConfig.FIXABLE_FAILED_BROKER_PERCENTAGE_THRESHOLD_CONFIG);
}
 
Example #6
Source File: UtilTest.java    From kafka-service-broker with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetConnection() throws InterruptedException {
    ZkConnection c = null;
    try {
        c = util.getConnection();
        assertNotNull(c);
        c.connect(null);
        String s = c.getServers();
        assertNotNull(s);
        assertEquals("104.154.136.114:2181", s);
    } finally {
        if (c != null) {
            c.close();
        }
    }
}
 
Example #7
Source File: KafkaStormIntegrationTest.java    From incubator-retired-pirk with Apache License 2.0 6 votes vote down vote up
private void startKafka() throws Exception
{
  FileUtils.deleteDirectory(new File(kafkaTmpDir));

  Properties props = new Properties();
  props.setProperty("zookeeper.session.timeout.ms", "100000");
  props.put("advertised.host.name", "localhost");
  props.put("port", 11111);
  // props.put("broker.id", "0");
  props.put("log.dir", kafkaTmpDir);
  props.put("enable.zookeeper", "true");
  props.put("zookeeper.connect", zookeeperLocalCluster.getConnectString());
  KafkaConfig kafkaConfig = KafkaConfig.fromProps(props);
  kafkaLocalBroker = new KafkaServer(kafkaConfig, new SystemTime(), scala.Option.apply("kafkaThread"));
  kafkaLocalBroker.startup();

  zkClient = new ZkClient(zookeeperLocalCluster.getConnectString(), 60000, 60000, ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperLocalCluster.getConnectString()), false);
  // ZkUtils zkUtils = ZkUtils.apply(zookeeperLocalCluster.getConnectString(), 60000, 60000, false);
  AdminUtils.createTopic(zkUtils, topic, 1, 1, new Properties());
}
 
Example #8
Source File: KafkaResourceController.java    From pubsub with Apache License 2.0 6 votes vote down vote up
@Override
protected void startAction() throws Exception {
  ZkClient zookeeperClient =
      new ZkClient(
          KafkaFlags.getInstance().zookeeperIp, 15000, 10000, ZKStringSerializer$.MODULE$);
  ZkUtils zookeeperUtils =
      new ZkUtils(zookeeperClient, new ZkConnection(KafkaFlags.getInstance().zookeeperIp), false);
  try {
    deleteTopic(zookeeperUtils);
    AdminUtils.createTopic(
        zookeeperUtils,
        topic,
        KafkaFlags.getInstance().partitions,
        KafkaFlags.getInstance().replicationFactor,
        AdminUtils.createTopic$default$5(),
        AdminUtils.createTopic$default$6());
    log.info("Created topic " + topic + ".");
  } finally {
    zookeeperClient.close();
  }
}
 
Example #9
Source File: SamzaExecutor.java    From samza with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> listTables(ExecutionContext context) throws ExecutorException {
  String address = environmentVariableHandler.getEnvironmentVariable(SAMZA_SQL_SYSTEM_KAFKA_ADDRESS);
  if (address == null || address.isEmpty()) {
    address = DEFAULT_SERVER_ADDRESS;
  }
  try {
    ZkUtils zkUtils = new ZkUtils(new ZkClient(address, DEFAULT_ZOOKEEPER_CLIENT_TIMEOUT),
        new ZkConnection(address), false);
    return JavaConversions.seqAsJavaList(zkUtils.getAllTopics())
      .stream()
      .map(x -> SAMZA_SYSTEM_KAFKA + "." + x)
      .collect(Collectors.toList());
  } catch (ZkTimeoutException ex) {
    throw new ExecutorException(ex);
  }
}
 
Example #10
Source File: TestZkNamespace.java    From samza with Apache License 2.0 5 votes vote down vote up
private void initZk(String zkConnect) {
  try {
    zkClient = new ZkClient(new ZkConnection(zkConnect, SESSION_TIMEOUT_MS), CONNECTION_TIMEOUT_MS);
  } catch (Exception e) {
    Assert.fail("Client connection setup failed for connect + " + zkConnect + ": " + e);
  }
}
 
Example #11
Source File: OperatorUtil.java    From doctorkafka with Apache License 2.0 5 votes vote down vote up
public static ZkUtils getZkUtils(String zkUrl) {
  if (!zkUtilsMap.containsKey(zkUrl)) {
    Tuple2<ZkClient, ZkConnection> zkClientAndConnection =
        ZkUtils.createZkClientAndConnection(zkUrl, 30000, 3000000);

    ZkUtils zkUtils = new ZkUtils(zkClientAndConnection._1(), zkClientAndConnection._2(), true);
    zkUtilsMap.put(zkUrl, zkUtils);
  }
  return zkUtilsMap.get(zkUrl);
}
 
Example #12
Source File: AtlasTopicCreator.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected ZkUtils createZkUtils(Configuration atlasProperties) {
    String zkConnect = atlasProperties.getString("atlas.kafka.zookeeper.connect");
    int sessionTimeout = atlasProperties.getInt("atlas.kafka.zookeeper.session.timeout.ms", 400);
    int connectionTimeout = atlasProperties.getInt("atlas.kafka.zookeeper.connection.timeout.ms", 200);
    Tuple2<ZkClient, ZkConnection> zkClientAndConnection = ZkUtils.createZkClientAndConnection(
            zkConnect, sessionTimeout, connectionTimeout);
    return new ZkUtils(zkClientAndConnection._1(), zkClientAndConnection._2(), false);
}
 
Example #13
Source File: BaseKafkaZkTest.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@BeforeMethod(alwaysRun = true)
public void beforeMethodSetup() throws Exception {
  DynamicMetricsManager.createInstance(new MetricRegistry(), getClass().getSimpleName());
  Properties kafkaConfig = new Properties();
  // we will disable auto topic creation for tests
  kafkaConfig.setProperty("auto.create.topics.enable", Boolean.FALSE.toString());
  kafkaConfig.setProperty("delete.topic.enable", Boolean.TRUE.toString());
  kafkaConfig.setProperty("offsets.topic.replication.factor", "1");
  _kafkaCluster = new DatastreamEmbeddedZookeeperKafkaCluster(kafkaConfig);
  _kafkaCluster.startup();
  _broker = _kafkaCluster.getBrokers().split("\\s*,\\s*")[0];
  _zkClient = new ZkClient(_kafkaCluster.getZkConnection());
  _zkUtils = new ZkUtils(_zkClient, new ZkConnection(_kafkaCluster.getZkConnection()), false);
}
 
Example #14
Source File: KafkaStoreUtilsTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
private boolean topicExists(String topic) {
  ZkClient zkClient = new ZkClient(cluster.zKConnectString(), SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS,
      ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(cluster.zKConnectString()), IS_SECURE_KAFKA_CLUSTER);
  boolean exists = AdminUtils.topicExists(zkUtils, topic);
  zkClient.close();
  zkUtils.close();
  return exists;
}
 
Example #15
Source File: TestKafkaBroker.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up ZooKeeper test server.
 *
 * @throws Exception If failed.
 */
private void setupZooKeeper() throws Exception {
    zkServer = new TestingServer(ZK_PORT, true);

    Tuple2<ZkClient, ZkConnection> zkTuple = ZkUtils.createZkClientAndConnection(zkServer.getConnectString(),
        ZK_SESSION_TIMEOUT, ZK_CONNECTION_TIMEOUT);

    zkUtils = new ZkUtils(zkTuple._1(), zkTuple._2(), false);
}
 
Example #16
Source File: MockKafka.java    From kylin with Apache License 2.0 5 votes vote down vote up
private static Properties createProperties(ZkConnection zkServerConnection, String logDir, String server,
        String brokerId) {
    Properties properties = new Properties();
    properties.put("broker.id", brokerId);
    properties.put("log.dirs", logDir);
    properties.put("offsets.topic.replication.factor", "1");
    properties.put("delete.topic.enable", "true");
    properties.put("zookeeper.connect", zkServerConnection.getServers());
    properties.put("listeners", "PLAINTEXT://" + server);
    properties.put("advertised.listeners", "PLAINTEXT://" + server);
    return properties;
}
 
Example #17
Source File: Kafka09DataWriter.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private void provisionTopic(String topicName,Config config) {
String zooKeeperPropKey = KafkaWriterConfigurationKeys.CLUSTER_ZOOKEEPER;
if(!config.hasPath(zooKeeperPropKey)) {
 log.debug("Topic "+topicName+" is configured without the partition and replication");
 return;
}
String zookeeperConnect = config.getString(zooKeeperPropKey);
int sessionTimeoutMs = ConfigUtils.getInt(config, KafkaWriterConfigurationKeys.ZOOKEEPER_SESSION_TIMEOUT, KafkaWriterConfigurationKeys.ZOOKEEPER_SESSION_TIMEOUT_DEFAULT);
int connectionTimeoutMs = ConfigUtils.getInt(config, KafkaWriterConfigurationKeys.ZOOKEEPER_CONNECTION_TIMEOUT, KafkaWriterConfigurationKeys.ZOOKEEPER_CONNECTION_TIMEOUT_DEFAULT);
// 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, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
// Security for Kafka was added in Kafka 0.9.0.0
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect), false);
int partitions = ConfigUtils.getInt(config, KafkaWriterConfigurationKeys.PARTITION_COUNT, KafkaWriterConfigurationKeys.PARTITION_COUNT_DEFAULT);
int replication = ConfigUtils.getInt(config, KafkaWriterConfigurationKeys.REPLICATION_COUNT, KafkaWriterConfigurationKeys.PARTITION_COUNT_DEFAULT);
Properties topicConfig = new Properties();
if(AdminUtils.topicExists(zkUtils, topicName)) {
log.debug("Topic"+topicName+" already Exists with replication: "+replication+" and partitions :"+partitions);
   return;
}
try {
   AdminUtils.createTopic(zkUtils, topicName, partitions, replication, topicConfig);
} catch (RuntimeException e) {
   throw new RuntimeException(e);
}
   log.info("Created Topic "+topicName+" with replication: "+replication+" and partitions :"+partitions);
}
 
Example #18
Source File: TollboothApp.java    From data-highway with Apache License 2.0 5 votes vote down vote up
private void checkAndCreateTopic(String zkConnect, String topic, int partitions, int replicas) {
  ZkClient zkClient = new ZkClient(zkConnect, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), IS_SECURE_KAFKA_CLUSTER);

  if (!AdminUtils.topicExists(zkUtils, topic)) {
    AdminUtils.createTopic(zkUtils, topic, partitions, replicas, new Properties(), RackAwareMode.Enforced$.MODULE$);
  }

  zkUtils.close();
  zkClient.close();
}
 
Example #19
Source File: TestZkNamespace.java    From samza with Apache License 2.0 5 votes vote down vote up
private void createNamespace(String pathToCreate) {
  if (Strings.isNullOrEmpty(pathToCreate)) {
    return;
  }

  String zkConnect = "127.0.0.1:" + zkServer.getPort();
  try {
    zkClient1 = new ZkClient(new ZkConnection(zkConnect, SESSION_TIMEOUT_MS), CONNECTION_TIMEOUT_MS);
  } catch (Exception e) {
    Assert.fail("Client connection setup failed. Aborting tests..");
  }
  zkClient1.createPersistent(pathToCreate, true);
}
 
Example #20
Source File: ZkClient.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void close() throws ZkInterruptedException {
  if (LOG.isTraceEnabled()) {
    StackTraceElement[] calls = Thread.currentThread().getStackTrace();
    LOG.trace("closing zkclient. callStack: {}", Arrays.asList(calls));
  }
  getEventLock().lock();
  try {
    if (_connection == null) {
      return;
    }
    LOG.info("closing zkclient: {}", ((ZkConnection) _connection).getZookeeper());
    super.close();
  } catch (ZkInterruptedException e) {
    /*
     * Workaround for HELIX-264: calling ZkClient#disconnect() in its own eventThread context will
     * throw ZkInterruptedException and skip ZkConnection#disconnect()
     */
    try {
      /*
       * ZkInterruptedException#construct() honors InterruptedException by calling
       * Thread.currentThread().interrupt(); clear it first, so we can safely disconnect the
       * zk-connection
       */
      Thread.interrupted();
      _connection.close();
      /*
       * restore interrupted status of current thread
       */
      Thread.currentThread().interrupt();
    } catch (InterruptedException e1) {
      throw new ZkInterruptedException(e1);
    }
  } finally {
    getEventLock().unlock();
    LOG.info("closed zkclient");
  }
}
 
Example #21
Source File: TestDatastreamServer.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testUserManagedDestination() throws Exception {
  int numberOfPartitions = 2;
  String destinationTopic = "userManaged_" + UUID.randomUUID().toString();
  _datastreamCluster =
      initializeTestDatastreamServerWithFileConnector(1, BROADCAST_STRATEGY_FACTORY, numberOfPartitions);
  int totalEvents = 10;
  _datastreamCluster.startup();

  Path tempFile1 = Files.createTempFile("testFile1", "");
  String fileName1 = tempFile1.toAbsolutePath().toString();

  ZkClient zkClient = new ZkClient(_datastreamCluster.getZkConnection());
  ZkConnection zkConnection = new ZkConnection(_datastreamCluster.getZkConnection());
  ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);
  AdminUtils.createTopic(zkUtils, destinationTopic, numberOfPartitions, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);

  Datastream fileDatastream1 = createFileDatastream(fileName1, destinationTopic, 2);
  Assert.assertEquals((int) fileDatastream1.getDestination().getPartitions(), 2);

  Collection<String> eventsWritten1 = TestUtils.generateStrings(totalEvents);
  FileUtils.writeLines(new File(fileName1), eventsWritten1);

  Collection<String> eventsReceived1 = readFileDatastreamEvents(fileDatastream1, 0, 4);
  Collection<String> eventsReceived2 = readFileDatastreamEvents(fileDatastream1, 1, 6);
  eventsReceived1.addAll(eventsReceived2);

  LOG.info("Events Received " + eventsReceived1);
  LOG.info("Events Written to file " + eventsWritten1);

  Assert.assertTrue(eventsReceived1.containsAll(eventsWritten1));
}
 
Example #22
Source File: KafkaResourceController.java    From pubsub with Apache License 2.0 5 votes vote down vote up
@Override
protected void stopAction() throws Exception {
  ZkClient zookeeperClient =
      new ZkClient(
          KafkaFlags.getInstance().zookeeperIp, 15000, 10000, ZKStringSerializer$.MODULE$);
  ZkUtils zookeeperUtils =
      new ZkUtils(zookeeperClient, new ZkConnection(KafkaFlags.getInstance().zookeeperIp), false);
  try {
    deleteTopic(zookeeperUtils);
  } finally {
    zookeeperClient.close();
  }
}
 
Example #23
Source File: ZkRegister.java    From netty-pubsub with MIT License 5 votes vote down vote up
/**
 * zookeeper�ڵ�ע��
 * @param path  �ӽڵ����·��
 * @param data  �ӽڵ�ip:port
 */
public void register(String path,Object data){
	  zkc=new ZkClient(new ZkConnection(CONNECT_ADDR_SINGLE),SESSION_OUTTIME);
	  if(!zkc.exists(ROOT_PATH)){
        	zkc.createPersistent(ROOT_PATH,"brokerList");
        	LOGGER.info("��zk�ڵ��ʼ���ɹ���");
        };
        //������ʱ�ڵ�
        String createEphemeralSequential = zkc.createEphemeralSequential(path, data);
        //���õ�ǰzk·��
        this.currentZKPath=createEphemeralSequential;
        LOGGER.info("��brokerע�᡿path->"+createEphemeralSequential+" data->"+data+" status=SUCCESS");
}
 
Example #24
Source File: KafkaProducerServiceIntegrationTest.java    From vertx-kafka-service with Apache License 2.0 5 votes vote down vote up
private void createTopic(String topic) {
    final ZkClient zkClient = new ZkClient(zookeeper.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
    final ZkConnection connection = new ZkConnection(zookeeper.getConnectString());
    final ZkUtils zkUtils = new ZkUtils(zkClient, connection, false);
    AdminUtils.createTopic(zkUtils, topic, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
    TestUtils.waitUntilMetadataIsPropagated(JavaConversions.asScalaBuffer(Lists.newArrayList(kafkaServer)), topic, 0, 10000);
    zkClient.close();
}
 
Example #25
Source File: SchedulerMonitor.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public SchedulerMonitor(String registryType, String servers) {
	if("zookeeper".equals(registryType)) {
		ZkConnection zkConnection = new ZkConnection(servers);
		zkClient = new ZkClient(zkConnection, 3000);
	}else{
		asyncEventBus = new AsyncEventBus(JobContext.getContext().getSyncExecutor());
		asyncEventBus.register(JobContext.getContext().getRegistry());
	}
}
 
Example #26
Source File: ZkRegister.java    From netty-pubsub with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	ZkClient client=new ZkClient(new ZkConnection(CONNECT_ADDR_SINGLE),SESSION_OUTTIME);
	List<String> children = client.getChildren("/broker_active");
	children.forEach((rs)->{
		Object readData = client.readData("/broker_active/"+rs);
		System.out.println(readData);
	});	
}
 
Example #27
Source File: KafkaUtils.java    From doctorkafka with Apache License 2.0 5 votes vote down vote up
public static ZkUtils getZkUtils(String zkUrl) {
  if (!zkUtilsMap.containsKey(zkUrl)) {
    Tuple2<ZkClient, ZkConnection> zkClientAndConnection =
        ZkUtils.createZkClientAndConnection(zkUrl, 30000, 3000000);

    ZkUtils zkUtils = new ZkUtils(zkClientAndConnection._1(), zkClientAndConnection._2(), true);
    zkUtilsMap.put(zkUrl, zkUtils);
  }
  return zkUtilsMap.get(zkUrl);
}
 
Example #28
Source File: SchedulerMonitor.java    From azeroth with Apache License 2.0 5 votes vote down vote up
public SchedulerMonitor(String registryType, String servers) {
    if ("redis".equals(registryType)) {

    } else {
        ZkConnection zkConnection = new ZkConnection(servers);
        zkClient = new ZkClient(zkConnection, 3000);
    }
}
 
Example #29
Source File: ProfileZkClient.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
@Override
public void run(String... args) throws Exception {
	List<ProfileEntity> profiles = profileMapper.findAllEnabledProfiles();
	for (ProfileEntity profile : profiles) {
		String zkServers = profileMapper.findExtrAttr(profile.getName(), ProfileExtrAttrName.ZK_SERVERS.name());
		if(zkServers != null){
			try {	
				if(profileZkServersMapping.values().contains(zkServers)){
					inner:for (String sameProfileName : profileZkServersMapping.keySet()) {
						if(profileZkServersMapping.get(sameProfileName).equals(zkServers)){								
							profileZkClientMapping.put(profile.getName(), profileZkClientMapping.get(sameProfileName));
							logger.info("create zkClient ok -> profile:{},sameProfileName:{}",profile,sameProfileName);
							break inner;
						}
					}
				}else{
					ZkConnection zkConnection = new ZkConnection(zkServers);
					ZkClient zkClient = new ZkClient(zkConnection, 3000);
					profileZkClientMapping.put(profile.getName(), zkClient);
					profileZkServersMapping.put(profile.getName(), zkServers);
					logger.info("create zkClient ok -> profile:{},zkServers:{}",profile.getName(),zkServers);
				}
				
			} catch (Exception e) {
				logger.error("create zkClient:" + zkServers,e);
			}
		}
	}
	//
	ConfigStateHolder.setProfileZkClient(this);
}
 
Example #30
Source File: KafkaTransportProviderAdmin.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Constructor for KafkaTransportProviderAdmin.
 * @param transportProviderName transport provider name
 * @param props TransportProviderAdmin configuration properties, e.g. ZooKeeper connection string, bootstrap.servers.
 */
public KafkaTransportProviderAdmin(String transportProviderName, Properties props) {
  _transportProviderProperties = props;
  VerifiableProperties transportProviderProperties = new VerifiableProperties(_transportProviderProperties);

  // ZK connect string and bootstrap servers configs might not exist for connectors that manage their own destinations
  _zkAddress = Optional.ofNullable(_transportProviderProperties.getProperty(ZK_CONNECT_STRING_CONFIG))
      .filter(v -> !v.isEmpty());

  _zkUtils = _zkAddress.map(address -> new ZkUtils(new ZkClient(address), new ZkConnection(address), false));

  //Load default producer bootstrap server from config if available
  _brokersConfig =
      Optional.ofNullable(_transportProviderProperties.getProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG));

  _numProducersPerConnector =
      transportProviderProperties.getInt(CONFIG_NUM_PRODUCERS_PER_CONNECTOR, DEFAULT_PRODUCERS_PER_CONNECTOR);

  _defaultNumProducersPerTask = transportProviderProperties.getInt(CONFIG_PRODUCERS_PER_TASK, 1);
  org.apache.commons.lang3.Validate.isTrue(_defaultNumProducersPerTask > 0 && _defaultNumProducersPerTask <= _numProducersPerConnector,
      "Invalid value for " + CONFIG_PRODUCERS_PER_TASK);

  String metricsPrefix = transportProviderProperties.getString(CONFIG_METRICS_NAMES_PREFIX, null);
  if (metricsPrefix != null && !metricsPrefix.endsWith(".")) {
    _transportProviderMetricsNamesPrefix = metricsPrefix + ".";
  } else {
    _transportProviderMetricsNamesPrefix = metricsPrefix;
  }

  _topicProperties = transportProviderProperties.getDomainProperties(DOMAIN_TOPIC);
}