donderdag 28 mei 2020

Google Cloud Function for Java with PubSub




At time of writing Google Cloud functions for Java is almost in public beta.

What i want to achieve is to give some insight on how to create a background function.
Background functions are triggered by events.

These events can be created by e.g.
-  File created or deleted on Google Cloud Storage
- Google Firestore trigger
- Google PubSub

We will check the PubSub trigger and how to trigger this on your local dev machine.
What you will need is:
- IntelliJ
- Maven Cloud function plugin

What is Google Cloud Pub/Sub?

Google Cloud Pub/Sub is an asynchronous messaging service that decouples services that produce events from services that process events.
Pub/Sub offers durable message storage and real-time message delivery with high availability and consistent performance at scale.

Now its time to create a function!

For this function i chose Spring Cloud Functions with the GCP adapter.
Spring Cloud Functions can be used on other providers as well. for instance AWS.
The code repository can be found here but i will take some snippets and elaborate.

So this is the actual function which we will trigger.
@SpringBootApplication
public class PubSubIngest {
private static final Log log = LogFactory.getLog(PubSubIngest.class);

public static void main(String[] args) {
SpringApplication.run(PubSubIngest.class, args);
}

@Bean
public Consumer<PubSubMessage> ingest() {
return message -> {

// The PubSubMessage data field is base-64 encoded which must be decoded.
String decodedMessage = new String(Base64.getDecoder().decode(message.getData()), StandardCharsets.UTF_8);
System.out.println("Received Pub/Sub message with data: " + decodedMessage);
};
}
}


It takes the input and log it.
Nothing more nothing less.
Please bare in mind that the data field data needs to be base64 encoded.

Now that we have an created the function, lets trigger it!
Normally you would expect that you run the pubsub emulator , create a message on a topic and the function would be triggered on your local environment.
This is not the case for local development. At the moment of writing there is no feasible solution to run the cloud function locally and invoke it with an emaultor. 

But there is another way, stay tuned, keep on reading!

For this you can use the Maven Cloud function plugin, which you can run directly from IntelliJ.
In your Maven section double click function:run



Now your application starts




With an http call you can even invoke the background function which normally is triggered by a pubsub message or cloud storage trigger.
POST http://localhost:8080/ingest

{
"data": "VGhpcyBpcyB0aGUgbWVzc2FnZSBkYXRh"
}
###

This snippet can be fired directly from intellij


Once triggered you should see the following in your spinned up console:


Thats it folks! A background function in Java is a fact!
Hopefully this gives a little insight how to use Google Cloud Functions with Java 11 on your local machine.

In this example Spring is used, but you could also use vanilla plain old Java to create functions.
Next steps could be to deploy it to the Google Cloud and get fancy new stuff online.



Google Cloud Function for Java with PubSub

At time of writing Google Cloud functions for Java is almost in public beta. What i want to achieve is to give some insight on how ...