Skip to content

Commit ed1af55

Browse files
committed
update docs, make jni/native functions private
1 parent f03c1d6 commit ed1af55

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/main/java/software/amazon/awssdk/crt/mqtt5/PublishReturn.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,10 @@ public PublishPacket getPublishPacket() {
4040
}
4141

4242
/**
43-
* Acquires manual control over the publish acknowledgement (PUBACK) for this PUBLISH message,
43+
* Acquires manual control over the publish acknowledgement for this PUBLISH message,
4444
* preventing the client from automatically sending an acknowledgement. The returned handle can be
4545
* passed to {@link Mqtt5Client#invokePublishAcknowledgement(Mqtt5PublishAcknowledgementControlHandle)}
46-
* at a later time to send the PUBACK to the broker.
47-
*
48-
* <p>The PUBACK control is eagerly acquired by the native layer as soon as the publish is received,
49-
* before this callback is invoked. Calling this method retrieves the pre-acquired control handle.</p>
46+
* at a later time to send the publish acknowledgement to the broker.
5047
*
5148
* <p><b>Important:</b> This method must be called within the
5249
* {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived} callback. Calling it after the
@@ -55,8 +52,8 @@ public PublishPacket getPublishPacket() {
5552
* <p>This method may only be called once per received PUBLISH. Subsequent calls will throw
5653
* an {@link IllegalStateException}.</p>
5754
*
58-
* <p>If this method is not called, the client will automatically send a PUBACK for QoS 1
59-
* messages when the callback returns.</p>
55+
* <p>If this method is not called, the client will automatically send a publish acknowledgment
56+
* for QoS 1 messages when the callback returns.</p>
6057
*
6158
* @return A {@link Mqtt5PublishAcknowledgementControlHandle} that can be used to manually send the acknowledgement.
6259
* @throws IllegalStateException if called outside the onMessageReceived callback, called more than once,
@@ -85,7 +82,7 @@ public synchronized Mqtt5PublishAcknowledgementControlHandle acquirePublishAckno
8582
*
8683
* @return {@code true} if the user acquired manual control of the PUBACK, {@code false} otherwise.
8784
*/
88-
boolean wasControlAcquired() {
85+
private boolean wasControlAcquired() {
8986
return controlAcquired;
9087
}
9188

@@ -97,7 +94,7 @@ boolean wasControlAcquired() {
9794
* <p>Zeroes out {@code controlId} so that any saved reference to this {@code PublishReturn}
9895
* cannot call {@code acquirePublishAcknowledgementControl()} after the callback has returned.</p>
9996
*/
100-
synchronized void invalidateAfterCallback() {
97+
private synchronized void invalidateAfterCallback() {
10198
controlId = 0;
10299
}
103100

src/native/mqtt5_client.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,19 @@ static void s_aws_mqtt5_client_java_publish_received(
617617
}
618618

619619
/*
620-
* After the callback returns, check whether the user called acquirePublishAcknowledgementControl()
621-
* by calling wasControlAcquired() on the PublishReturn object.
622-
*
623-
* If wasControlAcquired() returns false (or an exception occurred), the user did NOT take
624-
* manual control of the PUBACK, so we auto-invoke it to avoid losing it.
620+
* After the callback returns:
621+
* 1. Call invalidateAfterCallback() to zero out controlId in the PublishReturn, preventing
622+
* any post-callback call to acquirePublishAcknowledgementControl() from succeeding.
623+
* 2. Call wasControlAcquired() to check whether the user took manual control during the
624+
* callback. If they did NOT, auto-invoke the PUBACK to avoid losing it.
625625
*/
626626
if (control_id != 0 && publish_packet_return_data != NULL) {
627+
/* Invalidate the PublishReturn to prevent post-callback use */
628+
(*env)->CallVoidMethod(
629+
env, publish_packet_return_data, mqtt5_publish_return_properties.return_invalidate_after_callback_id);
630+
aws_jni_check_and_clear_exception(env); /* To hide JNI warning */
631+
632+
/* Check whether the user called acquirePublishAcknowledgementControl() during the callback */
627633
jboolean user_acquired_control = (*env)->CallBooleanMethod(
628634
env, publish_packet_return_data, mqtt5_publish_return_properties.return_was_control_acquired_id);
629635
aws_jni_check_and_clear_exception(env); /* To hide JNI warning */

0 commit comments

Comments
 (0)