Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<String> getTopicsNames(String connectionName) throws TopicsNameNotFound, BrokerNotFoundException {
- String brokerAddressesByName = getBrokerAddressesByName(connectionName);
- return getTopicByAddresses(brokerAddressesByName);
- }
- private List<String> getTopicByAddresses(String brokerAddresses) throws TopicsNameNotFound {
- List<String> result = new ArrayList<>();
- Properties props = new Properties();
- props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokerAddresses);
- try (AdminClient adminClient = AdminClient.create(props)) {
- ListTopicsOptions listTopicsOptions = new ListTopicsOptions();
- listTopicsOptions.timeoutMs(5000);
- result = adminClient.listTopics(listTopicsOptions).names().get()
- .stream().filter(StringUtils::hasLength).toList();
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- } catch (Exception e) {
- throw new TopicsNameNotFound("Topics name not found for connection name: " + brokerAddresses);
- }
- if (result.isEmpty()) {
- throw new TopicsNameNotFound("Topics name not found for connection name: " + brokerAddresses);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment