javagoogle-cloud-platformgoogle-cloud-pubsubgoogle-cloud-endpointsgoogle-cloud-pubsub-emulator

Creating topic on pubsub emulator


I started to use the pubsub emulator to test my basic implementations and ran into an issue while trying to create a new topic.

My emulator listens on localhost:8085 and if i create the topic via the api

PUT http://localhost:8085/v1/projects/testproject/topics/test

everything works fine and the topic gets created. But if i run the following snippet nothing works as intended and no topic gets created:

    TopicName topicName = TopicName.create("testproject", "test");
    ChannelProvider channelProvider =
            TopicAdminSettings.defaultChannelProviderBuilder()
                .setEndpoint("localhost:8085")
                .setCredentialsProvider(
                        FixedCredentialsProvider.create(NoCredentials.getInstance()))
                .build();
    TopicAdminClient topicClient = TopicAdminClient.create(
            TopicAdminSettings.defaultBuilder().setChannelProvider(channelProvider).build());
        topicClient.createTopic(topicName);

while running this the emulator logs

[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFORMATION: Adding handler(s) to newly registered Channel.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFORMATION: Detected non-HTTP/2 connection.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFORMATION: Unknown request URI: /bad-request
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFORMATION: Adding handler(s) to newly registered Channel.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFORMATION: Detected non-HTTP/2 connection.
[pubsub] Apr 27, 2017 1:10:47 PM io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFORMATION: Unknown request URI: /bad-request

...    

[pubsub] Apr 27, 2017 1:10:49 PM io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFORMATION: Adding handler(s) to newly registered Channel.
[pubsub] Apr 27, 2017 1:10:49 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFORMATION: Detected non-HTTP/2 connection.

Am i missing something on my ChannelProvider? Or didn't I configure my TopicAdminClient correctly? I don't see whats wrong since i used this as reference.

Maybe someone can help me out with this.


Solution

  • Channels used to communicate with the emulator need to set the negotiationType property to NegotiationType.PLAINTEXT. That means you need to create a custom ChannelProvider. Something like the following should work:

    public class PlainTextChannelProvider implements ChannelProvider {
      @Override
      public boolean shouldAutoClose() {
        return false;
      }
    
      @Override
      public boolean needsExecutor() {
        return false;
      }
    
      @Override
      public ManagedChannel getChannel() throws IOException {
        return NettyChannelBuilder.forAddress("localhost", 8085)
          .negotiationType(NegotiationType.PLAINTEXT)
          .build();
      }
    
      @Override
      public ManagedChannel getChannel(Executor executor) throws IOException {
        return getChannel();
      }
    }