Skip to content

Java SDK v4 Sample Code #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: stagingBranch
Choose a base branch
from
Open

Java SDK v4 Sample Code #2

wants to merge 10 commits into from

Conversation

anfeldma-ms
Copy link
Contributor

No description provided.

README.md Outdated
To choose which sample will run, populate the **Main class** field of the Configuration with

```
com.azure.cosmos.examples.changefeed.sample
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changefeed package. If we are including changefeed samples here as well, please move them bellow other crud samples, as the most basic samples are crud.

*Build and execute from command line without an IDE:* From top-level directory of repo:
```
mvn clean package
mvn exec:java -Dexec.mainClass="com.azure.cosmos.examples.changefeed.sample" -DACCOUNT_HOST=your account hostname -DACCOUNT_KEY=your account master key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, the sample has changefeed in the package name
com.azure.cosmos.examples.changefeed.sample

err.printStackTrace();
}

completionLatch.countDown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove countdown latch for sample if possible, they are hard to understand.

err.printStackTrace();
}

completionLatch.countDown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should try to remove countdown latch for samples, they are hard to understand.


completionLatch.countDown();
},
() -> {completionLatch.countDown();}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@moderakh moderakh self-requested a review March 10, 2020 15:17
Copy link
Contributor

@moderakh moderakh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen customers just copy paste samples to their own prod code

  1. we should consider moving away from countdown latch, they are hard to understand in basic entry samples.
  2. for async operation we should try to do more chaining when possible
  3. we should consider using slf4j/log4j2 for logging (instead of system.out) and point the logger to std out, also we should make a comment in the log4j2 config that for real use async log4j2 config should be used.
  4. the samples should reference CRUD before referencing changefeed.

//This sample models an application where documents are being inserted into one container (the "feed container"),
//and meanwhile another worker thread or worker application is pulling inserted documents from the feed container's Change Feed
//and operating on them in some way. For one or more workers to process the Change Feed of a container, the workers must first contact the server
//and "lease" access to monitor one or more partitions of the feed container. The Change Feed Processor Library

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try to simplify this comment and add the following:

"A secondary Cosmos container ("lease container") is required in order to keep track of the documents being processed and resume the work from where it was left in case the processing is stopped. When the "feed container" has multiple partitions, corresponding number of "lease documents" are created and the Change Feed Processor instance will create equal amount of individual workers that will automatically process each partition in a multithread manner. In such cases the user's provided handler implementation can be invoked from different threads and execute in parallel. Multiple instances of the Change Feed Processor (i.e. running on different virtual machines) can operate on the same "feed container" at the same time to further parallelize the workload. Change Feed Processor library implement a load balancing component that will ensure more or less equal distribution of the workload among different instances."

//Processor Library can store and track leases container partitions.

//Summary of the next four commands:
//-Create an asynchronous Azure Cosmos DB client and database so that we can issue async requests to the DB

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add a disclaimer here that by creating the Cosmos containers and executing work on them, there will be charges associated with. We should also capture the default RU we set and link to the charges doc/web page.

logger.info("END Sample");
}

public static ChangeFeedProcessor getChangeFeedProcessor(String hostName, CosmosAsyncContainer feedContainer, CosmosAsyncContainer leaseContainer) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For each individual method it will be great if we can capture a short summary of what it is doing.

*/
public final class SampleConfigurations {
// REPLACE MASTER_KEY and HOST with values from your Azure Cosmos DB account.
// The default values are credentials of the local emulator, which are not used in any production environment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local emulator -> Cosmos Public Emulator which can be downloaded from https://aka.ms/cosmosdb-emulator

// Licensed under the MIT License.
package com.azure.cosmos.examples.changefeed;

import com.google.common.base.Strings;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid taking any other dependencies and just the ones in Java (or Cosmos SDK)? As Mo commented, people tend to copy/paste the sample code in their apps and we should try to avoid bringing new dependencies that are not required for their app to execute.


//These two lines model an application which is inserting ten documents into the feed container
logger.info("-->START application that inserts documents into feed container");
createNewDocumentsCustomPOJO(feedContainer, 10, Duration.ofSeconds(3));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a bare bone CustomPOJO, it might be worth using one of the other POJO classes such a Family for instance. It will help make it more clear how properties will map and be accessed.

executeStoredProcedure();

//Perform a point-read to confirm that the item with id test_doc exists
logger.info("Checking that a document was created by the stored procedure...");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document check should be done inside executeStoredProcedure().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants