diff --git a/docker/java/build.gradle b/docker/java/build.gradle index 692206ad..67dbb765 100644 --- a/docker/java/build.gradle +++ b/docker/java/build.gradle @@ -5,7 +5,10 @@ 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/" } } @@ -13,7 +16,7 @@ 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' diff --git a/examples/jetstream/list-subjects/java/Main.java b/examples/jetstream/list-subjects/java/Main.java index 21dd5431..b92cae95 100644 --- a/examples/jetstream/list-subjects/java/Main.java +++ b/examples/jetstream/list-subjects/java/Main.java @@ -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) { @@ -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") @@ -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(); @@ -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 entry : state.getSubjectMap().entrySet()) { + System.out.println(" subject '" + entry.getKey() + "' has " + entry.getValue() + " message(s)"); } // ### Subject Filtering @@ -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(); } } } diff --git a/examples/jetstream/list-subjects/java/output.cast b/examples/jetstream/list-subjects/java/output.cast deleted file mode 100644 index 2dd5dba6..00000000 --- a/examples/jetstream/list-subjects/java/output.cast +++ /dev/null @@ -1,9 +0,0 @@ -{"version": 2, "width": 0, "height": 0, "timestamp": 1718133451, "env": {"SHELL": null, "TERM": null}, "title": "NATS by Example: jetstream/list-subjects/java"} -[2.006398, "o", "Before publishing any messages, there are 0 subjects: 0\r\n"] -[2.009113, "o", "After publishing a message to a subject, it appears in state:\r\n Subject{name='plain', count=1}"] -[2.010047, "o", "\r\n"] -[2.01343, "o", "Wildcard subjects show the actual subject, not the template.\r\n"] -[2.013706, "o", " Subject{name='greater.A', count=1}\r\n Subject{name='greater.B.B.B', count=1}\r\n Subject{name='greater.A.B', count=1}\r\n Subject{name='plain', count=1}\r\n Subject{name='star.1', count=1}\r\n Subject{name='star.2', count=1}\r\n Subject{name='greater.A.B.C', count=1}\r\n"] -[2.0153, "o", "Filtering the subject returns only matching entries ['greater.>']\r\n Subject{name='greater.A', count=1}\r\n Subject{name='greater.B.B.B', count=1}\r\n Subject{name='greater.A.B', count=1}\r\n Subject{name='greater.A.B.C', count=1}\r\n"] -[2.016868, "o", "Filtering the subject returns only matching entries ['greater.A.>']"] -[2.016994, "o", "\r\n Subject{name='greater.A.B', count=1}\r\n Subject{name='greater.A.B.C', count=1}\r\n"] diff --git a/examples/jetstream/list-subjects/java/output.txt b/examples/jetstream/list-subjects/java/output.txt deleted file mode 100644 index 324d7780..00000000 --- a/examples/jetstream/list-subjects/java/output.txt +++ /dev/null @@ -1,19 +0,0 @@ -Before publishing any messages, there are 0 subjects: 0 -After publishing a message to a subject, it appears in state: - Subject{name='plain', count=1} -Wildcard subjects show the actual subject, not the template. - Subject{name='greater.A', count=1} - Subject{name='greater.B.B.B', count=1} - Subject{name='greater.A.B', count=1} - Subject{name='plain', count=1} - Subject{name='star.1', count=1} - Subject{name='star.2', count=1} - Subject{name='greater.A.B.C', count=1} -Filtering the subject returns only matching entries ['greater.>'] - Subject{name='greater.A', count=1} - Subject{name='greater.B.B.B', count=1} - Subject{name='greater.A.B', count=1} - Subject{name='greater.A.B.C', count=1} -Filtering the subject returns only matching entries ['greater.A.>'] - Subject{name='greater.A.B', count=1} - Subject{name='greater.A.B.C', count=1}