Max Function

    Max Function

    1. * Copyright 2017-2020 original authors
    2. *
    3. * Licensed under the Apache License, Version 2.0 (the "License");
    4. * you may not use this file except in compliance with the License.
    5. * You may obtain a copy of the License at
    6. *
    7. * http://www.apache.org/licenses/LICENSE-2.0
    8. *
    9. * Unless required by applicable law or agreed to in writing, software
    10. * distributed under the License is distributed on an "AS IS" BASIS,
    11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. * See the License for the specific language governing permissions and
    13. * limitations under the License.
    14. */
    15. package io.micronaut.docs.function.client.aws
    16. import groovy.transform.Field
    17. math.multiplier = 2
    18. @Field MathService mathService
    19. Long max() {
    20. mathService.max()
    21. }

    Max Function

    1. /*
    2. * Copyright 2017-2020 original authors
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package io.micronaut.docs.function.client.aws
    17. import io.micronaut.function.FunctionBean
    18. import java.util.function.Supplier
    19. @FunctionBean("max")
    20. class MaxFunction(private val mathService: MathService) : Supplier<Int> {
    21. override fun get(): Int {
    22. return mathService.max()
    23. }
    24. }

    The following client can be used to execute it.

    Using @FunctionClient to Discover Function

    1. import io.micronaut.runtime.server.EmbeddedServer;
    2. import io.micronaut.function.client.FunctionClient;
    3. import org.junit.Test;
    4. import javax.inject.Named;
    5. import static org.junit.Assert.assertEquals;
    6. @FunctionClient
    7. interface MathClient {
    8. Long max(); (1)
    9. }
    1. import io.micronaut.function.client.FunctionClient
    2. import javax.inject.Named
    3. @FunctionClient
    4. static interface MathClient {
    5. Long max() (1)
    6. }

    Using @FunctionClient to Discover Function

    1. import io.kotlintest.shouldBe
    2. import io.kotlintest.specs.StringSpec
    3. import io.micronaut.context.ApplicationContext
    4. import io.micronaut.function.client.FunctionClient
    5. import javax.inject.Named
    6. @FunctionClient
    7. internal interface MathClient {
    8. fun max(): Long? (1)
    9. }

    If you would like the names of the client interface and target function to be different, you can use the annotation to specify the target method name.

    Round Function

    Round Function

    1. /*
    2. * Copyright 2017-2020 original authors
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package io.micronaut.docs.function.client.aws
    17. import groovy.transform.Field
    18. math.multiplier = 2
    19. @Field MathService mathService
    20. int round(float value) {
    21. mathService.round(value)
    22. }

    Round Function

    1. /*
    2. * Copyright 2017-2020 original authors
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package io.micronaut.docs.function.client.aws
    17. import io.micronaut.function.FunctionBean
    18. import java.util.function.Function
    19. @FunctionBean("round")
    20. class RoundFunction(private val mathService: MathService) : Function<Float, Int> {
    21. override fun apply(aFloat: Float): Int {
    22. return mathService.round(aFloat)
    23. }
    24. }

    Using @Named to customize target method

    1. import io.micronaut.runtime.server.EmbeddedServer;
    2. import io.micronaut.function.client.FunctionClient;
    3. import org.junit.Test;
    4. import javax.inject.Named;
    5. import static org.junit.Assert.assertEquals;
    6. @FunctionClient
    7. interface MathClient {
    8. @Named("round")
    9. int rnd(float value);
    10. }

    Using @Named to customize target method

    1. import io.micronaut.function.client.FunctionClient
    2. static interface MathClient {
    3. @Named("round")
    4. int rnd(float value)
    5. }

    Using @Named to customize target method

    1. import io.kotlintest.shouldBe
    2. import io.kotlintest.specs.StringSpec
    3. import io.micronaut.context.ApplicationContext
    4. import io.micronaut.runtime.server.EmbeddedServer
    5. import io.micronaut.function.client.FunctionClient
    6. import io.micronaut.http.client.RxHttpClient
    7. import javax.inject.Named
    8. @FunctionClient
    9. internal interface MathClient {
    10. @Named("round")
    11. fun rnd(value: Float): Int
    12. }

    Functions that only return a value are mapped to HTTP GET requests, whilst functions that accept an input require an HTTP POST.

    For a example, given the following function:

    1. import io.micronaut.function.FunctionBean
    2. import java.util.function.Function
    3. @FunctionBean("isbn-validator")
    4. class IsbnValidatorFunction implements Function<IsbnValidationRequest, IsbnValidationResponse> {
    5. @Override
    6. IsbnValidationResponse apply(IsbnValidationRequest request) {
    7. return new IsbnValidationResponse()
    8. }
    9. }
    1. import io.micronaut.function.FunctionBean
    2. import java.util.function.Function
    3. @FunctionBean("isbn-validator")
    4. class IsbnValidatorFunction : Function<IsbnValidationRequest, IsbnValidationResponse> {
    5. override fun apply(request: IsbnValidationRequest): IsbnValidationResponse {
    6. return IsbnValidationResponse()
    7. }
    8. }
    1. import io.micronaut.function.client.FunctionClient;
    2. import io.micronaut.http.annotation.Body;
    3. import io.reactivex.Single;
    4. import javax.inject.Named;
    5. @FunctionClient
    6. public interface IsbnValidatorClient {
    7. @Named("isbn-validator")
    8. Single<IsbnValidationResponse> validate(@Body IsbnValidationRequest request); (1)
    9. }
    1. import io.micronaut.function.client.FunctionClient;
    2. import io.micronaut.http.annotation.Body;
    3. import io.reactivex.Single;
    4. import javax.inject.Named;
    5. @FunctionClient
    6. interface IsbnValidatorClient {
    7. @Named("isbn-validator")
    8. Single<IsbnValidationResponse> validate(@Body IsbnValidationRequest request) (1)
    9. }
    1. import io.micronaut.function.client.FunctionClient
    2. import io.micronaut.http.annotation.Body
    3. import io.reactivex.Single
    4. import javax.inject.Named
    5. @FunctionClient
    6. interface IsbnValidatorClient {
    7. @Named("isbn-validator")
    8. fun validate(@Body request: IsbnValidationRequest): Single<IsbnValidationResponse> (1)
    1Please, note the @Body annotation in the method parameter.