-
Notifications
You must be signed in to change notification settings - Fork 3
Cancel Schedule #46
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
Merged
+931
−60
Merged
Cancel Schedule #46
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
114 changes: 114 additions & 0 deletions
114
schedule-message/src/examples/java/io/synadia/examples/ScheduleBasicsAlternate.java
This file contains hidden or 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,114 @@ | ||
| // Copyright (c) 2025 Synadia Communications Inc. All Rights Reserved. | ||
| // See LICENSE and NOTICE file for details. | ||
|
|
||
| package io.synadia.examples; | ||
|
|
||
| import io.nats.client.*; | ||
| import io.nats.client.api.StorageType; | ||
| import io.nats.client.api.StreamInfo; | ||
| import io.nats.client.support.DateTimeUtils; | ||
| import io.synadia.sm.ScheduleManagement; | ||
| import io.synadia.sm.ScheduledMessageBuilder; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static io.synadia.examples.ScheduleUtils.report; | ||
|
|
||
| /** | ||
| * Example: same scenario as {@link ScheduleBasics}, but built using | ||
| * {@link io.synadia.sm.ScheduledMessageBuilder#build()} and then published | ||
| * via {@link io.nats.client.JetStream#publish(io.nats.client.Message)}. | ||
| */ | ||
| public class ScheduleBasicsAlternate { | ||
|
|
||
| /** Stream name used by this example. */ | ||
| public static final String STREAM = "schedules-enabled"; | ||
|
|
||
| /** Prefix for all schedule subjects in this example. */ | ||
| public static final String SCHEDULE_PREFIX = "schedule."; | ||
|
|
||
| /** Prefix for all target subjects in this example. */ | ||
| public static final String TARGET_PREFIX = "target."; | ||
|
|
||
| private static final String SCHEDULES = SCHEDULE_PREFIX + ">"; | ||
| private static final String TARGETS = TARGET_PREFIX + "*"; | ||
|
|
||
| /** Subject patterns the example stream accepts. */ | ||
| public static final String[] STREAM_SUBJECTS = new String[]{SCHEDULES, TARGETS}; | ||
|
|
||
| private ScheduleBasicsAlternate() {} | ||
|
|
||
| /** | ||
| * Example entry point. | ||
| * @param args ignored | ||
| */ | ||
| public static void main(String[] args) { | ||
| try { | ||
| Options options = new Options.Builder() | ||
| .server("nats://localhost:4222") | ||
| .errorListener(new ErrorListener() {}) | ||
| .build(); | ||
|
|
||
| try (Connection connection = Nats.connect(options)) { | ||
| JetStreamManagement jsm = connection.jetStreamManagement(); | ||
| JetStream js = connection.jetStream(); | ||
|
|
||
| // delete the stream in case it existed, just for a fresh example | ||
| try { jsm.deleteStream(STREAM); } catch (Exception ignore) {} | ||
|
|
||
| // Use the utility to properly create a schedulable stream | ||
| StreamInfo si = ScheduleManagement.createSchedulableStream(jsm, STREAM, StorageType.Memory, STREAM_SUBJECTS); | ||
| report("Created stream", si.getConfiguration()); | ||
|
|
||
| CountDownLatch latch = new CountDownLatch(4); | ||
| Dispatcher d = connection.createDispatcher(); | ||
|
|
||
| // subscribe to the subject that receives the schedule message | ||
| js.subscribe(SCHEDULES, d, m -> { | ||
| report("SCHEDULED (received)", m); | ||
| m.ack(); | ||
| }, false); | ||
|
|
||
| // subscribe to the target subject | ||
| js.subscribe(TARGETS, d, m -> { | ||
| report("TARGETED (received)", m); | ||
| m.ack(); | ||
| latch.countDown(); | ||
| }, false); | ||
|
|
||
| Message m = new ScheduledMessageBuilder() | ||
| .scheduleSubject(SCHEDULE_PREFIX + "now") | ||
| .targetSubject(TARGET_PREFIX + "now") | ||
| .scheduleImmediate() | ||
| .data("Schedule-Now") | ||
| .build(); | ||
| report("SCHEDULE-NOW (publishing)", m); | ||
| js.publish(m); | ||
|
|
||
| m = new ScheduledMessageBuilder() | ||
| .scheduleSubject(SCHEDULE_PREFIX + "at") | ||
| .targetSubject(TARGET_PREFIX + "at") | ||
| .scheduleAt(DateTimeUtils.gmtNow().plusSeconds(5)) | ||
| .data("Scheduled-At") | ||
| .build(); | ||
| report("SCHEDULE-AT (publishing)", m); | ||
| js.publish(m); | ||
|
|
||
| m = new ScheduledMessageBuilder() | ||
| .scheduleSubject(SCHEDULE_PREFIX + "at") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this overwrite the above message? Should these be |
||
| .targetSubject(TARGET_PREFIX + "at") | ||
| .scheduleEvery(1, TimeUnit.SECONDS) | ||
| .data("Every Second") | ||
| .build(); | ||
| report("SCHEDULE-EVERY (publishing)", m); | ||
| js.publish(m); | ||
|
|
||
| latch.await(); | ||
| } | ||
| } | ||
| catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is unrelated housekeeping.