Files
spring-cloud-function/spring-cloud-function-samples/function-sample-gcp-background/src/main/java/com/example/BackgroundFunctionMain.java
dzou a4788aba08 Add docs and updates for background function support
rename fuction-sample-gcp to function-sample-gcp-http

refdoc polish

background sample polish

Resolves #525
Update pub/sub bg function to use base64 encoding
2020-05-22 12:24:24 +02:00

32 lines
1.0 KiB
Java

package com.example;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.function.Consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class BackgroundFunctionMain {
public static void main(String[] args) {
SpringApplication.run(BackgroundFunctionMain.class, args);
}
/**
* The background function which triggers on an event from Pub/Sub and consumes the Pub/Sub
* event message.
*/
@Bean
public Consumer<PubSubMessage> pubSubFunction() {
return message -> {
// The PubSubMessage data field arrives as a base-64 encoded string and must be decoded.
// See: https://cloud.google.com/functions/docs/calling/pubsub#event_structure
String decodedMessage = new String(Base64.getDecoder().decode(message.getData()), StandardCharsets.UTF_8);
System.out.println("Received Pub/Sub message with data: " + decodedMessage);
};
}
}