- Mastering Azure Serverless Computing
- Lorenzo Barbieri Massimo Bonanni
- 397字
- 2021-06-24 12:37:13
Azure Functions and Java
Despite the fact that the Azure Functions Runtime supports Java, you cannot create a Java Azure Function using the Azure Function Core Tool in the same way that you created a JavaScript function in the previous section.
To create a Java Azure Function you need the Azure Function Core Tools, but you also must have the following:
- Java Developer Kit (JDK) SE LTE version 8 (https://repos.azul.com/azure-only/zulu/packages/zulu-8/8u181/)
- Apache Maven version 3.0 or above (https://maven.apache.org/download.cgi)
The process to create a Java Azure Function is not simple and linear, but in the end, a Java Azure Function is a public method decorated with the @FunctionName annotation (as the FunctionName attribute for the C# functions). This method is the entry point for the function and it must be unique in the entire package.
The structure of a project written in Java that contains an Azure Function resembles the following:

The target folder contains the binaries of the functions, that is, the set of files to deploy.
The host.json file is used, as it is for the C# and Node.js functions, to configure the function app and to define the extensions you want to use inside your functions. Each function has its folder (with the same name as the function), in which you will find the function.json file that contains the definition of the bindings, as shown in the following code:
{
"scriptFile": "myFirstJavaFunction.jar",
"entryPoint": "com.example.Function.httpTriggerFunction",
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"direction": "in",
"authLevel": "function",
"methods": [ "get" ]
},
{
"type": "http",
"name": "$return",
"direction": "out"
}
]
}
The previous function.json corresponds to the function, as shown in the following code:
public class Function {
@FunctionName("httpTriggerFunction")
public String httpTriggerFunction(@HttpTrigger(name = "req", methods = {"get"}, authLevel = AuthorizationLevel.FUNCTION) String req,
ExecutionContext context)
{
return String.format(req);
}
}
As you can see in the previous function, one of the arguments is an instance of ExecutionContext. It is similar to the context you saw in the Node.js paragraph. It allows you to access log features.
If you want to host your Java functions locally, you have to use Maven, and in particular, you need to run the following commands inside the root folder of your function project:
mvn clean package
mvn myFirstJavaFunction:run
- Mastering vRealize Operations Manager(Second Edition)
- Implementing Cisco UCS Solutions
- Ansible權威指南
- Arch Linux Environment Setup How-to
- Persistence in PHP with the Doctrine ORM
- Ubuntu Linux操作系統
- 構建可擴展分布式系統:方法與實踐
- Linux網絡內核分析與開發
- 異質結原理與器件
- 嵌入式實時操作系統:RT-Thread設計與實現
- 一學就會:Windows Vista應用完全自學手冊
- CentOS 6 Linux Server Cookbook
- Windows 7使用詳解(修訂版)
- 嵌入式微系統
- VMware Horizon Mirage Essentials