Skip to content

Commit 475a02c

Browse files
authored
Add support for Consumer Create Action (#1108)
1 parent d9eec8b commit 475a02c

File tree

7 files changed

+291
-37
lines changed

7 files changed

+291
-37
lines changed

src/main/java/io/nats/client/JetStreamManagement.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ public interface JetStreamManagement {
121121
*/
122122
ConsumerInfo addOrUpdateConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException;
123123

124+
/**
125+
* Creates a consumer. Must not already exist.
126+
* @param streamName name of the stream
127+
* @param config the consumer configuration to use.
128+
* @return consumer information.
129+
* @throws IOException covers various communication issues with the NATS
130+
* server such as timeout or interruption
131+
* @throws JetStreamApiException the request had an error related to the data such as the consumer already exists
132+
*/
133+
ConsumerInfo createConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException;
134+
135+
/**
136+
* Updates an existing consumer. Must already exist.
137+
* @param streamName name of the stream
138+
* @param config the consumer configuration to use.
139+
* @return consumer information.
140+
* @throws IOException covers various communication issues with the NATS
141+
* server such as timeout or interruption
142+
* @throws JetStreamApiException the request had an error related to the data such as the consumer does not already exist
143+
*/
144+
ConsumerInfo updateConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException;
145+
124146
/**
125147
* Deletes a consumer.
126148
* @param streamName name of the stream

src/main/java/io/nats/client/api/ConsumerCreateRequest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,39 @@
1616
import io.nats.client.support.JsonSerializable;
1717
import io.nats.client.support.JsonUtils;
1818

19-
import static io.nats.client.support.ApiConstants.CONFIG;
20-
import static io.nats.client.support.ApiConstants.STREAM_NAME;
19+
import static io.nats.client.support.ApiConstants.*;
2120
import static io.nats.client.support.JsonUtils.*;
2221

2322
/**
2423
* Object used to make a request to create a consumer. Used Internally
2524
*/
2625
public class ConsumerCreateRequest implements JsonSerializable {
26+
public enum Action {
27+
Create("create"),
28+
Update("update"),
29+
CreateOrUpdate(null);
30+
31+
public final String actionText;
32+
33+
Action(String actionText) {
34+
this.actionText = actionText;
35+
}
36+
}
37+
2738
private final String streamName;
2839
private final ConsumerConfiguration config;
40+
private final Action action;
2941

3042
public ConsumerCreateRequest(String streamName, ConsumerConfiguration config) {
3143
this.streamName = streamName;
3244
this.config = config;
45+
this.action = Action.CreateOrUpdate;
46+
}
47+
48+
public ConsumerCreateRequest(String streamName, ConsumerConfiguration config, Action action) {
49+
this.streamName = streamName;
50+
this.config = config;
51+
this.action = action;
3352
}
3453

3554
public String getStreamName() {
@@ -40,11 +59,16 @@ public ConsumerConfiguration getConfig() {
4059
return config;
4160
}
4261

62+
public Action getAction() {
63+
return action;
64+
}
65+
4366
@Override
4467
public String toJson() {
4568
StringBuilder sb = beginJson();
4669

4770
addField(sb, STREAM_NAME, streamName);
71+
JsonUtils.addField(sb, ACTION, action.actionText);
4872
JsonUtils.addField(sb, CONFIG, config);
4973

5074
return endJson(sb).toString();

src/main/java/io/nats/client/impl/NatsJetStreamImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ConsumerInfo _getConsumerInfo(String streamName, String consumerName) throws IOE
8181
return new ConsumerInfo(resp).throwOnHasError();
8282
}
8383

84-
ConsumerInfo _createConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException {
84+
ConsumerInfo _createConsumer(String streamName, ConsumerConfiguration config, ConsumerCreateRequest.Action action) throws IOException, JetStreamApiException {
8585
// ConsumerConfiguration validates that name and durable are the same if both are supplied.
8686
String consumerName = config.getName();
8787
if (consumerName != null && !consumerCreate290Available) {
@@ -118,14 +118,14 @@ else if (durable == null) {
118118
subj = String.format(JSAPI_DURABLE_CREATE, streamName, durable);
119119
}
120120

121-
ConsumerCreateRequest ccr = new ConsumerCreateRequest(streamName, config);
121+
ConsumerCreateRequest ccr = new ConsumerCreateRequest(streamName, config, action);
122122
Message resp = makeRequestResponseRequired(subj, ccr.serialize(), jso.getRequestTimeout());
123123
return new ConsumerInfo(resp).throwOnHasError();
124124
}
125125

126126
void _createConsumerUnsubscribeOnException(String stream, ConsumerConfiguration cc, NatsJetStreamSubscription sub) throws IOException, JetStreamApiException {
127127
try {
128-
ConsumerInfo ci = _createConsumer(stream, cc);
128+
ConsumerInfo ci = _createConsumer(stream, cc, ConsumerCreateRequest.Action.CreateOrUpdate);
129129
sub.setConsumerName(ci.getName());
130130
}
131131
catch (IOException | JetStreamApiException e) {

src/main/java/io/nats/client/impl/NatsJetStreamManagement.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,27 @@ public PurgeResponse purgeStream(String streamName, PurgeOptions options) throws
128128
public ConsumerInfo addOrUpdateConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException {
129129
validateStreamName(streamName, true);
130130
validateNotNull(config, "Config");
131-
return _createConsumer(streamName, config);
131+
return _createConsumer(streamName, config, ConsumerCreateRequest.Action.CreateOrUpdate);
132+
}
133+
134+
/**
135+
* {@inheritDoc}
136+
*/
137+
@Override
138+
public ConsumerInfo createConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException {
139+
validateStreamName(streamName, true);
140+
validateNotNull(config, "Config");
141+
return _createConsumer(streamName, config, ConsumerCreateRequest.Action.Create);
142+
}
143+
144+
/**
145+
* {@inheritDoc}
146+
*/
147+
@Override
148+
public ConsumerInfo updateConsumer(String streamName, ConsumerConfiguration config) throws IOException, JetStreamApiException {
149+
validateStreamName(streamName, true);
150+
validateNotNull(config, "Config");
151+
return _createConsumer(streamName, config, ConsumerCreateRequest.Action.Update);
132152
}
133153

134154
/**

src/main/java/io/nats/client/impl/OrderedMessageManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.nats.client.Message;
1818
import io.nats.client.SubscribeOptions;
1919
import io.nats.client.api.ConsumerConfiguration;
20+
import io.nats.client.api.ConsumerCreateRequest;
2021
import io.nats.client.api.ConsumerInfo;
2122

2223
import java.util.concurrent.atomic.AtomicReference;
@@ -98,7 +99,7 @@ private void handleErrorCondition() {
9899
// 3. make a new consumer using the same deliver subject but
99100
// with a new starting point
100101
ConsumerConfiguration userCC = js.consumerConfigurationForOrdered(originalCc, lastStreamSeq, newDeliverSubject, actualConsumerName, null);
101-
ConsumerInfo ci = js._createConsumer(stream, userCC); // this can fail when a server is down.
102+
ConsumerInfo ci = js._createConsumer(stream, userCC, ConsumerCreateRequest.Action.Create); // this can fail when a server is down.
102103
sub.setConsumerName(ci.getName());
103104

104105
// 4. restart the manager.

src/main/java/io/nats/client/support/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface ApiConstants {
1818
String ACK_FLOOR = "ack_floor";
1919
String ACK_POLICY = "ack_policy";
2020
String ACK_WAIT = "ack_wait";
21+
String ACTION = "action";
2122
String ACTIVE = "active";
2223
String ALLOW_ROLLUP_HDRS = "allow_rollup_hdrs";
2324
String ALLOW_DIRECT = "allow_direct";

0 commit comments

Comments
 (0)