9

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.

Touko
  • 11,359
  • 16
  • 75
  • 105
T. Yoo
  • 161
  • 1
  • 7

2 Answers2

6

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();
  }
}
Kamal Aboul-Hosn
  • 15,111
  • 1
  • 34
  • 46
  • Thank you, that worked for creating Topics and publishing Messages. Do i have to implement something similar for the Subscriber.Listener aswell? I set the same channelProvider for the Subscriber but when calling subscriber.stopAsync(), it always throws an java.util.concurrent.RejectedExecutionException and (it seems randomly) does or doesn't pull messages, am i missing something here? – T. Yoo May 04 '17 at 12:33
  • You will need to use the same sort of ChannelProvider for your Subscriber, too, yes. It is hard to say what is causing the RejectedExecutionException. I don't think that is a problem specific to the Subscriber or emulator but might be specific to your code around subscribing, any Executors you use, or the lifetime of objects in your app. – Kamal Aboul-Hosn May 04 '17 at 14:23
  • i opened another question so i can give a more detailed description [link](http://stackoverflow.com/questions/43786716/subscriber-stopasync-results-in-rejectedexecutionexception) – T. Yoo May 04 '17 at 15:12
  • 1
    @KamalAboul-Hosn, I run into the same exception, the solution you provide is a bit outdated. It now needs a TransportChannelProvider. Is the fix this applicable? – Christophe Bouhier Nov 30 '18 at 09:44
3

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();
}
tianzi
  • 185
  • 1
  • 7