Chapter 4. Developing Quarkus functions
After you have created a Quarkus function project, you can modify the template files provided to add business logic to your function. This includes configuring function invocation and the returned headers and status codes.
4.1. Prerequisites Copy linkLink copied to clipboard!
- Before you can develop functions, you must complete the setup steps in Setting up OpenShift Serverless Functions.
4.2. Quarkus function template structure Copy linkLink copied to clipboard!
When you create a Quarkus function by using the Knative (kn
) CLI, the project directory looks similar to a typical Maven project. Additionally, the project contains the func.yaml
file, which is used for configuring the function.
Both http
and event
trigger functions have the same template structure:
Template structure
- 1
- Used to determine the image name and registry.
- 2
- The Project Object Model (POM) file contains project configuration, such as information about dependencies. You can add additional dependencies by modifying this file.
Example of additional dependencies
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Dependencies are downloaded during the first compilation.
- 3
- The function project must contain a Java method annotated with
@Funq
. You can place this method in theFunction.java
class. - 4
- Contains simple test cases that can be used to test your function locally.
4.3. About invoking Quarkus functions Copy linkLink copied to clipboard!
You can create a Quarkus project that responds to cloud events, or one that responds to simple HTTP requests. Cloud events in Knative are transported over HTTP as a POST request, so either function type can listen and respond to incoming HTTP requests.
When an incoming request is received, Quarkus functions are invoked with an instance of a permitted type.
Invocation method | Data type contained in the instance | Example of data |
---|---|---|
HTTP POST request | JSON object in the body of the request |
|
HTTP GET request | Data in the query string |
|
|
JSON object in the |
|
The following example shows a function that receives and processes the customerId
and productId
purchase data that is listed in the previous table:
Example Quarkus function
The corresponding Purchase
JavaBean class that contains the purchase data looks as follows:
Example class
public class Purchase { private long customerId; private long productId; // getters and setters }
public class Purchase {
private long customerId;
private long productId;
// getters and setters
}
4.3.1. Invocation examples Copy linkLink copied to clipboard!
The following example code defines three functions named withBeans
, withCloudEvent
, and withBinary
;
Example
The withBeans
function of the Functions
class can be invoked by:
An HTTP POST request with a JSON body:
curl "http://localhost:8080/withBeans" -X POST \ -H "Content-Type: application/json" \ -d '{"message": "Hello there."}'
$ curl "http://localhost:8080/withBeans" -X POST \ -H "Content-Type: application/json" \ -d '{"message": "Hello there."}'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow An HTTP GET request with query parameters:
curl "http://localhost:8080/withBeans?message=Hello%20there." -X GET
$ curl "http://localhost:8080/withBeans?message=Hello%20there." -X GET
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A
CloudEvent
object in binary encoding:Copy to Clipboard Copied! Toggle word wrap Toggle overflow A
CloudEvent
object in structured encoding:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The withCloudEvent
function of the Functions
class can be invoked by using a CloudEvent
object, similarly to the withBeans
function. However, unlike withBeans
, withCloudEvent
cannot be invoked with a plain HTTP request.
The withBinary
function of the Functions
class can be invoked by:
A
CloudEvent
object in binary encoding:Copy to Clipboard Copied! Toggle word wrap Toggle overflow A
CloudEvent
object in structured encoding:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.4. CloudEvent attributes Copy linkLink copied to clipboard!
If you need to read or write the attributes of a CloudEvent, such as type
or subject
, you can use the CloudEvent<T>
generic interface and the CloudEventBuilder
builder. The <T>
type parameter must be one of the permitted types.
In the following example, CloudEventBuilder
is used to return success or failure of processing the purchase:
4.5. Quarkus function return values Copy linkLink copied to clipboard!
Functions can return an instance of any type from the list of permitted types. Alternatively, they can return the Uni<T>
type, where the <T>
type parameter can be of any type from the permitted types.
The Uni<T>
type is useful if a function calls asynchronous APIs, because the returned object is serialized in the same format as the received object. For example:
- If a function receives an HTTP request, then the returned object is sent in the body of an HTTP response.
-
If a function receives a
CloudEvent
object in binary encoding, then the returned object is sent in the data property of a binary-encodedCloudEvent
object.
The following example shows a function that fetches a list of purchases:
Example command
- Invoking this function through an HTTP request produces an HTTP response that contains a list of purchases in the body of the response.
-
Invoking this function through an incoming
CloudEvent
object produces aCloudEvent
response with a list of purchases in thedata
property.
4.5.1. Permitted types Copy linkLink copied to clipboard!
The input and output of a function can be any of the void
, String
, or byte[]
types. Additionally, they can be primitive types and their wrappers, for example, int
and Integer
. They can also be the following complex objects: Javabeans, maps, lists, arrays, and the special CloudEvents<T>
type.
Maps, lists, arrays, the <T>
type parameter of the CloudEvents<T>
type, and attributes of Javabeans can only be of types listed here.
Example
4.6. Testing Quarkus functions Copy linkLink copied to clipboard!
Quarkus functions can be tested locally on your computer. In the default project that is created when you create a function using kn func create
, there is the src/test/
directory, which contains basic Maven tests. These tests can be extended as needed.
Prerequisites
- You have created a Quarkus function.
-
You have installed the Knative (
kn
) CLI.
Procedure
- Navigate to the project folder for your function.
Run the Maven tests:
./mvnw test
$ ./mvnw test
Copy to Clipboard Copied! Toggle word wrap Toggle overflow