DataStax-Examples / concurrent-requests-java

Execute multiple concurrent requests asynchronously with Cassandra using the Java DataStax Driver

Home Page:https://www.datastax.com/examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Concurrent Requests in Java

This example shows three different ways of handling concurrent request execution with Apache Cassandra™ using the Java DataStax Driver.

Contributors: Olivier Michallat, Hsu-Kwang Hwang, Tomasz Lelek - derived from here

Objectives

  • How to limit concurrent query executions with Cassandra in blocking and non-blocking ways.
  • How to throttle Concurrent Requests.

Project Layout

The following are the main Java files used by the application.

  • LimitConcurrencyCustom - This example shows executing concurrent queries in blocking ways. The basic java concurrent mechanism such as SEMAPHORE, and Latch are used to control the flow.
  • LimitConcurrencyCustomAsync - This example shows executing concurrent queries in non-blocking ways. Java CompletableFuture is used to achieve non-blocking.
  • LimitConcurrencyRequestThrottler - Throttling allows you to limit how many requests a session can execute concurrently. This is useful if you have multiple applications connecting to the same Cassandra cluster, and want to enforce some kind of SLA to ensure fair resource allocation.

Setup and Running

Prerequisites

  • Maven 3 installed on the machine running the examples
  • JDK 14
  • An Apache Cassandra(R) cluster is running and accessible through the contacts points and data center identified in application.conf

Running

Building

At the project root level

mvn clean package

This builds the JAR file located at target/java-concurrent-execution-1.0.jar

Configuration changes

The main configuration file is src/main/resources/application.conf Change basic.contact-points = ["127.0.0.1:9042"] to the IP and cql port of your Cassandra cluster. Change basic.local-datacenter = dc1 to the datacenter of the Cassandra cluster you want to connect to.

Throttling

For throttling the configurations are in the following block

advanced.throttler {
    class = ConcurrencyLimitingRequestThrottler
    max-concurrent-requests = 32
    max-queue-size = 10000
}

You are encouraged to experiment the configuration. For more details about throttling please check the following link

Run the program

  • java -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyCustom
  • java -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyCustomAsync
  • java -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyRequestThrottler

About

Execute multiple concurrent requests asynchronously with Cassandra using the Java DataStax Driver

https://www.datastax.com/examples

License:Apache License 2.0


Languages

Language:Java 100.0%