GH-600 Fix logic in Azure adapter to ensure proper handling of sveral functions

This also addresses re-initialization of AC when the second function is invoked
Added second function to the azure examples

Resolves #600
This commit is contained in:
Oleg Zhurakousky
2021-03-11 15:26:06 +01:00
parent 29ad49cbf6
commit e82f54d69e
6 changed files with 112 additions and 38 deletions

View File

@@ -1,4 +1,5 @@
You can run this Azure function locally, similar to other Spring Cloud Function samples, however this time by using the Azure Maven plugin, as the Microsoft Azure functions execution context must be available.
You can run this Azure function locally, similar to other Spring Cloud Function samples, however
this time by using the Azure Maven plugin, as the Microsoft Azure functions execution context must be available.
----
# Build and package
@@ -15,8 +16,9 @@ or
$ mvn azure-functions:run
----
The `uppercase` function takes `Function<String, String> uppercase()` and it's input is JSON, therefore we need to
provide the appropriate content-type (in this case `application/json`). The function iterates then over each element and returns its `uppercase` mapped value.
The `uppercase` function takes `Function<String, String> uppercase()` and its expected input is JSON map, therefore we need to
provide the appropriate content-type (in this case `application/json`). The function iterates then over each element
and returns its `uppercase` mapped value.
Test the function using cURL or HTTPie and notice that the URL is formed by concatenating `<function url>/api/<function name>`
----
@@ -33,6 +35,8 @@ $ http POST localhost:7071/api/uppercase greeting=hello name='your name'
}
----
The same is for `echo` function, however it will take any input since all it does is just echos it back.
To run locally on top of Azure Functions, and to deploy to your live Azure environment, you will need the Azure Functions Core Tools installed along with the Azure CLI (see https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven for more details).
To deploy the function to your live Azure environment, including an automatic provisioning of an HTTPTrigger for the function:

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,6 +34,11 @@ public class Config {
SpringApplication.run(Config.class, args);
}
@Bean
public Function<Message<String>, String> echo() {
return message -> message.getPayload();
}
@Bean
public Function<Message<String>, String> uppercase(JsonMapper mapper) {
return message -> {
@@ -46,7 +51,8 @@ public class Config {
map.forEach((k, v) -> map.put(k, v != null ? v.toUpperCase() : null));
if(context != null)
context.getLogger().info(new StringBuilder().append("Function: ").append(context.getFunctionName()).append(" is uppercasing ").append(value.toString()).toString());
context.getLogger().info(new StringBuilder().append("Function: ")
.append(context.getFunctionName()).append(" is uppercasing ").append(value.toString()).toString());
return mapper.toString(map);
} catch (Exception e) {

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2012-2020 the original author or 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
*
* https://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 example;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import java.util.Optional;
import org.springframework.cloud.function.adapter.azure.AzureSpringBootRequestHandler;
/**
* @author Soby Chacko
*/
public class EchoHandler extends AzureSpringBootRequestHandler<String, String> {
@FunctionName("echo")
public String execute(@HttpTrigger(name = "req", methods = {HttpMethod.GET,
HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
ExecutionContext context) {
return handleRequest(request.getBody().get(), context);
}
}