Source:
<code>private void ensureTopicCreated(final String topicName, int numPartitions, int replicationFactor) { final int sessionTimeoutMs = 10000; final int connectionTimeoutMs = 10000; final ZkClient zkClient = new ZkClient(zkAddress, sessionTimeoutMs, connectionTimeoutMs, utf8Serializer); try { Properties topicConfig = new Properties(); Seq<Object> brokerList = ZkUtils.getSortedBrokerList(zkClient); scala.collection.Map<Object, Seq<Object>> replicaAssignment = AdminUtils.assignReplicasToBrokers(brokerList, numPartitions, replicationFactor, -1, -1); AdminUtils.createOrUpdateTopicPartitionAssignmentPathInZK(zkClient, topicName, replicaAssignment, topicConfig, true); RetryTemplate retryTemplate = new RetryTemplate(); CompositeRetryPolicy policy = new CompositeRetryPolicy(); TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy(); timeoutRetryPolicy.setTimeout(METADATA_VERIFICATION_TIMEOUT); SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(); simpleRetryPolicy.setMaxAttempts(METADATA_VERIFICATION_RETRY_ATTEMPTS); policy.setPolicies(new RetryPolicy[] {timeoutRetryPolicy, simpleRetryPolicy}); try { retryTemplate.execute(new RetryCallback<Boolean, Exception>() { @Override public Boolean doWithRetry(RetryContext context) throws Exception { <START> TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk(topicName, zkClient); <END> if (topicMetadata.errorCode() != ErrorMapping.NoError()) { throw (Exception) ErrorMapping.exceptionFor(topicMetadata.errorCode()); } return true; } }); } catch (Exception e) { logger.error("Cannot initialize MessageBus", e); throw new RuntimeException("Cannot initialize message bus:", e); } } finally { zkClient.close(); } }</code><technical_language>I for low-level resource need some BackOffPolicy retry. default is NoBackOffPolicy. TimeoutRetryPolicy long retryTemplate.execute() live, some Thread.sleep retries. I missing</technical_language>

Target:
private void ensureTopicCreated(final String topicName, int numPartitions, int replicationFactor) { final int sessionTimeoutMs = 10000; final int connectionTimeoutMs = 10000; final ZkClient zkClient = new ZkClient(zkAddress, sessionTimeoutMs, connectionTimeoutMs, utf8Serializer); try { Properties topicConfig = new Properties(); Seq<Object> brokerList = ZkUtils.getSortedBrokerList(zkClient); scala.collection.Map<Object, Seq<Object>> replicaAssignment = AdminUtils.assignReplicasToBrokers(brokerList, numPartitions, replicationFactor, -1, -1); AdminUtils.createOrUpdateTopicPartitionAssignmentPathInZK(zkClient, topicName, replicaAssignment, topicConfig, true); RetryTemplate retryTemplate = new RetryTemplate(); CompositeRetryPolicy policy = new CompositeRetryPolicy(); TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy(); timeoutRetryPolicy.setTimeout(METADATA_VERIFICATION_TIMEOUT); SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(); simpleRetryPolicy.setMaxAttempts(METADATA_VERIFICATION_RETRY_ATTEMPTS); policy.setPolicies(new RetryPolicy[] {timeoutRetryPolicy, simpleRetryPolicy}); retryTemplate.setRetryPolicy(policy); ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); backOffPolicy.setInitialInterval(METADATA_VERIFICATION_RETRY_INITIAL_INTERVAL); backOffPolicy.setMultiplier(METADATA_VERIFICATION_RETRY_BACKOFF_MULTIPLIER); backOffPolicy.setMaxInterval(METADATA_VERIFICATION_MAX_INTERVAL); retryTemplate.setBackOffPolicy(backOffPolicy); try { retryTemplate.execute(new RetryCallback<Boolean, Exception>() { @Override public Boolean doWithRetry(RetryContext context) throws Exception { TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk(topicName, zkClient); if (topicMetadata.errorCode() != ErrorMapping.NoError()) { throw (Exception) ErrorMapping.exceptionFor(topicMetadata.errorCode()); } return true; } }); } catch (Exception e) { logger.error("Cannot initialize MessageBus", e); throw new RuntimeException("Cannot initialize message bus:", e); } } finally { zkClient.close(); } }