0
votes

In my code I have 2 cloud functions, cf1 and cf2. cf1 is triggered via pubsub topic t1 with a Google Scheduler cron job every 10 minutes and creates a list and sends it to topic t2 that triggers cf2. When I use the Google's example for the cf2 I can see my message and it works. However when I deploy my own code and log the message this is what I see: ```

cf2.accept:81) - data
.accept:83) - ms {"data_":{"bytes":[],"hash":0},"messageId_":"","orderingKey_":"","memoizedIsInitialized":-1,"unknownFields":{"fields":{},"fieldsDescending":{}},"memoizedSize":-1,"memoizedHashCode":0}

My code is: ```

public class cf2 implements BackgroundFunction<PubsubMessage> {
 @Override
public void accept(PubsubMessage message, Context context) throws Exception {
    if (message.getData() == null) {
        logger.info("No message provided");
        return;
    }


    String messageString = new String(
            Base64.getDecoder().decode(message.getData().toStringUtf8()),
            StandardCharsets.UTF_8);
    logger.info(messageString);

    logger.info("Starting the job");

    String data = message.getData().toStringUtf8();
    logger.info("data "+ data);
    String ms = new Gson().toJson(message);
    logger.info("ms "+ ms);
}```

But when I use Google's example code :

    package com.example;

import com.example.Example.PubSubMessage;
import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import java.util.Base64;
import java.util.Map;
import java.util.logging.Logger;

public class Example implements BackgroundFunction<PubSubMessage> {
  private static final Logger logger = Logger.getLogger(Example.class.getName());

  @Override
  public void accept(PubSubMessage message, Context context) {
    String data = message.data != null
      ? new String(Base64.getDecoder().decode(message.data))
      : "empty message";
    logger.info(data);
  }

  public static class PubSubMessage {
    String data;
    Map<String, String> attributes;
    String messageId;
    String publishTime;
  }
}

I see my message body very neatly in the logs. Can someone help me with what is wrong with my code?

Here's how I deploy my function:

gcloud --project=${PROJECT_ID} functions deploy \
    cf2 \
    --entry-point=path.to.cf2 \
    --runtime=java11 \
    --trigger-topic=t2 \
    --timeout=540\
    --source=folder \
    --set-env-vars="PROJECT_ID=${PROJECT_ID}" \
    --vpc-connector=projects/${PROJECT_ID}/locations/us-central1/connectors/appengine-default-connect

and when I log the message.getData() I get <ByteString@37c278a2 size=0 contents=""> while I know message is not empty ( I made another test subscription on the topic that helps me see the message there )

1
looks like your forcing it to UTF8. take out the .toStringUtf8()), StandardCharsets.UTF_8)Moiz
@Moiz even when I do that my message.getData() is <ByteString@37c278a2 size=0 contents=""> Peggy

1 Answers

3
votes

You need to define what is a PubSub message. This part is missing in your code and I don't know which PubSubMessage type you are using:

  public static class PubSubMessage {
    String data;
    Map<String, String> attributes;
    String messageId;
    String publishTime;
  }

It should solve your issue. Let me know.