@FunctionalInterface public interface RawBackgroundFunction
Here is an example of an implementation that parses the JSON payload using Gson, to access its
messageId property:
public class Example implements RawBackgroundFunction {
private static final Logger logger = Logger.getLogger(Example.class.getName());
@Override
public void accept(String json, Context context) {
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
JsonElement messageId = jsonObject.get("messageId");
String messageIdString = messageId.getAsJsonString();
logger.info("Got messageId " + messageIdString);
}
}
Here is an example of an implementation that deserializes the JSON payload into a Java object for simpler access, again using Gson:
public class Example implements RawBackgroundFunction {
private static final Logger logger = Logger.getLogger(Example.class.getName());
@Override
public void accept(String json, Context context) {
PubSubMessage message = new Gson().fromJson(json, PubSubMessage.class);
logger.info("Got messageId " + message.messageId);
}
}
// Where PubSubMessage is a user-defined class like this:
public class PubSubMessage {
String data;
Map<String, String> attributes;
String messageId;
String publishTime;
}
| Modifier and Type | Method and Description |
|---|---|
void |
accept(String json,
Context context)
Called to service an incoming event.
|
void accept(String json, Context context) throws Exception
Error) then the HTTP response will have a 500 status code.json - the payload of the event, as a JSON string.context - the context of the event. This is a set of values that every event has,
separately from the payload, such as timestamp and event type.ExceptionCopyright © 2020. All rights reserved.