@FunctionalInterface public interface BackgroundFunction
Here is an example of an implementation that operates on the JSON payload of the event directly:
public class Example implements BackgroundFunction {
private static final Logger logger = Logger.getLogger(Example.class.getName());
@Override
public void accept(JsonElement json, Context context) {
JsonElement messageId = json.getAsJsonObject().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:
public class Example implements BackgroundFunction {
private static final Logger logger = Logger.getLogger(Example.class.getName());
@Override
public void accept(JsonElement json, Context context) {
PubSubMessage message = 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(com.google.gson.JsonElement json,
Context context)
Called to service an incoming event.
|
void accept(com.google.gson.JsonElement json, Context context)
Error) then the HTTP response will have a 500 status code.json - the payload of the event, as a parsed JSON object.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.Copyright © 2019. All rights reserved.