Skip to content

Commit 5c79beb

Browse files
committed
2 parents f0d78c7 + 81206a2 commit 5c79beb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+760
-118
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
hs_err_pid*
2424
/.idea/*
2525
/target/*
26+
/bin
27+
/.classpath
28+
/.settings/*
29+
.project

README.md

+9-59
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,6 @@
1-
# Azure Functions Java Library
2-
3-
This package contains libraries to author Azure Functions in Java.
4-
5-
### Usage
6-
Use this package by including the following snippet in your `pom.xml`.
7-
8-
```xml
9-
<dependency>
10-
<groupId>com.microsoft.azure.functions</groupId>
11-
<artifactId>azure-functions-java-library</artifactId>
121
<version>1.0.0-beta-4</version>
13-
</dependency>
14-
```
15-
16-
### Annotation for Triggers and Bindings
17-
Instead of writing `function.json`, you can use annotations in code to specify triggers and bindings of your functions.
18-
`function.json` can be automatically generated by [Maven Plugin for Azure Functions](https://github.com/Microsoft/azure-maven-plugins/tree/master/azure-functions-maven-plugin).
19-
20-
The following table lists the Java annotations for each binding type.
21-
22-
Binding | Annotation
23-
---|---
24-
HTTP | <ul><li>`HttpTrigger`</li><li>`HttpOutput`</li></ul>
25-
Storage Blob | <ul><li>`BlobTrigger`</li><li>`BlobInput`</li><li>`BlobOutput`</li></ul>
26-
Storage Queue | <ul><li>`QueueTrigger`</li><li>`QueueOutput`</li></ul>
27-
Storage Table | <ul><li>`TableInput`</li><li>`TableOutput`</li></ul>
28-
Timer | <ul><li>`TimerTrigger`</li></ul>
29-
302
# Building Microsoft Azure Functions in Java
313

32-
334
## Prerequisites
345

356
* JDK 8
@@ -42,6 +13,8 @@ Timer | <ul><li>`TimerTrigger`</li></ul>
4213

4314
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.
4415

16+
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`).
17+
4518
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.
4619

4720
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.
@@ -63,36 +36,7 @@ public class MyClass {
6336

6437
### Including 3rd Party Libraries
6538

66-
Azure Functions only accept one single JAR file. Therefore you are required to package all your dependencies into one single JAR. A simple approach is to add the following plugin into your `pom.xml`:
67-
68-
```xml
69-
<plugin>
70-
<artifactId>maven-shade-plugin</artifactId>
71-
<version>3.1.0</version>
72-
<executions>
73-
<execution>
74-
<phase>package</phase>
75-
<goals>
76-
<goal>shade</goal>
77-
</goals>
78-
<configuration>
79-
<filters>
80-
<filter>
81-
<artifact>*:*</artifact>
82-
<excludes>
83-
<exclude>META-INF/*.SF</exclude>
84-
<exclude>META-INF/*.DSA</exclude>
85-
<exclude>META-INF/*.RSA</exclude>
86-
</excludes>
87-
</filter>
88-
</filters>
89-
</configuration>
90-
</execution>
91-
</executions>
92-
</plugin>
93-
```
94-
95-
Sometimes you also need to care about the static constructors used in some libraries (for example, database drivers). You need to call [`Class.forName()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#forName-java.lang.String-) method to invoke the corresponding static constructor (for example `Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");`).
39+
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.
9640

9741
## General Data Types
9842

@@ -243,3 +187,9 @@ public class MyClass {
243187
}
244188
}
245189
```
190+
191+
### License
192+
193+
This project is under the benevolent umbrella of the [.NET Foundation](http://www.dotnetfoundation.org/) and is licensed under [the MIT License](LICENSE.txt)
194+
195+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

pom.xml

+113-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,117 @@
77

88
<name>Microsoft Azure Functions Java Core Types</name>
99
<description>This package contains all Java interfaces and annotations to interact with Microsoft Azure functions runtime.</description>
10-
<url>https://azure.microsoft.com/en-us/services/functions</url>
10+
<url>https://azure.microsoft.com/en-us/services/functions</url>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<junit.version>4.12</junit.version>
15+
<mockito.version>2.11.0</mockito.version>
16+
</properties>
17+
18+
<licenses>
19+
<license>
20+
<name>MIT License</name>
21+
<url>https://opensource.org/licenses/MIT</url>
22+
<distribution>repo</distribution>
23+
</license>
24+
</licenses>
25+
26+
<scm>
27+
<connection>scm:git:https://github.com/Azure/azure-functions-java-worker</connection>
28+
<developerConnection>scm:git:[email protected]:Azure/azure-functions-java-worker</developerConnection>
29+
<url>https://github.com/Azure/azure-functions-java-worker</url>
30+
<tag>HEAD</tag>
31+
</scm>
32+
33+
<developers>
34+
<developer>
35+
<id>junyi</id>
36+
<name>Junyi Yi</name>
37+
<email>[email protected]</email>
38+
</developer>
39+
<developer>
40+
<id>xscript</id>
41+
<name>Kevin Zhao</name>
42+
<email>[email protected]</email>
43+
</developer>
44+
</developers>
45+
46+
<dependencies>
47+
48+
<!-- test -->
49+
<dependency>
50+
<groupId>org.reflections</groupId>
51+
<artifactId>reflections</artifactId>
52+
<version>0.9.11</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>${junit.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-core</artifactId>
64+
<version>${mockito.version}</version>
65+
<scope>test</scope>
66+
</dependency>
67+
68+
</dependencies>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<artifactId>maven-compiler-plugin</artifactId>
74+
<version>3.7.0</version>
75+
<configuration>
76+
<source>1.8</source>
77+
<target>1.8</target>
78+
</configuration>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-source-plugin</artifactId>
83+
<version>3.0.1</version>
84+
<executions>
85+
<execution>
86+
<id>attach-sources</id>
87+
<goals>
88+
<goal>jar</goal>
89+
</goals>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-javadoc-plugin</artifactId>
96+
<version>3.0.0-M1</version>
97+
<executions>
98+
<execution>
99+
<id>attach-javadocs</id>
100+
<goals>
101+
<goal>jar</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
<plugin>
107+
<groupId>org.apache.maven.plugins</groupId>
108+
<artifactId>maven-surefire-plugin</artifactId>
109+
<version>2.21.0</version>
110+
<configuration>
111+
<workingDirectory>${project.build.directory}</workingDirectory>
112+
<systemProperties>
113+
<property>
114+
<name>testing-project-jar</name>
115+
<value>${project.artifactId}-${project.version}-tests.jar</value>
116+
</property>
117+
</systemProperties>
118+
</configuration>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
11123
</project>

src/main/java/com/microsoft/azure/functions/ExecutionContext.java

+22
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,30 @@
22

33
import java.util.logging.Logger;
44

5+
/**
6+
* The execution context enables interaction with the Azure Functions execution environment.
7+
*
8+
* @since 1.0.0
9+
*/
510
public interface ExecutionContext {
11+
/**
12+
* Returns the built-in logger, which is integrated with the logging functionality provided in the Azure Functions
13+
* portal, as well as in Azure Application Insights.
14+
*
15+
* @return A Java logger that will see output directed to Azure Portal, as well as any other configured output
16+
* locations.
17+
*/
618
Logger getLogger();
19+
20+
/**
21+
* Returns the invocation ID for the function call.
22+
* @return the invocation ID for the function call.
23+
*/
724
String getInvocationId();
25+
26+
/**
27+
* Returns the function name.
28+
* @return the function name.
29+
*/
830
String getFunctionName();
931
}

src/main/java/com/microsoft/azure/functions/HttpRequestMessage.java

+53
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,65 @@
33
import java.net.URI;
44
import java.util.Map;
55

6+
/**
7+
* An HttpRequestMessage instance is provided to Azure functions that use
8+
* {@link com.microsoft.azure.functions.annotation.HttpTrigger HTTP Triggers}. For an example of how to use
9+
* the http functionality of Azure Functions, refer to the example in the
10+
* {@link com.microsoft.azure.functions.annotation.HttpTrigger}
11+
*
12+
* @see com.microsoft.azure.functions.annotation.HttpTrigger
13+
* @see HttpResponseMessage
14+
* @param <T> The type of the body content that is expected to be received as part of this HTTP request.
15+
* @since 1.0.0
16+
*/
617
public interface HttpRequestMessage<T> {
18+
/**
19+
* Returns the URI that was called that resulted in this HTTP request being submitted.
20+
* @return the URI that was called that resulted in this HTTP request being submitted.
21+
*/
722
URI getUri();
23+
24+
/**
25+
* Returns the HTTP method name, such as "GET" and "POST".
26+
* @return the HTTP method name, such as "GET" and "POST".
27+
*/
828
String getMethod();
29+
30+
/**
31+
* Returns a map of headers that were contained within this HTTP request.
32+
* @return a map of headers that were contained within this HTTP request.
33+
*/
934
Map<String, String> getHeaders();
35+
36+
/**
37+
* Returns a map of query parameters that were included with this HTTP request.
38+
* @return a map of query parameters that were included with this HTTP request.
39+
*/
1040
Map<String, String> getQueryParameters();
41+
42+
/**
43+
* Returns any body content that was included with this HTTP request.
44+
* @return any body content that was included with this HTTP request.
45+
*/
1146
T getBody();
1247

48+
/**
49+
* Generates a {@link HttpResponseMessage} instance containing the given HTTP status code and no response body.
50+
* Additional headers may be added by calling appropriate methods on {@link HttpResponseMessage}.
51+
*
52+
* @param status The HTTP status code to return to the caller of the function.
53+
* @return An {@link HttpResponseMessage} instance containing the provided status and empty body.
54+
*/
55+
HttpResponseMessage<Object> createResponse(int status);
56+
57+
/**
58+
* Generates a {@link HttpResponseMessage} instance containing the given HTTP status code and response body.
59+
* Additional headers may be added by calling appropriate methods on {@link HttpResponseMessage}.
60+
*
61+
* @param status The HTTP status code to return to the caller of the function.
62+
* @param body The body content to return to the caller of the function.
63+
* @param <R> The type of the body, as determined by the return type specified on the function itself.
64+
* @return An {@link HttpResponseMessage} instance containing the provided status and body content.
65+
*/
1366
<R> HttpResponseMessage<R> createResponse(int status, R body);
1467
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
package com.microsoft.azure.functions;
22

3+
/**
4+
* An HttpResponseMessage instance is returned by Azure Functions methods that are triggered by an
5+
* {@link com.microsoft.azure.functions.annotation.HttpTrigger}.
6+
*
7+
* @see com.microsoft.azure.functions.annotation.HttpTrigger
8+
* @see HttpRequestMessage
9+
* @param <T> The type of the body, as determined by the return type specified on the function itself.
10+
* @since 1.0.0
11+
*/
312
public interface HttpResponseMessage<T> {
13+
14+
/**
15+
* Returns the status code set on the HttpResponseMessage instance.
16+
* @return the status code set on the HttpResponseMessage instance.
17+
*/
418
int getStatus();
19+
20+
/**
21+
* Sets the status code on the HttpResponseMessage instance.
22+
* @param status An HTTP status code representing the outcome of the HTTP request.
23+
*/
524
void setStatus(int status);
25+
26+
/**
27+
* Adds a (key,value) header to the response.
28+
* @param key The key of the header value.
29+
* @param value The value of the header value.
30+
*/
631
void addHeader(String key, String value);
32+
33+
/**
34+
* Returns a header value for the given key.
35+
* @param key The key for which the header value is sought.
36+
* @return Returns the value if the key has previously been added, or null if it has not.
37+
*/
738
String getHeader(String key);
39+
40+
/**
41+
* Returns the body of the HTTP response.
42+
* @return the body of the HTTP response.
43+
*/
844
T getBody();
45+
46+
/**
47+
* Sets the body of the HTTP response.
48+
* @param body The body of the HTTP response
49+
*/
950
void setBody(T body);
1051
}

src/main/java/com/microsoft/azure/functions/OutputBinding.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.microsoft.azure.functions;
22

3+
/**
4+
*
5+
* @since 1.0.0
6+
*/
37
public interface OutputBinding<T> {
48
T getValue();
59
void setValue(T value);

src/main/java/com/microsoft/azure/functions/annotation/AccessRights.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
package com.microsoft.azure.functions.annotation;
88

9+
/**
10+
*
11+
* @since 1.0.0
12+
*/
913
public enum AccessRights {
1014
MANAGE,
1115
LISTEN

0 commit comments

Comments
 (0)