Reputation: 722
I'm working on a Java Function that has a timer trigger and try to insert a simple record in a DocumentDB collection. The Java code is the following
import java.util.Calendar;
import java.util.UUID;
import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.serverless.functions.ExecutionContext;
import com.microsoft.azure.serverless.functions.annotation.*;
public class DocumentDBFunction {
@FunctionName("documentDBFunction")
@DocumentDBOutput(name = "testdoc",
createIfNotExists = true,
databaseName = "functiondb",
collectionName="functioncoll",
connection = "CosmosDBConnectionString")
public Document functionHandler(
@TimerTrigger(name = "timerInfo", schedule = "*/30 * * * * *")
String timerInfo,
final ExecutionContext executionContext) {
String randomString=UUID.randomUUID().toString();
executionContext.getLogger().info("Insert obj documentDB: " + randomString);
Document document=new Document();
document.set("id",randomString);
document.set("_id",randomString);
document.set("uuid", randomString);
return document;
}
}
CosmosDBConnectionString is available in AppSetting and I also created the collection. Function.json is the following
{
"scriptFile": "../azurefunctions-1.0-SNAPSHOT.jar",
"entryPoint": "test.azure.functions.DocumentDBFunction.functionHandler",
"bindings": [
{
"type": "timerTrigger",
"name": "timerInfo",
"direction": "in",
"schedule": "*/30 * * * * *"
},
{
"type": "documentdb",
"name": "$return",
"direction": "out",
"databaseName": "functiondb",
"collectionName": "functioncoll",
"connection": "CosmosDBConnectionString",
"createIfNotExists": true
}
],
"disabled": false
}
If I run this function I haven'n error on the log files, but there isn't a new object in the DocumentDB collection. Any ideas? Comments and suggestions are welcome
Upvotes: 0
Views: 512
Reputation: 23782
I tried to test your java azure function code on my side.
Java code:
@FunctionName("doc")
@DocumentDBOutput(name = "testdoc",
createIfNotExists = true,
databaseName = "db",
collectionName="coll",
connection = "CosmosDBConnectionString")
public String functionHandler(
@TimerTrigger(name = "timerInfo", schedule = "*/30 * * * * *")
String timerInfo,
final ExecutionContext executionContext) {
String randomString=UUID.randomUUID().toString();
executionContext.getLogger().info("Insert obj documentDB: " + randomString);
Document document=new Document();
document.set("id",randomString);
document.set("name","Jay!!!");
return document.toString();
}
function.json
{
"scriptFile" : "..\\fabrikam-functions-1.0-SNAPSHOT.jar",
"entryPoint" : "com.fabrikam.functions.Function.functionHandler",
"bindings" : [ {
"type" : "timerTrigger",
"name" : "timerInfo",
"direction" : "in",
"schedule" : "*/30 * * * * *"
}, {
"type" : "documentdb",
"name" : "$return",
"direction" : "out",
"databaseName" : "db",
"collectionName" : "coll",
"connection" : "CosmosDBConnectionString",
"createIfNotExists" : true
} ],
"disabled" : false
}
The Azure Function runs successfully locally ,but the documents are not be passed into Document DB, as same as you.
I tried to run the same code in Azure and it doesn't show have any difference.
As I know , Java Azure Function is still a preview
version and I find the Annotation
against Cosmos db
for java is N/A
.
You could check the webjob dashboard to verify if any error log exist in the table storage which is configured in the AzureWebJobsStorage
.
In addition , I suggest you a workaround that you could call the Document DB Java SDK in your TimerTrigger. Please refer to the snippet of code as below:
private static final String account = "***";
private static final String key = "***";
private static DocumentClient client = new DocumentClient("***",
key, ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
@FunctionName("doc")
public String functionHandler(
@TimerTrigger(name = "timerInfo", schedule = "*/5 * * * * *")
String timerInfo,
final ExecutionContext executionContext) {
try {
String randomString = UUID.randomUUID().toString();
executionContext.getLogger().info("Insert obj documentDB: " + randomString);
Document document = new Document();
document.set("id", randomString);
document.set("name", "Jay!!!");
client.createDocument("dbs/db/colls/coll",document,null,false);
return "Insert Success id: "+ randomString;
} catch (Exception e) {
executionContext.getLogger().info(e.toString());
return e.toString();
}
}
Hope it helps you.
Upvotes: 2