-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package example; | ||
|
||
import io.nats.client.*; | ||
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; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
String natsURL = System.getenv("NATS_URL"); | ||
if (natsURL == null) { | ||
natsURL = "nats://127.0.0.1:4222"; | ||
} | ||
|
||
try (Connection conn = Nats.connect(natsURL)) { | ||
JetStreamManagement jsm = conn.jetStreamManagement(); | ||
JetStream js = jsm.jetStream(); | ||
|
||
// Create a stream with a few subjects | ||
jsm.addStream(StreamConfiguration.builder() | ||
.name("subjects") | ||
.subjects("plain", "greater.>", "star.*") | ||
.build()); | ||
|
||
// ### GetStreamInfo with StreamInfoOptions | ||
// Get the subjects via the getStreamInfo call. | ||
// 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(); | ||
System.out.println("Before publishing any messages, there are 0 subjects: " + state.getSubjectCount()); | ||
|
||
// Publish a message | ||
js.publish("plain", null); | ||
|
||
si = jsm.getStreamInfo("subjects", StreamInfoOptions.allSubjects()); | ||
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); | ||
} | ||
|
||
// 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); | ||
|
||
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); | ||
} | ||
|
||
// ### Subject Filtering | ||
// Instead of allSubjects, you can filter for a specific subject | ||
si = jsm.getStreamInfo("subjects", StreamInfoOptions.filterSubjects("greater.>")); | ||
state = si.getStreamState(); | ||
System.out.println("Filtering the subject returns only matching entries ['greater.>']"); | ||
for (Subject s : state.getSubjects()) { | ||
System.out.println(" " + 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); | ||
} | ||
} | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
title: List subjects for a specific stream | ||
description: |- | ||
All clients have a way to get the list of subjects for any given stream, except it's not completely obvious how to do this. | ||
These examples will show you how to get the list of subjects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e46c6de
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for nats-by-example ready!
✅ Preview
https://nats-by-example-ka65n71nn-connecteverything.vercel.app
Built with commit e46c6de.
This pull request is being automatically deployed with vercel-action