Max 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 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)
}
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.function.client.FunctionClient
import javax.inject.Named
@FunctionClient
internal interface MathClient {
fun max(): Long? (1)
}
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
/*
* 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)
}
}
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
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")
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()
}
}
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. |