。セッション）;
試す {
//リースコンテナが存在しない場合は、リースコンテナを作成します
CreateLeaseContainerifnotexists（documentClient、database_id、lease_container_id）;
SCSS
コードをコピーします
// ChangeFeedoptions ChangeFeedoptions ChangeFeedoptions = new ChangeFeedoptions（）を定義します。changefeedoptions.setstartfrombegining（true）;// ChangeFeedProcessor Optionsを定義しますChangeFeedProcessorOptions ChangeFeedProcessorOptions = new ChangeFeedProcessorOptions（）;ChangeFeedProcessorOptions.setLeasePrefix（ "myChangeFeed"）;// ChangeFeedProcessor ChangeFeedProcessor ChangeFeedProcessorを定義します= new ChangeFeedProcessorBuilder（）.WithDocumentClient（documentClient）.WithFeedCollection（database_id、container_id）.withleasecollection（database_id、lease_id）。 （ChangeFeedProcessorexample :: processChanges）.build（）;// ChangeFeedProcessor ChangeFeedProcessor.start（）を開始します。} catch（documentClientException e）{system.err.println（ "変更フィードの処理中にエラーが発生しました：" + e）;}} private static void processChanges（list <document> docs、cancellationtoken cancellationtoken）{for（document doc：docs）{system.out.println（ "change：" + doc.tojson（））;}} private static void createleaseContainerifnotexists（documentclient documentclient、string databaseid、string containerid）throws documentclientexception {string collectionlink = string.format（ "/dbs/％s/％s"、databaseid、containerid）;try {documentclient.readContainer（collectionlink、new Feedoptions（））。getResource（）;} catch（documentClientException e）{if（e.getStatusCode（）== 404）{documentCollection collectionDefinition = new DocumentCollection（）;collectiondefinition.setId（containerid）;requestoptions requestoptions = new requestoptions（）;requestoptions.setofferthroughput（400）;documentClient.createContainer（databaseid、collectiondefinition、requestoptions）;} else {throw e;}}}
}
.Session);
try {
// Create the lease container if it does not exist
createLeaseContainerIfNotExists(documentClient, DATABASE_ID, LEASE_CONTAINER_ID);
scss
Copy code
// Define the ChangeFeedOptions ChangeFeedOptions changeFeedOptions = new ChangeFeedOptions(); changeFeedOptions.setStartFromBeginning(true); // Define the ChangeFeedProcessor options ChangeFeedProcessorOptions changeFeedProcessorOptions = new ChangeFeedProcessorOptions(); changeFeedProcessorOptions.setLeasePrefix("myChangeFeed"); // Define the ChangeFeedProcessor ChangeFeedProcessor changeFeedProcessor = new ChangeFeedProcessorBuilder() .withDocumentClient(documentClient) .withFeedCollection(DATABASE_ID, CONTAINER_ID) .withLeaseCollection(DATABASE_ID, LEASE_CONTAINER_ID) .withProcessorOptions(changeFeedProcessorOptions) .withHandleChanges(ChangeFeedProcessorExample::processChanges) .build(); // Start the ChangeFeedProcessor changeFeedProcessor.start(); } catch (DocumentClientException e) { System.err.println("An error occurred while processing the Change Feed: " + e); } } private static void processChanges(List<Document> docs, CancellationToken cancellationToken) { for (Document doc : docs) { System.out.println("Change: " + doc.toJson()); } } private static void createLeaseContainerIfNotExists(DocumentClient documentClient, String databaseId, String containerId) throws DocumentClientException { String collectionLink = String.format("/dbs/%s/colls/%s", databaseId, containerId); try { documentClient.readContainer(collectionLink, new FeedOptions()).getResource(); } catch (DocumentClientException e) { if (e.getStatusCode() == 404) { DocumentCollection collectionDefinition = new DocumentCollection(); collectionDefinition.setId(containerId); RequestOptions requestOptions = new RequestOptions(); requestOptions.setOfferThroughput(400); documentClient.createContainer(databaseId, collectionDefinition, requestOptions); } else { throw e; } } }
}