public ListTopicsPagedResponse listTopics ( ) throws Exception { // [START pubsub_list_topics] try ( TopicAdminClient topicAdminClient = TopicAdminClient . create ( ) ) { ListTopicsRequest listTopicsRequest = ListTopicsRequest . newBuilder ( ) . setProject ( ProjectName . format ( projectId ) ) . build ( ) ; ListTopicsPagedResponse response = topicAdminClient . listTopics ( listTopicsRequest ) ; Iterable < Topic > topics = response . iterateAll ( ) ; for ( Topic topic : topics ) { // do something with the topic } return response ; } // [END pubsub_list_topics] }
Example of listing topics .