Set up Java client

To set up Java client in Pulsar, complete the following steps.

Step 1: Install Java client library

The latest version of the Pulsar Java client library is available via Maven Central. To use the latest version, add the pulsar-client library to your build configuration.

Set up - 图1tip

Maven

If you use Maven, add the following information to the pom.xml file.

  1. <!-- in your <properties> block -->
  2. <pulsar.version>4.0.0</pulsar.version>
  3. <!-- in your <dependencies> block -->
  4. <dependency>
  5. <groupId>org.apache.pulsar</groupId>
  6. <artifactId>pulsar-client</artifactId>
  7. <version>${pulsar.version}</version>
  8. </dependency>

Gradle

If you use Gradle, add the following information to the build.gradle file.

  1. def pulsarVersion = '4.0.0'
  2. dependencies {
  3. implementation "org.apache.pulsar:pulsar-client:${pulsarVersion}"
  4. }

Pulsar BOM

While the above dependencies are sufficient to obtain the Pulsar Java client, it is recommended to also use the Pulsar BOM to ensure that all Pulsar dependencies (both direct and transitive) are at the same expected version. In order to use the BOM, the previous directions are modified slightly as follows:

Maven

Set up - 图2note

Please notice that when using Spring Boot and the default Maven build, it is necessary to use Spring Boot Maven plugin features to configure the Pulsar version. Please refer to the Spring Boot using Maven section for more details.

If you use Maven, add the following information to the pom.xml file.

  1. <!-- in your <properties> block -->
  2. <pulsar.version>4.0.0</pulsar.version>
  3. <!-- in your <dependencyManagement>/<dependencies> block -->
  4. <dependency>
  5. <groupId>org.apache.pulsar</groupId>
  6. <artifactId>pulsar-bom</artifactId>
  7. <version>${pulsar.version}</version>
  8. <type>pom</type>
  9. <scope>import</scope>
  10. </dependency>
  11. <!-- in your <dependencies> block -->
  12. <dependency>
  13. <groupId>org.apache.pulsar</groupId>
  14. <artifactId>pulsar-client</artifactId>
  15. </dependency>

Gradle

If you use Gradle, add the following information to the build.gradle file.

Set up - 图3note

Please notice that when using Spring Boot and the default Gradle build with the Spring Dependency Management plugin (io.spring.dependency-management), it is necessary to use Spring Dependency Management plugin features to configure the Pulsar version. Please refer to the Spring Boot using Gradle section for more details.

  1. def pulsarVersion = '4.0.0'
  2. dependencies {
  3. implementation enforcedPlatform("org.apache.pulsar:pulsar-bom:${pulsarVersion}")
  4. implementation 'org.apache.pulsar:pulsar-client'
  5. }

Note that the version is number for the pulsar-client dependency is now omitted as the Pulsar BOM dictates which version is used.

Spring Boot

You can find more information about using Pulsar with Spring Boot in the Spring Boot documentation.

Spring Boot using Maven

The Spring Boot Dependency Version properties define pulsar.version and pulsar-reactive.version for controlling the Pulsar Java client version and Pulsar Reactive client version.

  1. <!-- in your <properties> block -->
  2. <pulsar.version>4.0.0</pulsar.version>
  3. <!-- in your <dependencies> block -->
  4. <!-- The Pulsar Java client will be automatically added to dependencies as a transitive dependency of the spring-boot-starter-pulsar dependency -->
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-pulsar</artifactId>
  8. </dependency>

Spring Boot using Gradle

Please notice that when using the Spring Dependency Management plugin (io.spring.dependency-management) in Gradle, it is necessary to use Spring Dependency Management plugin features to configure the Pulsar version. The Spring Boot Dependency Version properties define pulsar.version and pulsar-reactive.version for controlling the Pulsar Java client version and Pulsar Reactive client version.

To use a specific Pulsar version for the Pulsar Java client in a Spring Boot application using Gradle, add the following to your build.gradle file in a Spring Boot project:

  1. // Alternatively, you can set the `pulsar.version` property in the `gradle.properties` file.
  2. ext['pulsar.version'] = '4.0.0'
  3. // The Pulsar Java client will be automatically added to dependencies as a transitive dependency of the spring-boot-starter-pulsar dependency
  4. dependencies {
  5. implementation 'org.springframework.boot:spring-boot-starter-pulsar'
  6. }

Step 2: Connect to Pulsar cluster

To connect to Pulsar using client libraries, you need to specify a Pulsar protocol URL.

You can assign Pulsar protocol URLs to specific clusters and use the pulsar scheme. The following is an example of localhost with the default port 6650:

  1. pulsar://localhost:6650

If you have multiple brokers, separate IP:port by commas:

  1. pulsar://localhost:6550,localhost:6651,localhost:6652

If you use mTLS authentication, add +ssl in the scheme:

  1. pulsar+ssl://pulsar.us-west.example.com:6651

Java client Performance considerations

Increasing the memory limit

For high-throughput applications, you can increase the amount of memory with the Java client builder’s memoryLimit configuration option). The default limit is 64MB which is usually too low for high-throughput applications.

By default Java applications have a limit for direct memory allocations. The allocations are limited by the -XX:MaxDirectMemorySize JVM option. In many JVM implementations, this defaults to the maximum heap size unless explicitly set. Allocations happen outside of the Java heap.

Enabling optimized Netty direct memory buffer access

The Pulsar Java client uses Netty under the hood and uses Netty direct buffers for data transport.

Netty has a feature that allows optimized direct memory buffer access. This feature enables Netty to use low level APIs such as sun.misc.Unsafe for direct memory operations, which provides faster allocation and deallocation of direct buffers. The faster deallocation can help avoid direct memory exhaustion and java.lang.OutOfMemoryError: Direct buffer memory errors. These errors can occur when the Netty memory pool and memory allocator cannot release memory back to the operating system quickly enough.

To enable this feature in Java clients since Java 11, you need to add the following JVM options to the application that uses the Java client:

  • --add-opens java.base/java.nio=ALL-UNNAMED
  • --add-opens java.base/jdk.internal.misc=ALL-UNNAMED

In addition, you need to add one of the following JVM options:

  • -Dorg.apache.pulsar.shade.io.netty.tryReflectionSetAccessible=true for the default shaded Pulsar client
  • -Dio.netty.tryReflectionSetAccessible=true for the unshaded “original” Pulsar client