Enhance README documentation (#28)
Add link back to Java Dev Guide, reference Maven plugin, and other minor adjustments.
This commit is contained in:
Родитель
017a0c5835
Коммит
a0ad533419
64
README.md
64
README.md
|
@ -1,27 +1,26 @@
|
|||
# Building Microsoft Azure Functions in Java
|
||||
# Microsoft Azure Functions API for Java
|
||||
This project contains the Java API for building functions for the Azure Functions service. Visit the [complete documentation of Azure Functions - Java Developer Guide](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-java) for more details.
|
||||
|
||||
## azure-functions-maven plugin
|
||||
[How to use azure-functions-maven plugin to create, update, deploy and test azure java functions](https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-functions-maven-plugin/readme?view=azure-java-stable)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* JDK 8
|
||||
* Maven
|
||||
* JAVA_HOME environment variable
|
||||
* Node
|
||||
* NPM Azure Functions CLI
|
||||
* Java 8
|
||||
|
||||
## Programming Model
|
||||
## Summary
|
||||
|
||||
Your Azure function should be a stateless method to process input and produce output. Although you are allowed to write instance methods, your function must not depend on any instance fields of the class. You need to make sure all the function methods are `public` accessible.
|
||||
Azure Functions is capable of running Function Apps that may contain one or more functions grouped. A function should be a stateless method to process input and produce output. Although you are allowed to write instance methods, your function must not depend on any instance fields of the class. You need to make sure all the function methods are `public` accessible.
|
||||
|
||||
You can put multiple functions in one single project (or specifically speaking, one single jar). We strongly recommend you **not to** put your functions in separate jars (or `pom.xml`).
|
||||
A deployable unit is an uber JAR containing one or more functions (see below), and a JSON file with the list of functions and triggers definitions, deployed to Azure Functions. The JAR can be created in many ways, although we recommend the use of the [Azure Functions Maven Plugin](https://docs.microsoft.com/en-us/java/api/overview/azure/maven/azure-functions-maven-plugin/readme), which also generates the JSON file for you automatically.
|
||||
|
||||
Typically an Azure function is invoked because of one trigger. Your function needs to process that trigger (sometimes with additional inputs) and gives one or more output values.
|
||||
Typically a function is invoked because of a trigger. Your function needs to process that trigger (sometimes with additional inputs) and provide an optional output.
|
||||
|
||||
All the input and output bindings can be defined in `function.json` (not recommended), or in the Java method by using annotations (recommended). All the types and annotations used in this document are included in the `azure-functions-java-library` package.
|
||||
|
||||
Here is an example for a simple Azure function written in Java:
|
||||
### Sample
|
||||
|
||||
Here is an example for a simple Azure Function written in Java:
|
||||
|
||||
```Java
|
||||
package com.example;
|
||||
|
@ -36,22 +35,24 @@ public class MyClass {
|
|||
}
|
||||
```
|
||||
|
||||
### Including 3rd Party Libraries
|
||||
### Adding 3rd Party Libraries
|
||||
|
||||
Azure Functions supports the use of 3rd party libraries. By default, all dependencies specified in your project pom.xml file will be automatically bundled during the `mvn package` step. For libraries that are not specified as dependencies in the pom.xml file, you may place them in a `lib` directory in the function's root directory. These will then be deployed as part of your functions application. All dependencies that are placed in the `lib` directory will be added to the system class loader at runtime.
|
||||
Azure Functions supports the use of 3rd party libraries. If using the Maven plugin for Azure Functions, all of your dependencies specified in your `pom.xml` file will be automatically bundled during the `mvn package` step.
|
||||
|
||||
## General Data Types
|
||||
## Data Types
|
||||
|
||||
You are free to use all the data types in Java for the input and output data, including native types; customized POJO types and specialized Azure types defined in `azure-functions-java-library` package. And Azure Functions runtime will try its best to convert the actual input value to the type you need (for example, a `String` input will be treated as a JSON string and be parsed to a POJO type defined in your code).
|
||||
You are free to use all the data types in Java for the input and output data, including native types; customized POJO types and specialized Azure types defined in this API. Azure Functions runtime will try its best to convert the actual input value to the type you need (for example, a `String` input will be treated as a JSON string and be parsed to a POJO type defined in your code).
|
||||
|
||||
The POJO types you defined have the same accessible requirements as the function methods, it needs to be `public` accessible. While the POJO class fields are not; for example a JSON string `{ "x": 3 }` is able to be converted to the following POJO type:
|
||||
### JSON Support
|
||||
The POJO types (Java classes) you may define have to be publicly accessible (`public` modifier). POJO properties/fields may be `private`. For example a JSON string `{ "x": 3 }` is able to be converted to the following POJO type:
|
||||
|
||||
```Java
|
||||
```java
|
||||
public class MyData {
|
||||
private int x;
|
||||
}
|
||||
```
|
||||
|
||||
### Other supported types
|
||||
Binary data is represented as `byte[]` or `Byte[]` in your Azure functions code. And make sure you specify `dataType = "binary"` in the corresponding triggers/bindings.
|
||||
|
||||
Empty input values could be `null` as your functions argument, but a recommended way to deal with potential empty values is to use `Optional<T>` type.
|
||||
|
@ -63,25 +64,27 @@ Inputs are divided into two categories in Azure Functions: one is the trigger in
|
|||
|
||||
Let's take the following code snippet as an example:
|
||||
|
||||
```Java
|
||||
```java
|
||||
package com.example;
|
||||
|
||||
import com.microsoft.azure.functions.annotation.*;
|
||||
|
||||
public class MyClass {
|
||||
@FunctionName("echo")
|
||||
public static String echo(
|
||||
public String echo(
|
||||
@HttpTrigger(name = "req", methods = { "put" }, authLevel = AuthorizationLevel.ANONYMOUS, route = "items/{id}") String in,
|
||||
@TableInput(name = "item", tableName = "items", partitionKey = "Example", rowKey = "{id}", connection = "AzureWebJobsStorage") MyObject obj
|
||||
@TableInput(name = "item", tableName = "items", partitionKey = "example", rowKey = "{id}", connection = "AzureWebJobsStorage") MyObject obj
|
||||
) {
|
||||
return "Hello, " + in + " and " + obj.getKey() + ".";
|
||||
return "Hello, " + in + " and " + obj.getRowKey() + ".";
|
||||
}
|
||||
|
||||
public static class MyObject {
|
||||
public String getKey() { return this.RowKey; }
|
||||
private String RowKey;
|
||||
}
|
||||
}
|
||||
|
||||
public class MyObject {
|
||||
public String getRowKey() { return this.rowKey; }
|
||||
private String rowKey;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
When this function is invoked, the HTTP request payload will be passed as the `String` for argument `in`; and one entry will be retrieved from the Azure Table Storage and be passed to argument `obj` as `MyObject` type.
|
||||
|
@ -94,7 +97,7 @@ Return value is the simplest form of output, you just return the value of any ty
|
|||
|
||||
For example, a blob content copying function could be defined as the following code. `@StorageAccount` annotation is used here to prevent the duplicating of the `connection` property for both `@BlobTrigger` and `@BlobOutput`.
|
||||
|
||||
```Java
|
||||
```java
|
||||
package com.example;
|
||||
|
||||
import com.microsoft.azure.functions.annotation.*;
|
||||
|
@ -103,7 +106,7 @@ public class MyClass {
|
|||
@FunctionName("copy")
|
||||
@StorageAccount("AzureWebJobsStorage")
|
||||
@BlobOutput(name = "$return", path = "samples-output-java/{name}")
|
||||
public static String copy(@BlobTrigger(name = "blob", path = "samples-input-java/{name}") String content) {
|
||||
public String copy(@BlobTrigger(name = "blob", path = "samples-input-java/{name}") String content) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
@ -111,7 +114,7 @@ public class MyClass {
|
|||
|
||||
To produce multiple output values, use `OutputBinding<T>` type defined in the `azure-functions-java-library` package. If you need to make an HTTP response and push a message to a queue, you can write something like:
|
||||
|
||||
```Java
|
||||
```java
|
||||
package com.example;
|
||||
|
||||
import com.microsoft.azure.functions.*;
|
||||
|
@ -119,7 +122,7 @@ import com.microsoft.azure.functions.annotation.*;
|
|||
|
||||
public class MyClass {
|
||||
@FunctionName("push")
|
||||
public static String push(
|
||||
public String push(
|
||||
@HttpTrigger(name = "req", methods = { "post" }, authLevel = AuthorizationLevel.ANONYMOUS) String body,
|
||||
@QueueOutput(name = "message", queueName = "myqueue", connection = "AzureWebJobsStorage") OutputBinding<String> queue
|
||||
) {
|
||||
|
@ -131,14 +134,13 @@ public class MyClass {
|
|||
|
||||
Of course you could use `OutputBinding<byte[]>` type to make a binary output value (for parameters); for return values, just use `byte[]`.
|
||||
|
||||
|
||||
## Execution Context
|
||||
|
||||
You interact with Azure Functions execution environment via the `ExecutionContext` object defined in the `azure-functions-java-library` package. You are able to get the invocation ID, the function name and a built-in logger (which is integrated prefectly with Azure Function Portal experience as well as AppInsights) from the context object.
|
||||
|
||||
What you need to do is just add one more `ExecutionContext` typed parameter to your function method. Let's take a timer triggered function as an example:
|
||||
|
||||
```Java
|
||||
```java
|
||||
package com.example;
|
||||
|
||||
import com.microsoft.azure.functions.*;
|
||||
|
|
Загрузка…
Ссылка в новой задаче