Interface BackgroundFunction<T>

  • Type Parameters:
    T - the class of payload objects that this function expects.
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface BackgroundFunction<T>
    Represents a Cloud Function that is activated by an event and parsed into a user-supplied class. The payload of the event is a JSON object, which is deserialized into a user-defined class as described for Gson.

    Here is an example of an implementation that accesses the messageId property from a payload that matches a user-defined PubSubMessage class:

     public class Example implementsBackgroundFunction<PubSubMessage> {
       private static final Logger logger = Logger.getLogger(Example.class.getName());
    
      @Override
       public void accept(PubSubMessage pubSubMessage, Context context) {
         logger.info("Got messageId " + pubSubMessage.messageId);
       }
     }
    
     // Where PubSubMessage is a user-defined class like this:
     public class PubSubMessage {
       String data;
      Map<String, String> attributes;
       String messageId;
       String publishTime;
     }
     
    • Method Detail

      • accept

        void accept​(T payload,
                    Context context)
             throws java.lang.Exception
        Called to service an incoming event. This interface is implemented by user code to provide the action for a given background function. If this method throws any exception (including any Error) then the HTTP response will have a 500 status code.
        Parameters:
        payload - the payload of the event, deserialized from the original 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.
        Throws:
        java.lang.Exception - to produce a 500 status code in the HTTP response.