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:

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
You can find more info about Java Azure Function development in the official documentation at https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven. You can find the developer guide reference at https://docs.microsoft.com/en-us/java/api/overview/azure/functions/readme?view=azure-java-stable.