Skip to content

Commit

Permalink
General Retry Functionality (#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored May 19, 2024
1 parent 43bfd1b commit 1de9fa3
Show file tree
Hide file tree
Showing 9 changed files with 941 additions and 25 deletions.
77 changes: 77 additions & 0 deletions src/examples/java/io/nats/examples/RetrierPublishAsyncExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package io.nats.examples;

import io.nats.client.Connection;
import io.nats.client.JetStreamApiException;
import io.nats.client.Nats;
import io.nats.client.api.PublishAck;
import io.nats.client.api.StorageType;
import io.nats.client.api.StreamConfiguration;
import io.nats.client.utility.Retrier;
import io.nats.client.utility.RetryConfig;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class RetrierPublishAsyncExample {

public static String STREAM = "retrier";
public static String SUBJECT = "retriersub";

public static void main(String[] args) throws InterruptedException {
try (Connection nc = Nats.connect()) {
try {
nc.jetStreamManagement().deleteStream(STREAM);
}
catch (Exception ignore) {}

// since the default backoff is {250, 250, 500, 500, 3000, 5000}
new Thread(() -> {
try {
Thread.sleep(1100);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
try {
System.out.println("Creating Stream @ " + System.currentTimeMillis());
nc.jetStreamManagement().addStream(StreamConfiguration.builder()
.name(STREAM)
.subjects(SUBJECT)
.storageType(StorageType.Memory)
.build());
}
catch (IOException | JetStreamApiException e) {
throw new RuntimeException(e);
}
}).start();

RetryConfig config = RetryConfig.builder().attempts(10).build();
long now = System.currentTimeMillis();

System.out.println("Publishing @ " + now);
CompletableFuture<PublishAck> cfpa = Retrier.publishAsync(config, nc.jetStream(), SUBJECT, null);
PublishAck pa = cfpa.get(30, TimeUnit.SECONDS);
long done = System.currentTimeMillis();

System.out.println("Publish Ack: " + pa.getJv().toJson());
System.out.println("Done @ " + done + ", Elapsed: " + (done - now));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
74 changes: 74 additions & 0 deletions src/examples/java/io/nats/examples/RetrierPublishExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package io.nats.examples;

import io.nats.client.Connection;
import io.nats.client.JetStreamApiException;
import io.nats.client.Nats;
import io.nats.client.api.PublishAck;
import io.nats.client.api.StorageType;
import io.nats.client.api.StreamConfiguration;
import io.nats.client.utility.Retrier;
import io.nats.client.utility.RetryConfig;

import java.io.IOException;

public class RetrierPublishExample {

public static String STREAM = "retrier";
public static String SUBJECT = "retriersub";

public static void main(String[] args) throws InterruptedException {
try (Connection nc = Nats.connect()) {
try {
nc.jetStreamManagement().deleteStream(STREAM);
}
catch (Exception ignore) {}

// since the default backoff is {250, 250, 500, 500, 3000, 5000}
new Thread(() -> {
try {
Thread.sleep(1100);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
try {
System.out.println("Creating Stream @ " + System.currentTimeMillis());
nc.jetStreamManagement().addStream(StreamConfiguration.builder()
.name(STREAM)
.subjects(SUBJECT)
.storageType(StorageType.Memory)
.build());
}
catch (IOException | JetStreamApiException e) {
throw new RuntimeException(e);
}
}).start();

RetryConfig config = RetryConfig.builder().attempts(10).build();
long now = System.currentTimeMillis();

System.out.println("Publishing @ " + now);
PublishAck pa = Retrier.publish(config, nc.jetStream(), SUBJECT, null);
long done = System.currentTimeMillis();

System.out.println("Publish Ack: " + pa.getJv().toJson());
System.out.println("Done @ " + done + ", Elapsed: " + (done - now));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
36 changes: 12 additions & 24 deletions src/main/java/io/nats/client/JetStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public interface JetStream {
/**
* Send a message to the specified subject and waits for a response from
* Jetstream. The default publish options will be used.
* The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -51,8 +50,7 @@ public interface JetStream {
/**
* Send a message to the specified subject and waits for a response from
* Jetstream. The default publish options will be used.
* The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -77,8 +75,7 @@ public interface JetStream {

/**
* Send a message to the specified subject and waits for a response from
* Jetstream. The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* Jetstream. The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -102,8 +99,7 @@ public interface JetStream {

/**
* Send a message to the specified subject and waits for a response from
* Jetstream. The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* Jetstream. The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -130,8 +126,7 @@ public interface JetStream {
/**
* Send a message to the specified subject and waits for a response from
* Jetstream. The default publish options will be used.
* The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -158,8 +153,7 @@ public interface JetStream {

/**
* Send a message to the specified subject and waits for a response from
* Jetstream. The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* Jetstream. The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand Down Expand Up @@ -188,8 +182,7 @@ public interface JetStream {
/**
* Send a message to the specified subject but does not wait for a response from
* Jetstream. The default publish options will be used.
* The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -214,8 +207,7 @@ public interface JetStream {
/**
* Send a message to the specified subject but does not wait for a response from
* Jetstream. The default publish options will be used.
* The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -241,8 +233,7 @@ public interface JetStream {

/**
* Send a message to the specified subject but does not wait for a response from
* Jetstream. The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* Jetstream. The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -267,8 +258,7 @@ public interface JetStream {

/**
* Send a message to the specified subject but does not wait for a response from
* Jetstream. The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* Jetstream. The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand Down Expand Up @@ -296,8 +286,7 @@ public interface JetStream {
/**
* Send a message to the specified subject but does not wait for a response from
* Jetstream. The default publish options will be used.
* The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand All @@ -323,8 +312,7 @@ public interface JetStream {

/**
* Send a message to the specified subject but does not wait for a response from
* Jetstream. The message body <strong>will not</strong> be copied. The expected
* usage with string content is something like:
* Jetstream. The expected usage with string content is something like:
*
* <pre>
* nc = Nats.connect()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/nats/client/PublishOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public String getMessageId() {

/**
* Creates a builder for the options.
* @return the builder.s
* @return the builder
*/
public static Builder builder() {
return new Builder();
Expand Down
Loading

0 comments on commit 1de9fa3

Please sign in to comment.