I want to connect to Google pubsub and when I use the documentation of Google itself it works. I enable port forwarding, so the connection is available on localhost:8086
. Below I have two code snippets. The first one connects and prints all the topics, the second one which has a custom host/port configuration does not work.
Option 1 – works
<code>import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import java.io.IOException;
import java.util.stream.StreamSupport;
public static void main(String[] args) throws IOException {
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder().build();
TopicAdminClient topicClient = TopicAdminClient.create(topicAdminSettings);
TopicAdminClient.ListTopicsPagedResponse listTopicsPagedResponse = topicClient.listTopics(ProjectName.newBuilder().setProject("my-project-id").build());
StreamSupport.stream(listTopicsPagedResponse.iterateAll().spliterator(), false)
.forEach(System.out::println);
<code>import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import java.io.IOException;
import java.util.stream.StreamSupport;
public class App {
public static void main(String[] args) throws IOException {
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder().build();
TopicAdminClient topicClient = TopicAdminClient.create(topicAdminSettings);
TopicAdminClient.ListTopicsPagedResponse listTopicsPagedResponse = topicClient.listTopics(ProjectName.newBuilder().setProject("my-project-id").build());
StreamSupport.stream(listTopicsPagedResponse.iterateAll().spliterator(), false)
.map(Topic::getName)
.forEach(System.out::println);
topicClient.close();
}
}
</code>
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import java.io.IOException;
import java.util.stream.StreamSupport;
public class App {
public static void main(String[] args) throws IOException {
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder().build();
TopicAdminClient topicClient = TopicAdminClient.create(topicAdminSettings);
TopicAdminClient.ListTopicsPagedResponse listTopicsPagedResponse = topicClient.listTopics(ProjectName.newBuilder().setProject("my-project-id").build());
StreamSupport.stream(listTopicsPagedResponse.iterateAll().spliterator(), false)
.map(Topic::getName)
.forEach(System.out::println);
topicClient.close();
}
}
Option 2 – does not work
<code>import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.FixedTransportChannelProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.util.stream.StreamSupport;
public static void main(String[] args) throws IOException {
ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8086").usePlaintext().build();
TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
TopicAdminClient topicClient = TopicAdminClient.create(topicAdminSettings);
TopicAdminClient.ListTopicsPagedResponse listTopicsPagedResponse = topicClient.listTopics(ProjectName.newBuilder().setProject("my-project-id").build());
StreamSupport.stream(listTopicsPagedResponse.iterateAll().spliterator(), false)
.forEach(System.out::println);
<code>import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.FixedTransportChannelProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.util.stream.StreamSupport;
public class App {
public static void main(String[] args) throws IOException {
ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8086").usePlaintext().build();
TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.build();
TopicAdminClient topicClient = TopicAdminClient.create(topicAdminSettings);
TopicAdminClient.ListTopicsPagedResponse listTopicsPagedResponse = topicClient.listTopics(ProjectName.newBuilder().setProject("my-project-id").build());
StreamSupport.stream(listTopicsPagedResponse.iterateAll().spliterator(), false)
.map(Topic::getName)
.forEach(System.out::println);
topicClient.close();
}
}
</code>
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.FixedTransportChannelProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminSettings;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.util.stream.StreamSupport;
public class App {
public static void main(String[] args) throws IOException {
ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8086").usePlaintext().build();
TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
.setTransportChannelProvider(channelProvider)
.build();
TopicAdminClient topicClient = TopicAdminClient.create(topicAdminSettings);
TopicAdminClient.ListTopicsPagedResponse listTopicsPagedResponse = topicClient.listTopics(ProjectName.newBuilder().setProject("my-project-id").build());
StreamSupport.stream(listTopicsPagedResponse.iterateAll().spliterator(), false)
.map(Topic::getName)
.forEach(System.out::println);
topicClient.close();
}
}
Any idea why the second one does not work, do I miss maybe some configuration? And how come the first option just works out of the box?
The reason why I want a custom host and port is because I have an end-to-end test which I want to run locally, development and test environment. These environments have different hosts.