-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor ackMode to AckBehavior #67
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c2b7327
Refactor ackMode to AckPolicy
tilakraj94 5420d31
resolve comments
tilakraj94 fc0cab2
update code to support ack behavior
tilakraj94 1813633
re-indent explicit source config json
tilakraj94 9d450ec
rename files
tilakraj94 cbc418f
resolve comments
tilakraj94 d44da9f
add blank lines at the eof
tilakraj94 e479af3
refactor comments from ack policy to ack behavior
tilakraj94 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
258 changes: 258 additions & 0 deletions
258
src/examples/java/io/synadia/flink/examples/JetStreamExplicitButDoNotAckExample.java
Large diffs are not rendered by default.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/examples/java/io/synadia/flink/examples/JetStreamExplicitSourceConnector.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,29 @@ | ||
| // Copyright (c) 2023-2025 Synadia Communications Inc. All Rights Reserved. | ||
| // See LICENSE and NOTICE file for details. | ||
|
|
||
| package io.synadia.flink.examples; | ||
|
|
||
| import io.nats.client.Message; | ||
| import io.nats.client.support.JsonUtils; | ||
| import io.synadia.flink.message.SourceConverter; | ||
| import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class JetStreamExplicitSourceConnector implements SourceConverter<String> { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| @Override | ||
| public String convert(Message message) { | ||
| StringBuilder stringBuilder = JsonUtils.beginJson(); | ||
| String data = new String(message.getData(), StandardCharsets.UTF_8); | ||
| JsonUtils.addField(stringBuilder, "data", data); | ||
| JsonUtils.addField(stringBuilder, "reply_to", message.getReplyTo()); | ||
| return JsonUtils.endJson(stringBuilder).toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public TypeInformation<String> getProducedType() { | ||
| return TypeInformation.of(String.class); | ||
| } | ||
| } | ||
|
|
64 changes: 64 additions & 0 deletions
64
src/examples/java/io/synadia/flink/examples/support/AckMapFunction.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,64 @@ | ||
| // Copyright (c) 2023-2025 Synadia Communications Inc. All Rights Reserved. | ||
| // See LICENSE and NOTICE file for details. | ||
|
|
||
| package io.synadia.flink.examples.support; | ||
|
|
||
| import io.nats.client.Connection; | ||
| import io.nats.client.impl.AckType; | ||
| import io.nats.client.support.JsonParser; | ||
| import io.nats.client.support.JsonValue; | ||
| import org.apache.flink.api.common.functions.RichMapFunction; | ||
| import org.apache.flink.configuration.Configuration; | ||
| import org.apache.flink.util.FlinkRuntimeException; | ||
|
|
||
| // ===================================================================================================================================== | ||
| // This class is used to simulate acknowledge messages in a Flink job that processes & acks NATS messages. | ||
| // It connects to a NATS server and sends an acknowledgment message back to the replyTo subject specified in the input JSON. | ||
| // The input JSON is expected to have a "data" field containing the message data and a "reply_to" field specifying where to send the ack. | ||
| // ===================================================================================================================================== | ||
| public class AckMapFunction extends RichMapFunction<String, String> { | ||
| private static final byte[] ACK_BODY_BYTES = AckType.AckAck.bodyBytes(-1); | ||
| private transient Connection natsCtx; | ||
|
|
||
| @Override | ||
| public void open(Configuration parameters) throws Exception { | ||
| try { | ||
| this.natsCtx = ExampleUtils.connect(ExampleUtils.EXAMPLES_CONNECTION_PROPERTIES_FILE); | ||
| } | ||
| catch (Exception e){ | ||
| System.out.println("Error connecting to NATS server: " + e); | ||
| throw new FlinkRuntimeException("Error connecting to NATS server", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String map(String s) throws Exception { | ||
| try { | ||
| JsonValue parsed = JsonParser.parse(s); | ||
| JsonValue data = parsed.map.get("data"); | ||
| if (data == null) { | ||
| throw new IllegalArgumentException("data field is missing in the input: " + s); | ||
| } | ||
|
|
||
| JsonValue replyTo = parsed.map.get("reply_to"); | ||
| if (replyTo == null) { | ||
| throw new IllegalArgumentException("reply_to is missing in the input: " + s); | ||
| } | ||
|
|
||
| natsCtx.publish(replyTo.string, ACK_BODY_BYTES); | ||
| return data.string; | ||
| } | ||
| catch (Exception e) { | ||
| System.out.println("Error processing message: " + e); | ||
| throw new FlinkRuntimeException("Error processing message: " + s, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws Exception { | ||
| if (natsCtx != null) { | ||
| natsCtx.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
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,31 @@ | ||
| { | ||
| "source_converter_class_name": "io.synadia.flink.examples.JetStreamExplicitSourceConnector", | ||
| "jetstream_subject_configurations": [ | ||
| { | ||
| "stream_name": "source-b", | ||
| "subject": "ub1", | ||
| "ack_behavior": "ExplicitButDoNotAck" | ||
| }, | ||
| { | ||
| "stream_name": "source-b", | ||
| "subject": "ub3", | ||
| "ack_behavior": "ExplicitButDoNotAck" | ||
| }, | ||
| { | ||
| "stream_name": "source-b", | ||
| "subject": "ub4", | ||
| "ack_behavior": "ExplicitButDoNotAck" | ||
| }, | ||
| { | ||
| "stream_name": "source-a", | ||
| "subject": "ua", | ||
| "ack_behavior": "ExplicitButDoNotAck" | ||
| }, | ||
| { | ||
| "stream_name": "source-b", | ||
| "subject": "ub2", | ||
| "ack_behavior": "ExplicitButDoNotAck" | ||
| } | ||
| ] | ||
| } | ||
|
|
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,19 @@ | ||
| --- | ||
| source_converter_class_name: io.synadia.flink.examples.JetStreamExplicitSourceConnector | ||
| jetstream_subject_configurations: | ||
| - stream_name: source-b | ||
| subject: ub1 | ||
| ack_behavior: ExplicitButDoNotAck | ||
| - stream_name: source-b | ||
| subject: ub3 | ||
| ack_behavior: ExplicitButDoNotAck | ||
| - stream_name: source-b | ||
| subject: ub4 | ||
| ack_behavior: ExplicitButDoNotAck | ||
| - stream_name: source-a | ||
| subject: ua | ||
| ack_behavior: ExplicitButDoNotAck | ||
| - stream_name: source-b | ||
| subject: ub2 | ||
| ack_behavior: ExplicitButDoNotAck | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) 2023-2025 Synadia Communications Inc. All Rights Reserved. | ||
| // See LICENSE and NOTICE file for details. | ||
|
|
||
| package io.synadia.flink.source; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public enum AckBehavior { | ||
| NoAck("NoAck"), | ||
| AckAll("AckAll"), | ||
| AllButDoNotAck("AllButDoNotAck"), | ||
| ExplicitButDoNotAck("ExplicitButDoNotAck"); | ||
|
|
||
| private final String behavior; | ||
| private static final Map<String, AckBehavior> strEnumHash = new HashMap(); | ||
|
|
||
| AckBehavior(String p) { | ||
| this.behavior = p; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return this.behavior; | ||
| } | ||
|
|
||
| public static AckBehavior get(String value) { | ||
| return strEnumHash.get(value); | ||
| } | ||
|
|
||
| static { | ||
| for(AckBehavior env : values()) { | ||
| strEnumHash.put(env.toString(), env); | ||
| } | ||
| } | ||
| } | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.