FunctionClient
The @FunctionClient annotation makes it very straightforward to invoke local or remotely deployed functions. For example, given the following function:
Max Function
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.function.client.aws;
import io.micronaut.function.FunctionBean;
import java.util.function.Supplier;
@FunctionBean("max")
public class MaxFunction implements Supplier<Integer> {
private final MathService mathService;
public MaxFunction(MathService mathService) {
this.mathService = mathService;
}
@Override
public Integer get() {
return mathService.max();
}
}
Max Function
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.function.client.aws
import groovy.transform.Field
math.multiplier = 2
@Field MathService mathService
Long max() {
mathService.max()
}
Max Function
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.function.client.aws
import io.micronaut.function.FunctionBean
import java.util.function.Supplier
@FunctionBean("max")
class MaxFunction(private val mathService: MathService) : Supplier<Int> {
override fun get(): Int {
return mathService.max()
}
}
The following client can be used to execute it.
Using @FunctionClient to Discover Function
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.function.client.FunctionClient;
import org.junit.Test;
import javax.inject.Named;
import static org.junit.Assert.assertEquals;
@FunctionClient
interface MathClient {
Long max(); (1)
}
Using @FunctionClient to Discover Function
import io.micronaut.function.client.FunctionClient
import javax.inject.Named
@FunctionClient
static interface MathClient {
Long max() (1)
}
Using @FunctionClient to Discover Function
import io.kotlintest.shouldBe
import io.kotlintest.specs.StringSpec
import io.micronaut.context.ApplicationContext
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.function.client.FunctionClient
import io.micronaut.http.client.RxHttpClient
import javax.inject.Named
@FunctionClient
internal interface MathClient {
fun max(): Long? (1)
}
1 | Method 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
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.function.client.aws;
import io.micronaut.function.FunctionBean;
import java.util.function.Function;
@FunctionBean("round")
public class RoundFunction implements Function<Float, Integer> {
private final MathService mathService;
public RoundFunction(MathService mathService) {
this.mathService = mathService;
}
@Override
public Integer apply(Float aFloat) {
return mathService.round(aFloat);
}
}
Round Function
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.function.client.aws
import groovy.transform.Field
math.multiplier = 2
@Field MathService mathService
int round(float value) {
mathService.round(value)
}
Round Function
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.docs.function.client.aws
import io.micronaut.function.FunctionBean
import java.util.function.Function
@FunctionBean("round")
class RoundFunction(private val mathService: MathService) : Function<Float, Int> {
override fun apply(aFloat: Float): Int {
return mathService.round(aFloat)
}
}
The named annotation can be placed on the client as well.
Using @Named to customize target method
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.function.client.FunctionClient;
import org.junit.Test;
import javax.inject.Named;
import static org.junit.Assert.assertEquals;
@FunctionClient
interface MathClient {
@Named("round")
int rnd(float value);
}
Using @Named to customize target method
import io.micronaut.function.client.FunctionClient
import javax.inject.Named
@FunctionClient
static interface MathClient {
@Named("round")
int rnd(float value)
}
Using @Named to customize target method
import io.kotlintest.shouldBe
import io.kotlintest.specs.StringSpec
import io.micronaut.context.ApplicationContext
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.function.client.FunctionClient
import io.micronaut.http.client.RxHttpClient
import javax.inject.Named
@FunctionClient
internal interface MathClient {
@Named("round")
fun rnd(value: Float): Int
}
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:
import io.micronaut.function.FunctionBean;
import java.util.function.Function;
@FunctionBean("isbn-validator")
public class IsbnValidatorFunction implements Function<IsbnValidationRequest, IsbnValidationResponse> {
@Override
public IsbnValidationResponse apply(IsbnValidationRequest request) {
return new IsbnValidationResponse();
}
}
import io.micronaut.function.FunctionBean
import java.util.function.Function
@FunctionBean("isbn-validator")
class IsbnValidatorFunction implements Function<IsbnValidationRequest, IsbnValidationResponse> {
@Override
IsbnValidationResponse apply(IsbnValidationRequest request) {
return new IsbnValidationResponse()
}
}
import io.micronaut.function.FunctionBean
import java.util.function.Function
@FunctionBean("isbn-validator")
class IsbnValidatorFunction : Function<IsbnValidationRequest, IsbnValidationResponse> {
override fun apply(request: IsbnValidationRequest): IsbnValidationResponse {
return IsbnValidationResponse()
}
}
The function can be accessed using the IsbnValidatorClient
interface listed below.
import io.micronaut.function.client.FunctionClient;
import io.micronaut.http.annotation.Body;
import io.reactivex.Single;
import javax.inject.Named;
@FunctionClient
public interface IsbnValidatorClient {
@Named("isbn-validator")
Single<IsbnValidationResponse> validate(@Body IsbnValidationRequest request); (1)
}
import io.micronaut.function.client.FunctionClient;
import io.micronaut.http.annotation.Body;
import io.reactivex.Single;
import javax.inject.Named;
@FunctionClient
interface IsbnValidatorClient {
@Named("isbn-validator")
Single<IsbnValidationResponse> validate(@Body IsbnValidationRequest request) (1)
}
import io.micronaut.function.client.FunctionClient
import io.micronaut.http.annotation.Body
import io.reactivex.Single
import javax.inject.Named
@FunctionClient
interface IsbnValidatorClient {
@Named("isbn-validator")
fun validate(@Body request: IsbnValidationRequest): Single<IsbnValidationResponse> (1)
}
1 | Please, note the @Body annotation in the method parameter. |