Quarkus - Google Cloud Functions (Serverless)

The quarkus-google-cloud-functions extension allows you to use Quarkus to build your Google Cloud Functions. Your functions can use injection annotations from CDI or Spring and other Quarkus facilities as you need them.

As the Google Cloud Function Java engine is a new Beta feature of Google Cloud, this extension is flagged as experimental.

This technology is considered experimental.

In experimental mode, early feedback is requested to mature the idea. There is no guarantee of stability nor long term presence in the platform until the solution matures. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible extension statuses, check our FAQ entry.

Prerequisites

To complete this guide, you need:

Solution

This guide walks you through generating a sample project followed by creating multiple functions showing how to implement HttpFunction, BackgroundFunction and RawBackgroundFunction in Quarkus. Once built, you will be able to deploy the project to Google Cloud.

If you don’t want to follow all these steps, you can go right to the completed example.

Clone the Git repository: git clone [https://github.com/quarkusio/quarkus-quickstarts.git](https://github.com/quarkusio/quarkus-quickstarts.git), or download an archive.

The solution is located in the google-cloud-functions-quickstart directory.

Creating the Maven Deployment Project

Create an application with the quarkus-google-cloud-functions extension. You can use the following Maven command to create it:

  1. mvn io.quarkus:quarkus-maven-plugin:1.7.6.Final:create \
  2. -DprojectGroupId=org.acme \
  3. -DprojectArtifactId=google-cloud-functions \
  4. -DclassName="org.acme.quickstart.GreetingResource" \
  5. -Dpath="/hello" \
  6. -Dextensions="google-cloud-functions"

Now, let’s remove what’s not needed inside the generated application:

  • Remove the dependency io.quarkus:quarkus-reasteasy from your pom.xml file.

  • Remove the generated org.acme.quickstart.GreetingResource class.

  • Remove the index.html from resources/META-INF/resources or it will be picked up instead of your Function.

  • Remove the existing tests.

Login to Google Cloud

Login to Google Cloud is necessary for deploying the application and it can be done as follows:

  1. gcloud auth login

At the time of this writing, Cloud Functions are still in beta so make sure to install the beta command group.

  1. gcloud components install beta

Creating the functions

For this example project, we will create three functions, one HttpFunction, one BackgroundFunction (Storage event) and one RawBakgroundFunction (PubSub event).

Choose Your Function

The quarkus-google-cloud-functions extension scans your project for a class that directly implements the Google Cloud HttpFunction, BackgroundFunction or RawBakgroundFunction interface. It must find a class in your project that implements one of these interfaces or it will throw a build time failure. If it finds more than one function classes, a build time exception will also be thrown.

Sometimes, though, you might have a few related functions that share code and creating multiple maven modules is just an overhead you don’t want to do. The extension allows you to bundle multiple functions in one project and use configuration or an environment variable to pick the function you want to deploy.

To configure the name of the function, you can use the following configuration property:

  1. quarkus.google-cloud-functions.function=test

The quarkus.google-cloud-functions.function property tells Quarkus which function to deploy. This can be overridden with an environment variable too.

The CDI name of the function class must match the value specified within the quarkus.google-cloud-functions.function property. This must be done using the @Named annotation.

  1. @Named("test")
  2. public class TestHttpFunction implements HttpFunction {
  3. }

The HttpFunction

  1. import javax.ws.rs.GET;
  2. import javax.ws.rs.Path;
  3. import javax.ws.rs.Produces;
  4. import javax.ws.rs.core.MediaType;
  5. import java.io.Writer;
  6. import javax.enterprise.context.ApplicationScoped;
  7. import javax.inject.Inject;
  8. import javax.inject.Named;
  9. import com.google.cloud.functions.HttpFunction;
  10. import com.google.cloud.functions.HttpRequest;
  11. import com.google.cloud.functions.HttpResponse;
  12. import io.quarkus.gcp.function.test.service.GreetingService;
  13. @Named("httpFunction") (1)
  14. @ApplicationScoped (2)
  15. public class HttpFunctionTest implements HttpFunction { (3)
  16. @Inject GreetingService greetingService; (4)
  17. @Override
  18. public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception { (5)
  19. Writer writer = httpResponse.getWriter();
  20. writer.write(greetingService.hello());
  21. }
  22. }
  1. The @Named annotation allows to name the CDI bean to be used by the quarkus.google-cloud-functions.function property, this is optional.

  2. The function must be a CDI bean

  3. This is a regular Google Cloud Function implementation, so it needs to implement com.google.cloud.functions.HttpFunction.

  4. Injection works inside your function.

  5. This is standard Google Cloud Function implementation, nothing fancy here.

The BackgroundFunction

This BackgroundFunction is triggered by a Storage event, you can use any events supported by Google Cloud instead.

  1. import javax.enterprise.context.ApplicationScoped;
  2. import javax.inject.Inject;
  3. import javax.inject.Named;
  4. import com.google.cloud.functions.BackgroundFunction;
  5. import com.google.cloud.functions.Context;
  6. import io.quarkus.gcp.function.test.service.GreetingService;
  7. @Named("storageTest") (1)
  8. @ApplicationScoped (2)
  9. public class BackgroundFunctionStorageTest implements BackgroundFunction<BackgroundFunctionStorageTest.StorageEvent> { (3)
  10. @Inject GreetingService greetingService; (4)
  11. @Override
  12. public void accept(StorageEvent event, Context context) throws Exception { (5)
  13. System.out.println("Receive event: " + event);
  14. System.out.println("Be polite, say " + greetingService.hello());
  15. }
  16. //
  17. public static class StorageEvent { (6)
  18. public String name;
  19. }
  20. }
  1. The @Named annotation allows to name the CDI bean to be used by the quarkus.google-cloud-functions.function property, this is optional.

  2. The function must be a CDI bean

  3. This is a regular Google Cloud Function implementation, so it needs to implement com.google.cloud.functions.BackgroundFunction.

  4. Injection works inside your function.

  5. This is standard Google Cloud Function implementation, nothing fancy here.

  6. This is the class the event will be deserialized to.

The RawBackgroundFunction

This RawBackgroundFunction is triggered by a PubSub event, you can use any events supported by Google Cloud instead.

  1. import javax.enterprise.context.ApplicationScoped;
  2. import javax.inject.Inject;
  3. import javax.inject.Named;
  4. import com.google.cloud.functions.Context;
  5. import com.google.cloud.functions.RawBackgroundFunction;
  6. import io.quarkus.gcp.function.test.service.GreetingService;
  7. @Named("rawPubSubTest") (1)
  8. @ApplicationScoped (2)
  9. public class RawBackgroundFunctionPubSubTest implements RawBackgroundFunction { (3)
  10. @Inject GreetingService greetingService; (4)
  11. @Override
  12. public void accept(String event, Context context) throws Exception { (5)
  13. System.out.println("PubSub event: " + event);
  14. System.out.println("Be polite, say " + greetingService.hello());
  15. }
  16. }
  1. The @Named annotation allows to name the CDI bean to be used by the quarkus.google-cloud-functions.function property, this is optional.

  2. The function must be a CDI bean

  3. This is a regular Google Cloud Function implementation, so it needs to implement com.google.cloud.functions.RawBackgroundFunction.

  4. Injection works inside your function.

  5. This is standard Google Cloud Function implementation, nothing fancy here.

Build and Deploy to Google Cloud

To build your application, you can package it using the standard mvn clean package command.

The result of the previous command is a single JAR file inside the target/deployment repository that contains classes and dependencies of the project.

Then you will be able to use gcloud beta functions deploy command to deploy your function to Google Cloud.

The first time you launch this command, you can have the following error message:

  1. ERROR: (gcloud.beta.functions.deploy) OperationError: code=7, message=Build Failed: Cloud Build has not been used in project <project_name> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudbuild.googleapis.com/overview?project=<my-project&gt; then retry.

This means that Cloud Build is not activated yet. To overcome this error, open the URL shown in the error, follow the instructions and then wait a few minutes before retrying the command.

The HttpFunction

This is an example command to deploy your HttpFunction to Google Cloud:

  1. gcloud beta functions deploy quarkus-example-http \
  2. --entry-point=io.quarkus.gcp.functions.QuarkusHttpFunction \
  3. --runtime=java11 --trigger-http --source=target/deployment

The entry point must always be set to io.quarkus.gcp.functions.QuarkusHttpFunction as this is the class that integrates Cloud Functions with Quarkus.

This command will give you as output a httpsTrigger.url that points to your function.

The BackgroundFunction

Before deploying your function, you need to create a bucket.

  1. gsutil mb gs://quarkus-hello

This is an example command to deploy your BackgroundFunction to Google Cloud, as the function is triggered by a Storage event, it needs to use --trigger-event google.storage.object.finalize and the --trigger-resource parameter with the name of a previously created bucket:

  1. gcloud beta functions deploy quarkus-example-storage \
  2. --entry-point=io.quarkus.gcp.functions.QuarkusBackgroundFunction \
  3. --trigger-resource quarkus-hello --trigger-event google.storage.object.finalize \
  4. --runtime=java11 --source=target/deployment

The entry point must always be set to io.quarkus.gcp.functions.QuarkusBackgroundFunction as this is the class that integrates Cloud Functions with Quarkus.

To trigger the event, you can send a file to the GCS quarkus-hello bucket or you can use gcloud to simulate one:

  1. gcloud functions call quarkus-example-storage --data '{"name":"test.txt"}'
—data contains the GCS event, it is a JSON document with the name of the file added to the bucket.

The RawBackgroundFunction

This is an example command to deploy your RawBackgroundFunction to Google Cloud, as the function is triggered by a PubSub event, it needs to use --trigger-event google.pubsub.topic.publish and the --trigger-resource parameter with the name of a previously created topic:

  1. gcloud beta functions deploy quarkus-example-pubsub \
  2. --entry-point=io.quarkus.gcp.functions.QuarkusBackgroundFunction \
  3. --runtime=java11 --trigger-resource hello_topic --trigger-event google.pubsub.topic.publish --source=target/deployment

The entry point must always be set to io.quarkus.gcp.functions.QuarkusBackgroundFunction as this is the class that integrates Cloud Functions with Quarkus.

To trigger the event, you can send a file to the hello_topic topic of you can use gcloud to simulate one:

  1. gcloud functions call quarkus-example-pubsub --data '{"data":{"greeting":"world"}}'

Testing locally

The easiest way to locally test your function is using the Cloud Function invoker JAR.

You can download it via Maven using the following command:

  1. mvn dependency:copy \
  2. -Dartifact='com.google.cloud.functions.invoker:java-function-invoker:1.0.0-beta1' \
  3. -DoutputDirectory=.

Before using the invoker, you first need to build your function via mvn package.

The HttpFunction

To test an HttpFunction, you can use this command to launch your function locally.

  1. java -jar java-function-invoker-1.0.0-beta1.jar \
  2. --classpath target/google-cloud-functions-1.0.0-SNAPSHOT-runner.jar \
  3. --target io.quarkus.gcp.functions.QuarkusHttpFunction
The —classpath parameter needs to be set to the previously packaged JAR that contains your function class and all Quarkus related classes.

Your endpoints will be available on http://localhost:8080.

The BackgroundFunction

For background functions, you launch the invoker with a target class of io.quarkus.gcp.functions.BackgroundFunction.

  1. java -jar java-function-invoker-1.0.0-beta1.jar \
  2. --classpath target/google-cloud-functions-1.0.0-SNAPSHOT-runner.jar \
  3. --target io.quarkus.gcp.functions.QuarkusBackgroundFunction
The —classpath parameter needs to be set to the previously packaged JAR that contains your function class and all Quarkus related classes.

Then you can call your background function via an HTTP call with a payload containing the event:

  1. curl localhost:8080 -d '{"data":{"name":"hello.txt"}}'

This will call your Storage background function with an event {"name":"hello.txt"}, so an event on the hello.txt file.

The RawBackgroundFunction

For background functions, you launch the invoker with a target class of io.quarkus.gcp.functions.BackgroundFunction.

  1. java -jar java-function-invoker-1.0.0-beta1.jar \
  2. --classpath target/google-cloud-functions-1.0.0-SNAPSHOT-runner.jar \
  3. --target io.quarkus.gcp.functions.QuarkusBackgroundFunction
The —classpath parameter needs to be set to the previously packaged JAR that contains your function class and all Quarkus related classes.

Then you can call your background function via an HTTP call with a payload containing the event:

  1. curl localhost:8080 -d '{"data":{"greeting":"world"}}'

This will call your PubSub background function with a PubSubMessage {"greeting":"world"}.