FunctionClient

The @FunctionClient annotation makes it very straightforward to invoke local or remotely deployed functions. For example, given the following function:

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. public class MaxFunction implements Supplier<Integer> {
  21. private final MathService mathService;
  22. public MaxFunction(MathService mathService) {
  23. this.mathService = mathService;
  24. }
  25. @Override
  26. public Integer get() {
  27. return mathService.max();
  28. }
  29. }

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 groovy.transform.Field
  18. math.multiplier = 2
  19. @Field MathService mathService
  20. Long max() {
  21. mathService.max()
  22. }

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. }

Using @FunctionClient to Discover Function

  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.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. fun max(): Long? (1)
  11. }
1Method names in the interface will be mapped to methods on the target function, in this case Long max()

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

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. public class RoundFunction implements Function<Float, Integer> {
  21. private final MathService mathService;
  22. public RoundFunction(MathService mathService) {
  23. this.mathService = mathService;
  24. }
  25. @Override
  26. public Integer apply(Float aFloat) {
  27. return mathService.round(aFloat);
  28. }
  29. }

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. }

The named annotation can be placed on the client as well.

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. import javax.inject.Named
  3. @FunctionClient
  4. static interface MathClient {
  5. @Named("round")
  6. int rnd(float value)
  7. }

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. public class IsbnValidatorFunction implements Function<IsbnValidationRequest, IsbnValidationResponse> {
  5. @Override
  6. public 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 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. }

The function can be accessed using the IsbnValidatorClient interface listed below.

  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)
  9. }
1Please, note the @Body annotation in the method parameter.