Use Google Cloud PubSub emulator from Java

2k Views Asked by At

I've setup Pubsub.Builder with root url pointed to local PubSub emulator (localhost:8036 at my case). It seems to be working, I see that emulator is receiving my test pushes, but gives me error 400 Bad Request.

Publish code:

auth = GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY)
         .createScoped(PubsubScopes.all());
client = new Pubsub.Builder(HTTP_TRANSPORT, JSON_FACTORY, auth)
         .setApplicationName "test"
         .setRootUrl("http://localhost:8036/")
         .build();
msg = new PubsubMessage().encodeData("{\"a\": 1, \"b\": 2}".getBytes());
req = new PublishRequest().setMessages(Arrays.asList(msg));
client.projects()
  .topics()
  .publish("projects/GCLOUD-DEFAULT-PROJECT/topics/test-topic", req)
  .execute();

In emulator console output I see following (that's for very basic PublishRequest):

[pubsub] jan 27, 2016 9:03:20 PM com.google.cloud.pubsub.testing.v1.FakePubsubGrpcServer$2 operationComplete
[pubsub] INFO: Adding handler(s) to newly registered Channel.
[pubsub] jan 27, 2016 9:03:20 PM com.google.cloud.pubsub.testing.v1.NettyUtil$HttpVersionDecider channelRead
[pubsub] INFO: Detected non-HTTP/2 connection.
[pubsub] jan 27, 2016 9:03:20 PM com.google.cloud.pubsub.testing.v1.NettyUtil$HttpJsonAdapter channelRead
[pubsub] INFO: Invalid input: Expect message object but got: "\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�M-.NLO-V���VJI"
[pubsub] com.google.protobuf.InvalidProtocolBufferException: Expect message object but got: "\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�M-.NLO-V���VJI"
[pubsub]    at com.google.protobuf.util.JsonFormat$ParserImpl.mergeMessage(JsonFormat.java:1099)
[pubsub]    at com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:1075)
[pubsub]    at com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:973)
[pubsub]    at com.google.protobuf.util.JsonFormat$Parser.merge(JsonFormat.java:201)
[pubsub]    at com.google.cloud.pubsub.testing.v1.PubsubJsonGrpcAdapters$PublisherAdapter.handleRequest(PubsubJsonGrpcAdapters.java:231)
[pubsub]    at com.google.cloud.pubsub.testing.v1.NettyUtil$HttpJsonAdapter.channelRead(NettyUtil.java:94)
[pubsub]    at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:83)

Seems that default PubSub client uses different protocol, but I don't see any way to configure it.

How can I use com.google.apis:google-api-services-pubsub library with local emulator?

1

There are 1 best solutions below

1
On

The emulator does not require or handle authentication or authorization; I'm guessing that's where the problem lies. Can you try passing null as the last parameter to the builder?

Also, it appears that Gzip compression must be explicitly disabled. Please set this on the builder:

.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
  @Override
  public void initialize(AbstractGoogleClientRequest<?> request) throws IOException {
    request.setDisableGZipContent(true);
  }
})