简体   繁体   中英

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.

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();
  }
}

This post is a little old, hope this serves as an update.

The code snippet from Testing apps locally with the emulator also works. The full snippet is on GitHub if you follow the "View on GitHub" link on the linked page.

String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
try {
  TransportChannelProvider channelProvider =
      FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
  CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

  TopicAdminClient topicClient =
      TopicAdminClient.create(
          TopicAdminSettings.newBuilder()
              .setTransportChannelProvider(channelProvider)
              .setCredentialsProvider(credentialsProvider)
              .build());
  try {
      response = topicClient.createTopic(topicName);
      System.out.printf("Topic %s created.\n", response);
  } catch (ApiException e) {
      System.out.println(e.getStatusCode().getCode());
      System.out.println(e.isRetryable());
      System.out.println("No topic was created.");
  }

} finally {
  channel.shutdown();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM