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
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
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);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.example;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A class that can be mapped to the GCF Pub/Sub Message event type. This is for use in
|
||||
* the background functions.
|
||||
*
|
||||
* <p>See the PubSubMessage definition for reference:
|
||||
* https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
|
||||
*
|
||||
* @author Mike Eltsufin
|
||||
*/
|
||||
public class PubSubMessage {
|
||||
|
||||
private String data;
|
||||
|
||||
private Map<String, String> attributes;
|
||||
|
||||
private String messageId;
|
||||
|
||||
private String publishTime;
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Map<String, String> getAttributes() {
|
||||
return attributes;
|
||||
}
|
||||
|
||||
public void setAttributes(Map<String, String> attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public String getMessageId() {
|
||||
return messageId;
|
||||
}
|
||||
|
||||
public void setMessageId(String messageId) {
|
||||
this.messageId = messageId;
|
||||
}
|
||||
|
||||
public String getPublishTime() {
|
||||
return publishTime;
|
||||
}
|
||||
|
||||
public void setPublishTime(String publishTime) {
|
||||
this.publishTime = publishTime;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Main-Class: com.example.BackgroundFunctionMain
|
||||
Reference in New Issue
Block a user