|
| 1 | +// Module included in the following assemblies |
| 2 | +// |
| 3 | +// * /serverless/functions/serverless-developing-quarkus-functions.adoc |
| 4 | + |
| 5 | +[id="serverless-invoking-quarkus-functions_{context}"] |
| 6 | += About invoking Quarkus functions |
| 7 | + |
| 8 | +You can create a Quarkus project that responds to CloudEvents, or one that responds to simple HTTP requests. CloudEvents in Knative are transported over HTTP as a POST request, so either function type can listen and respond to incoming HTTP requests. |
| 9 | + |
| 10 | +When an incoming request is received, Quarkus functions are invoked with an instance of a permitted type. |
| 11 | + |
| 12 | +.Function invocation options |
| 13 | +[options="header",cols="d,d,m"] |
| 14 | +|==== |
| 15 | +|Invocation method |Data type contained in the instance |Example of data |
| 16 | +|HTTP POST request | JSON object in the body of the request |`{ "customerId": "0123456", "productId": "6543210" }` |
| 17 | +|HTTP GET request | Data in the query string |`?customerId=0123456&productId=6543210` |
| 18 | +|`CloudEvent` | JSON object in the `data` property |`{ "customerId": "0123456", "productId": "6543210" }` |
| 19 | +|==== |
| 20 | + |
| 21 | +The following example shows a function that receives and processes the `customerId` and `productId` purchase data that is listed in the previous table: |
| 22 | + |
| 23 | +.Example Quarkus function |
| 24 | +[source,java] |
| 25 | +---- |
| 26 | +public class Functions { |
| 27 | + @Funq |
| 28 | + public void processPurchase(Purchase purchase) { |
| 29 | + // process the purchase |
| 30 | + } |
| 31 | +} |
| 32 | +---- |
| 33 | + |
| 34 | +The corresponding `Purchase` JavaBean class that contains the purchase data looks as follows: |
| 35 | + |
| 36 | +.Example class |
| 37 | +[source,java] |
| 38 | +---- |
| 39 | +public class Purchase { |
| 40 | + private long customerId; |
| 41 | + private long productId; |
| 42 | + // getters and setters |
| 43 | +} |
| 44 | +---- |
| 45 | + |
| 46 | +[id="serverless-invoking-quarkus-functions-examples_{context}"] |
| 47 | +== Invocation examples |
| 48 | + |
| 49 | +The following example code defines three functions named `withBeans`, `withCloudEvent`, and `withBinary`; |
| 50 | + |
| 51 | +.Example |
| 52 | +[source,java] |
| 53 | +---- |
| 54 | +import io.quarkus.funqy.Funq; |
| 55 | +import io.quarkus.funqy.knative.events.CloudEvent; |
| 56 | +
|
| 57 | +public class Input { |
| 58 | + private String message; |
| 59 | +
|
| 60 | + // getters and setters |
| 61 | +} |
| 62 | +
|
| 63 | +public class Output { |
| 64 | + private String message; |
| 65 | +
|
| 66 | + // getters and setters |
| 67 | +} |
| 68 | +
|
| 69 | +public class Functions { |
| 70 | + @Funq |
| 71 | + public Output withBeans(Input in) { |
| 72 | + // function body |
| 73 | + } |
| 74 | +
|
| 75 | + @Funq |
| 76 | + public CloudEvent<Output> withCloudEvent(CloudEvent<Input> in) { |
| 77 | + // function body |
| 78 | + } |
| 79 | +
|
| 80 | + @Funq |
| 81 | + public void withBinary(byte[] in) { |
| 82 | + // function body |
| 83 | + } |
| 84 | +} |
| 85 | +---- |
| 86 | + |
| 87 | +The `withBeans` function of the `Functions` class can be invoked by: |
| 88 | + |
| 89 | +* An HTTP POST request with a JSON body: |
| 90 | ++ |
| 91 | +[source,terminal] |
| 92 | +---- |
| 93 | +$ curl "http://localhost:8080/withBeans" -X POST \ |
| 94 | + -H "Content-Type: application/json" \ |
| 95 | + -d '{"message": "Hello there."}' |
| 96 | +---- |
| 97 | +* An HTTP GET request with query parameters: |
| 98 | ++ |
| 99 | +[source,terminal] |
| 100 | +---- |
| 101 | +$ curl "http://localhost:8080/withBeans?message=Hello%20there." -X GET |
| 102 | +---- |
| 103 | +* A `CloudEvent` object in binary encoding: |
| 104 | ++ |
| 105 | +[source,terminal] |
| 106 | +---- |
| 107 | +$ curl "http://localhost:8080/" -X POST \ |
| 108 | + -H "Content-Type: application/json" \ |
| 109 | + -H "Ce-SpecVersion: 1.0" \ |
| 110 | + -H "Ce-Type: withBeans" \ |
| 111 | + -H "Ce-Source: cURL" \ |
| 112 | + -H "Ce-Id: 42" \ |
| 113 | + -d '{"message": "Hello there."}' |
| 114 | +---- |
| 115 | +* A `CloudEvent` object in structured encoding: |
| 116 | ++ |
| 117 | +[source,terminal] |
| 118 | +---- |
| 119 | +$ curl http://localhost:8080/ \ |
| 120 | + -H "Content-Type: application/cloudevents+json" \ |
| 121 | + -d '{ "data": {"message":"Hello there."}, |
| 122 | + "datacontenttype": "application/json", |
| 123 | + "id": "42", |
| 124 | + "source": "curl", |
| 125 | + "type": "withBeans", |
| 126 | + "specversion": "1.0"}' |
| 127 | +---- |
| 128 | + |
| 129 | +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. |
| 130 | + |
| 131 | +The `withBinary` function of the `Functions` class can be invoked by: |
| 132 | + |
| 133 | +* A `CloudEvent` object in binary encoding: |
| 134 | ++ |
| 135 | +[source] |
| 136 | +---- |
| 137 | +$ curl "http://localhost:8080/" -X POST \ |
| 138 | + -H "Content-Type: application/octet-stream" \ |
| 139 | + -H "Ce-SpecVersion: 1.0"\ |
| 140 | + -H "Ce-Type: withBinary" \ |
| 141 | + -H "Ce-Source: cURL" \ |
| 142 | + -H "Ce-Id: 42" \ |
| 143 | + --data-binary '@img.jpg' |
| 144 | +---- |
| 145 | +* A `CloudEvent` object in structured encoding: |
| 146 | ++ |
| 147 | +[source] |
| 148 | +---- |
| 149 | +$ curl http://localhost:8080/ \ |
| 150 | + -H "Content-Type: application/cloudevents+json" \ |
| 151 | + -d "{ \"data_base64\": \"$(base64 --wrap=0 img.jpg)\", |
| 152 | + \"datacontenttype\": \"application/octet-stream\", |
| 153 | + \"id\": \"42\", |
| 154 | + \"source\": \"curl\", |
| 155 | + \"type\": \"withBinary\", |
| 156 | + \"specversion\": \"1.0\"}" |
| 157 | +---- |
0 commit comments