Skip to content
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

[Java] Improve List Subjects #228

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docker/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ plugins {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
url "https://oss.sonatype.org/content/repositories/releases/"
}
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}

dependencies {
implementation 'io.nats:nkeys-java:2.0.1'
implementation 'io.nats:jnats-json:2.0.0'
implementation 'io.nats:jwt-java:2.0.0'
implementation 'io.nats:jnats:2.19.0'
implementation 'io.nats:jnats:2.19.2-SNAPSHOT'
}

apply plugin: 'java'
Expand Down
52 changes: 28 additions & 24 deletions examples/jetstream/list-subjects/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import io.nats.client.api.*;

import java.io.IOException;
import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.Map;

public class Main {
public static void main(String[] args) {
Expand All @@ -16,10 +13,17 @@ public static void main(String[] args) {
natsURL = "nats://127.0.0.1:4222";
}

try (Connection conn = Nats.connect(natsURL)) {
JetStreamManagement jsm = conn.jetStreamManagement();
try (Connection nc = Nats.connect(natsURL)) {
JetStreamManagement jsm = nc.jetStreamManagement();
JetStream js = jsm.jetStream();

// Delete the stream, so we always have a fresh start for the example
// don't care if this, errors in this example, it will if the stream exists.
try {
jsm.deleteStream("subjects");
}
catch (Exception ignore) {}

// Create a stream with a few subjects
jsm.addStream(StreamConfiguration.builder()
.name("subjects")
Expand All @@ -28,7 +32,7 @@ public static void main(String[] args) {

// ### GetStreamInfo with StreamInfoOptions
// Get the subjects via the getStreamInfo call.
// Since this is "state" a subject is not in the state unless
// Since this is "state" there are no subjects in the state unless
// there are messages in the subject.
StreamInfo si = jsm.getStreamInfo("subjects", StreamInfoOptions.allSubjects());
StreamState state = si.getStreamState();
Expand All @@ -41,22 +45,26 @@ public static void main(String[] args) {
state = si.getStreamState();
System.out.println("After publishing a message to a subject, it appears in state:");
for (Subject s : state.getSubjects()) {
System.out.println(" " + s);
System.out.println(" subject '" + s.getName() + "' has " + s.getCount() + " message(s)");
}

// Publish some more messages, this time against wildcard subjects
js.publish("greater.A", null);
js.publish("greater.A.B", null);
js.publish("greater.A.B.C", null);
js.publish("greater.B.B.B", null);
js.publish("star.1", null);
js.publish("star.2", null);
js.publish("greater.A", "gtA-1".getBytes());
js.publish("greater.A", "gtA-2".getBytes());
js.publish("greater.A.B", "gtAB-1".getBytes());
js.publish("greater.A.B", "gtAB-2".getBytes());
js.publish("greater.A.B.C", "gtABC".getBytes());
js.publish("greater.B.B.B", "gtBBB".getBytes());
js.publish("star.1", "star1-1".getBytes());
js.publish("star.1", "star1-2".getBytes());
js.publish("star.2", "star2".getBytes());

// Get all subjects, but get the subjects as a map, via getSubjectMap
si = jsm.getStreamInfo("subjects", StreamInfoOptions.allSubjects());
state = si.getStreamState();
System.out.println("Wildcard subjects show the actual subject, not the template.");
for (Subject s : state.getSubjects()) {
System.out.println(" " + s);
for (Map.Entry<String, Long> entry : state.getSubjectMap().entrySet()) {
System.out.println(" subject '" + entry.getKey() + "' has " + entry.getValue() + " message(s)");
}

// ### Subject Filtering
Expand All @@ -65,21 +73,17 @@ public static void main(String[] args) {
state = si.getStreamState();
System.out.println("Filtering the subject returns only matching entries ['greater.>']");
for (Subject s : state.getSubjects()) {
System.out.println(" " + s);
System.out.println(" subject '" + s.getName() + "' has " + s.getCount() + " message(s)");
}

si = jsm.getStreamInfo("subjects", StreamInfoOptions.filterSubjects("greater.A.>"));
state = si.getStreamState();
System.out.println("Filtering the subject returns only matching entries ['greater.A.>']");
for (Subject s : state.getSubjects()) {
System.out.println(" " + s);
System.out.println(" subject '" + s.getName() + "' has " + s.getCount() + " message(s)");
}
}
catch (JetStreamApiException | IOException | InterruptedException e) {
// * JetStreamApiException: the stream or consumer did not exist
// * IOException: problem making the connection
// * InterruptedException: thread interruption in the body of the example
System.out.println(e);
} catch (InterruptedException | IOException | JetStreamApiException e) {
e.printStackTrace();
}
}
}
9 changes: 0 additions & 9 deletions examples/jetstream/list-subjects/java/output.cast

This file was deleted.

19 changes: 0 additions & 19 deletions examples/jetstream/list-subjects/java/output.txt

This file was deleted.

Loading