Skip to content

Commit 586ccb9

Browse files
Merge pull request #263660 from jcocchi/update-java-functions
Azure Functions: update Cosmos DB java trigger example
2 parents 65c99ad + 8723c77 commit 586ccb9

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

articles/azure-functions/functions-bindings-cosmosdb-v2-trigger.md

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Azure Cosmos DB trigger for Functions 2.x and higher
33
description: Learn to use the Azure Cosmos DB trigger in Azure Functions.
44
ms.topic: reference
5-
ms.date: 04/04/2023
5+
ms.date: 01/19/2024
66
ms.devlang: csharp
77
# ms.devlang: csharp, java, javascript, powershell, python
88
ms.custom: devx-track-csharp, devx-track-python, ignite-2022, devx-track-extended-java, devx-track-js
@@ -55,7 +55,7 @@ The following examples depend on the extension version for the given C# mode.
5555

5656
# [Extension 4.x+](#tab/extensionv4/in-process)
5757

58-
Apps using [Azure Cosmos DB extension version 4.x](./functions-bindings-cosmosdb-v2.md?tabs=extensionv4) or higher will have different attribute properties, which are shown below. This example refers to a simple `ToDoItem` type.
58+
Apps using [Azure Cosmos DB extension version 4.x](./functions-bindings-cosmosdb-v2.md?tabs=extensionv4) or higher have different attribute properties, which are shown here. This example refers to a simple `ToDoItem` type.
5959

6060
```cs
6161
namespace CosmosDBSamplesV2
@@ -169,7 +169,7 @@ public void Run([CosmosDBTrigger(
169169

170170
The following code defines a `MyDocument` type:
171171

172-
:::code language="csharp" source="~/azure-functions-dotnet-worker/samples/Extensions/CosmosDB/CosmosDBFunction.cs" range="40-49":::
172+
:::code language="csharp" source="~/azure-functions-dotnet-worker/samples/Extensions/CosmosDB/CosmosDBFunction.cs" range="49-58":::
173173

174174
An [`IReadOnlyList<T>`](/dotnet/api/system.collections.generic.ireadonlylist-1) is used as the Azure Cosmos DB trigger binding parameter in the following example:
175175

@@ -184,7 +184,29 @@ This example requires the following `using` statements:
184184
::: zone-end
185185
::: zone pivot="programming-language-java"
186186

187-
This function is invoked when there are inserts or updates in the specified database and collection.
187+
This function is invoked when there are inserts or updates in the specified database and container.
188+
189+
# [Extension 4.x+](#tab/extensionv4)
190+
191+
[!INCLUDE [functions-cosmosdb-extension-java-note](../../includes/functions-cosmosdb-extension-java-note.md)]
192+
193+
```java
194+
@FunctionName("CosmosDBTriggerFunction")
195+
public void run(
196+
@CosmosDBTrigger(
197+
name = "items",
198+
databaseName = "ToDoList",
199+
containerName = "Items",
200+
leaseContainerName="leases",
201+
connection = "AzureCosmosDBConnection",
202+
createLeaseContainerIfNotExists = true
203+
)
204+
Object inputItem,
205+
final ExecutionContext context
206+
) {
207+
context.getLogger().info("Items modified: " + inputItems.size());
208+
}
209+
```
188210

189211
# [Functions 2.x+](#tab/functionsv2)
190212

@@ -201,9 +223,6 @@ This function is invoked when there are inserts or updates in the specified data
201223
context.getLogger().info(items.length + "item(s) is/are changed.");
202224
}
203225
```
204-
# [Extension 4.x+](#tab/extensionv4)
205-
206-
[!INCLUDE [functions-cosmosdb-extension-java-note](../../includes/functions-cosmosdb-extension-java-note.md)]
207226

208227
---
209228

@@ -220,7 +239,7 @@ The following example shows an Azure Cosmos DB trigger [TypeScript function](fun
220239

221240
# [Model v3](#tab/nodejs-v3)
222241

223-
TypeScript samples are not documented for model v3.
242+
TypeScript samples aren't documented for model v3.
224243

225244
---
226245

@@ -356,9 +375,35 @@ For Python functions defined by using *function.json*, see the [Configuration](#
356375
::: zone pivot="programming-language-java"
357376
## Annotations
358377

378+
# [Extension 4.x+](#tab/extensionv4)
379+
380+
[!INCLUDE [functions-cosmosdb-extension-java-note](../../includes/functions-cosmosdb-extension-java-note.md)]
381+
382+
Use the `@CosmosDBTrigger` annotation on parameters that read data from Azure Cosmos DB. The annotation supports the following properties:
383+
384+
|Attribute property | Description|
385+
|---------|----------------------|
386+
|**connection** | The name of an app setting or setting collection that specifies how to connect to the Azure Cosmos DB account being monitored. For more information, see [Connections](#connections).|
387+
|**name** | The name of the function. |
388+
|**databaseName** | The name of the Azure Cosmos DB database with the container being monitored. |
389+
|**containerName** | The name of the container being monitored. |
390+
|**leaseConnectionStringSetting** | (Optional) The name of an app setting or setting collection that specifies how to connect to the Azure Cosmos DB account that holds the lease container. <br><br> When not set, the `Connection` value is used. This parameter is automatically set when the binding is created in the portal. The connection string for the leases container must have write permissions.|
391+
|**leaseDatabaseName** | (Optional) The name of the database that holds the container used to store leases. When not set, the value of the `databaseName` setting is used. |
392+
|**leaseContainerName** | (Optional) The name of the container used to store leases. When not set, the value `leases` is used. |
393+
|**createLeaseContainerIfNotExists** | (Optional) When set to `true`, the leases container is automatically created when it doesn't already exist. The default value is `false`. When using Microsoft Entra identities if you set the value to `true`, creating containers isn't [an allowed operation](../cosmos-db/nosql/troubleshoot-forbidden.md#non-data-operations-are-not-allowed) and your Function won't start.|
394+
|**leasesContainerThroughput** | (Optional) Defines the number of Request Units to assign when the leases container is created. This setting is only used when `CreateLeaseContainerIfNotExists` is set to `true`. This parameter is automatically set when the binding is created using the portal. |
395+
|**leaseContainerPrefix** | (Optional) When set, the value is added as a prefix to the leases created in the Lease container for this function. Using a prefix allows two separate Azure Functions to share the same Lease container by using different prefixes. |
396+
|**feedPollDelay**| (Optional) The time (in milliseconds) for the delay between polling a partition for new changes on the feed, after all current changes are drained. Default is 5,000 milliseconds, or 5 seconds.|
397+
|**leaseAcquireInterval**| (Optional) When set, it defines, in milliseconds, the interval to kick off a task to compute if partitions are distributed evenly among known host instances. Default is 13000 (13 seconds). |
398+
|**leaseExpirationInterval**| (Optional) When set, it defines, in milliseconds, the interval for which the lease is taken on a lease representing a partition. If the lease isn't renewed within this interval, it will expire and ownership of the partition moves to another instance. Default is 60000 (60 seconds).|
399+
|**leaseRenewInterval**| (Optional) When set, it defines, in milliseconds, the renew interval for all leases for partitions currently held by an instance. Default is 17000 (17 seconds). |
400+
|**maxItemsPerInvocation**| (Optional) When set, this property sets the maximum number of items received per Function call. If operations in the monitored container are performed through stored procedures, [transaction scope](../cosmos-db/nosql/stored-procedures-triggers-udfs.md#transactions) is preserved when reading items from the change feed. As a result, the number of items received could be higher than the specified value so that the items changed by the same transaction are returned as part of one atomic batch. |
401+
|**startFromBeginning**| (Optional) This option tells the Trigger to read changes from the beginning of the container's change history instead of starting at the current time. Reading from the beginning only works the first time the trigger starts, as in subsequent runs, the checkpoints are already stored. Setting this option to `true` when there are leases already created has no effect. |
402+
|**preferredLocations**| (Optional) Defines preferred locations (regions) for geo-replicated database accounts in the Azure Cosmos DB service. Values should be comma-separated. For example, "East US,South Central US,North Europe". |
403+
359404
# [Functions 2.x+](#tab/functionsv2)
360405

361-
From the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `@CosmosDBInput` annotation on parameters that read data from Azure Cosmos DB. The annotation supports the following properties:
406+
From the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `@CosmosDBTrigger` annotation on parameters that read data from Azure Cosmos DB. The annotation supports the following properties:
362407

363408
+ [name](/java/api/com.microsoft.azure.functions.annotation.cosmosdbtrigger.name)
364409
+ [connectionStringSetting](/java/api/com.microsoft.azure.functions.annotation.cosmosdbtrigger.connectionstringsetting)
@@ -380,10 +425,6 @@ From the [Java functions runtime library](/java/api/overview/azure/functions/run
380425
+ [startFromBeginning](/java/api/com.microsoft.azure.functions.annotation.cosmosdbtrigger.startfrombeginning)
381426
+ [preferredLocations](/java/api/com.microsoft.azure.functions.annotation.cosmosdbtrigger.preferredlocations)
382427

383-
# [Extension 4.x+](#tab/extensionv4)
384-
385-
[!INCLUDE [functions-cosmosdb-extension-java-note](../../includes/functions-cosmosdb-extension-java-note.md)]
386-
387428
---
388429

389430
::: zone-end
@@ -415,14 +456,14 @@ The following table explains the binding configuration properties that you set i
415456
::: zone-end
416457
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"
417458

418-
# [Functions 2.x+](#tab/functionsv2)
419-
420-
[!INCLUDE [functions-cosmosdb-settings-v3](../../includes/functions-cosmosdb-settings-v3.md)]
421-
422459
# [Extension 4.x+](#tab/extensionv4)
423460

424461
[!INCLUDE [functions-cosmosdb-settings-v4](../../includes/functions-cosmosdb-settings-v4.md)]
425462

463+
# [Functions 2.x+](#tab/functionsv2)
464+
465+
[!INCLUDE [functions-cosmosdb-settings-v3](../../includes/functions-cosmosdb-settings-v3.md)]
466+
426467
---
427468

428469
::: zone-end

articles/azure-functions/functions-bindings-error-pages.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ The following table indicates which triggers support retries and where the retry
6565

6666
### Retry policies
6767

68-
Starting with version 3.x of the Azure Functions runtime, you can define retry policies for Timer, Kafka, and Event Hubs triggers that are enforced by the Functions runtime.
68+
Starting with version 3.x of the Azure Functions runtime, you can define retry policies for Timer, Kafka, Event Hubs, and Azure Cosmos DB triggers that are enforced by the Functions runtime.
6969

7070
The retry policy tells the runtime to rerun a failed execution until either successful completion occurs or the maximum number of retries is reached.
7171

72-
A retry policy is evaluated when a Timer, Kafka, or Event Hubs-triggered function raises an uncaught exception. As a best practice, you should catch all exceptions in your code and rethrow any errors that you want to result in a retry.
72+
A retry policy is evaluated when a Timer, Kafka, Event Hubs, or Azure Cosmos DB-triggered function raises an uncaught exception. As a best practice, you should catch all exceptions in your code and rethrow any errors that you want to result in a retry.
7373

7474
> [!IMPORTANT]
7575
> Event Hubs checkpoints won't be written until the retry policy for the execution has finished. Because of this behavior, progress on the specific partition is paused until the current batch has finished.

0 commit comments

Comments
 (0)