Skip to content

Java Java

Gate provides a Java API for integrating with your Java applications using gRPC. You can use the API to interact with Gate programmatically.

Installation

First, configure your package manager to add the Buf registry. You only need to do this once:

xml
<!-- pom.xml -->
<repositories>
  <repository>
    <name>Buf Maven Repository</name>
    <id>buf</id>
    <url>https://buf.build/gen/maven</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>
kotlin
// build.gradle.kts
repositories {
  mavenCentral()
  maven {
    name = "buf"
    url = uri("https://buf.build/gen/maven")
  }
}
groovy
// build.gradle
repositories {
  mavenCentral()
  maven {
    name = 'buf'
    url 'https://buf.build/gen/maven'
  }
}

Then add the dependencies:

Latest Version Check

Make sure to check buf.build/minekube/gate/sdks for the latest versions of the dependencies.

xml
<dependencies>
  <!-- Check latest version at https://buf.build/minekube/gate/sdks -->
  <dependency>
    <groupId>build.buf.gen</groupId>
    <artifactId>minekube_gate_protocolbuffers_java</artifactId>
    <version>28.3.0.2.20241118150055.50fffb007499</version>
  </dependency>
  <dependency>
    <groupId>build.buf.gen</groupId>
    <artifactId>minekube_gate_grpc_java</artifactId>
    <version>1.68.1.1.20241118150055.50fffb007499</version>
  </dependency>
</dependencies>
kotlin
dependencies {
  // Check latest version at https://buf.build/minekube/gate/sdks
  implementation("build.buf.gen:minekube_gate_protocolbuffers_java:28.3.0.2.20241118150055.50fffb007499")
  implementation("build.buf.gen:minekube_gate_grpc_java:1.68.1.1.20241118150055.50fffb007499")
}
groovy
dependencies {
  // Check latest version at https://buf.build/minekube/gate/sdks
  implementation 'build.buf.gen:minekube_gate_protocolbuffers_java:28.3.0.2.20241118150055.50fffb007499'
  implementation 'build.buf.gen:minekube_gate_grpc_java:1.68.1.1.20241118150055.50fffb007499'
}

Usage Example

Here's a basic example of using the Gate Java API to connect to Gate and list servers:

java
package com.example;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import build.buf.gen.minekube.gate.v1.*;

public class Main {
    public static void main(String[] args) {
        try {
            // Create a gRPC channel
            ManagedChannel channel = ManagedChannelBuilder
                .forAddress("localhost", 8080)
                .usePlaintext()
                .build();

            // Create a blocking stub
            GateServiceGrpc.GateServiceBlockingStub stub = GateServiceGrpc.newBlockingStub(channel);

            // List all servers
            ListServersResponse response = stub.listServers(ListServersRequest.getDefaultInstance());

            // Print protobuf response
            System.out.println(response);

            // Shutdown the channel
            channel.shutdown();
        } catch (Exception e) {
            System.err.println("Make sure Gate is running with the API enabled");
            e.printStackTrace();
        }
    }
}

Running the Example

  1. Run Gate with the API enabled
  2. Navigate to the docs/developers/api/java directory
  3. Run the following commands:
bash
mvn compile
mvn exec:java -Dexec.mainClass="com.example.Main"

servers {
  name: "server3"
  address: "localhost:25568"
}
servers {
  name: "server4"
  address: "localhost:25569"
}
servers {
  name: "server1"
  address: "localhost:25566"
}
servers {
  name: "server2"
  address: "localhost:25567"
}

Learn More

For more details on using gRPC with Java, check out the gRPC Java Documentation.

Released under the Apache 2.0 License. (web version: 037ca6cf)