Reputation: 5110
I'm trying to receive message to a topic and receive the message in the subscriber. But in the subscriber I don't receive any message, only an empty PubsubMessage
.
Subscriber
package com.dsam.assignment02.functions;
import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.pubsub.v1.PubsubMessage;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
public class OrderBillPdfGeneratorFunction implements BackgroundFunction<PubsubMessage> {
private static final Logger logger = Logger.getLogger(OrderBillPdfGeneratorFunction.class.getName());
@Override
public void accept(PubsubMessage payload, Context context) throws Exception {
String s = payload.getData().isEmpty() ? "No data" : new String(
Base64.getDecoder().decode(payload.getData().toByteArray()),
StandardCharsets.UTF_8
);
String t = String.valueOf(payload.getAttributesCount());
logger.log(Level.INFO, String.format("Data: %s\nAttribute Count: %s", s, t));
}
}
Log Output
Data: No data
Attribute Count: 0
Any idea what I'm missing here?
Update
I published message from cloud console pub/sub topic, but still didn't receive any data, which rules out the possiblity of issue being related to the publisher code.
Upvotes: 0
Views: 1611
Reputation: 5110
Eventually I fugured out the problem. I had to define my own class for function parameter instead of using com.google.pubsub.v1.PubsubMessage
.
Defined below PubSubMessage
class and used as public class OrderBillPdfGeneratorFunction implements BackgroundFunction<PubSubMessage>
public static class PubSubMessage {
public String data;
public Map<String, String> attributes;
public String messageId;
public String publishTime;
}
This solved the issue.
EDIT
Here's the full working code.
package com.dsam.assignment02.functions;
import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import com.google.pubsub.v1.PubsubMessage;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
public class OrderBillPdfGeneratorFunction implements BackgroundFunction<PubSubMessage> {
private static final Logger logger = Logger.getLogger(OrderBillPdfGeneratorFunction.class.getName());
@Override
public void accept(PubSubMessage payload, Context context) throws Exception {
String s = payload.getData().isEmpty() ? "No data" : new String(
Base64.getDecoder().decode(payload.getData().toByteArray()),
StandardCharsets.UTF_8
);
String t = String.valueOf(payload.getAttributesCount());
logger.log(Level.INFO, String.format("Data: %s\nAttribute Count: %s", s, t));
}
public static class PubSubMessage {
public String data;
public Map<String, String> attributes;
public String messageId;
public String publishTime;
}
}
Upvotes: 3