11.8.3. Quarkus 함수 호출 정보

클라우드 이벤트에 응답하는 Quarkus 프로젝트 또는 간단한 HTTP 요청에 응답하는 Quarkus 프로젝트를 생성할 수 있습니다. Knative의 클라우드 이벤트는 HTTP를 통해 POST 요청으로 전송되므로 두 기능 유형 모두 들어오는 HTTP 요청을 수신하고 응답할 수 있습니다.

들어오는 요청이 수신되면 Quarkus 함수가 허용된 유형의 인스턴스와 함께 호출됩니다.

표 11.1. 함수 호출 옵션

호출 메소드인스턴스에 포함된 데이터 유형데이터 예

HTTP POST 요청

요청 본문에 있는 JSON 오브젝트

{ "customerId": "0123456", "productId": "6543210" }

HTTP GET 요청

쿼리 문자열의 데이터

?customerId=0123456&productId=6543210

CloudEvent

data 속성의 JSON 개체

{ "customerId": "0123456", "productId": "6543210" }

다음 예제에서는 이전 표에 나열된 customerIdproductId 구매 데이터를 수신하고 처리하는 함수를 보여줍니다.

Quarkus 함수의 예

public class Functions {
    @Funq
    public void processPurchase(Purchase purchase) {
        // process the purchase
    }
}

구매 데이터를 포함하는 Purchase JavaBean 클래스는 다음과 같습니다.

클래스 예

public class Purchase {
    private long customerId;
    private long productId;
    // getters and setters
}

11.8.3.1. 호출 예

다음 예제 코드는 withBeans, withCloudEvent, withBinary라는 세 가지 함수를 정의합니다.

예제

import io.quarkus.funqy.Funq;
import io.quarkus.funqy.knative.events.CloudEvent;

public class Input {
    private String message;

    // getters and setters
}

public class Output {
    private String message;

    // getters and setters
}

public class Functions {
    @Funq
    public Output withBeans(Input in) {
        // function body
    }

    @Funq
    public CloudEvent<Output> withCloudEvent(CloudEvent<Input> in) {
        // function body
    }

    @Funq
    public void withBinary(byte[] in) {
        // function body
    }
}

Functions 클래스의 withBeans 함수는 다음을 통해 호출할 수 있습니다.

  • JSON 본문이 있는 HTTP POST 요청:

    $ curl "http://localhost:8080/withBeans" -X POST \
        -H "Content-Type: application/json" \
        -d '{"message": "Hello there."}'
  • 쿼리 매개변수가 있는 HTTP GET 요청:

    $ curl "http://localhost:8080/withBeans?message=Hello%20there." -X GET
  • 바이너리 인코딩의 CloudEvent 오브젝트:

    $ curl "http://localhost:8080/" -X POST \
      -H "Content-Type: application/json" \
      -H "Ce-SpecVersion: 1.0" \
      -H "Ce-Type: withBeans" \
      -H "Ce-Source: cURL" \
      -H "Ce-Id: 42" \
      -d '{"message": "Hello there."}'
  • 구조화된 인코딩의 CloudEvent 오브젝트:

    $ curl http://localhost:8080/ \
        -H "Content-Type: application/cloudevents+json" \
        -d '{ "data": {"message":"Hello there."},
              "datacontenttype": "application/json",
              "id": "42",
              "source": "curl",
              "type": "withBeans",
              "specversion": "1.0"}'

Functions 클래스의 withCloudEvent 함수는 withBeans 함수와 유사하게 CloudEvent 오브젝트를 사용하여 호출할 수 있습니다. 그러나 withBeans와 달리withCloudEvent는 일반 HTTP 요청으로 호출할 수 없습니다.

Functions 클래스의 withBinary 함수는 다음을 통해 호출할 수 있습니다.

  • 바이너리 인코딩의 CloudEvent 오브젝트:

    $ curl "http://localhost:8080/" -X POST \
      -H "Content-Type: application/octet-stream" \
      -H "Ce-SpecVersion: 1.0"\
      -H "Ce-Type: withBinary" \
      -H "Ce-Source: cURL" \
      -H "Ce-Id: 42" \
      --data-binary '@img.jpg'
  • 구조화된 인코딩의 CloudEvent 오브젝트:

    $ curl http://localhost:8080/ \
      -H "Content-Type: application/cloudevents+json" \
      -d "{ \"data_base64\": \"$(base64 --wrap=0 img.jpg)\",
            \"datacontenttype\": \"application/octet-stream\",
            \"id\": \"42\",
            \"source\": \"curl\",
            \"type\": \"withBinary\",
            \"specversion\": \"1.0\"}"