Creating a theme for the pubsub emulator

I started using the pubsub emulator to test my core implementations and ran into a problem while trying to create a new theme.

My emulator is listening on localhost: 8085 and if I create a theme via api

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

      

everything works fine and the theme is created. But if I run the following snippet, nothing works as planned and no theme 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 starting this emulator log

[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 have I configured my TopicAdminClient incorrectly? I don't understand what happened since I used this as a link .

Can someone help me with this.

+3


source to share


2 answers


The channels used to communicate with the emulator need to set the property to a negotiationType

value NegotiationType.PLAINTEXT

. This means that 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();
  }
}

      

+3


source


This post is a bit old, hopefully it will serve as an update.

A snippet of code from the application Testing locally with an emulator also works. The complete snippet is on GitHub if you click 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();
}

      

0


source







All Articles