@@ -30,14 +30,6 @@ public class PublishReturn {
3030 */
3131 private long threadID ;
3232
33- /**
34- * Set to true when {@code acquirePublishAcknowledgementControl()} is called, indicating that
35- * the user has taken manual control of the publish acknowledgement. Native code reads this
36- * via {@code wasControlAcquired()} after the callback returns to decide whether to
37- * auto-invoke the publish acknowledgement.
38- */
39- private boolean controlAcquired ;
40-
4133 /**
4234 * Returns the PublishPacket returned from the server or Null if none was returned.
4335 * @return The PublishPacket returned from the server.
@@ -53,58 +45,29 @@ public PublishPacket getPublishPacket() {
5345 * at a later time to send the publish acknowledgement to the broker.
5446 *
5547 * <p><b>Important:</b> This method must be called within the
56- * {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived} callback. Calling it after the
57- * callback returns will throw an {@link IllegalStateException }.</p>
48+ * {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived} callback. Calling it outside the
49+ * callback (wrong thread) or after it has already been called will return {@code null }.</p>
5850 *
59- * <p>This method may only be called once per received PUBLISH. Subsequent calls will throw
60- * an {@link IllegalStateException }.</p>
51+ * <p>This method may only be called once per received PUBLISH. Subsequent calls will return
52+ * {@code null }.</p>
6153 *
6254 * <p>If this method is not called, the client will automatically send a publish acknowledgment
6355 * for QoS 1 messages when the callback returns.</p>
6456 *
65- * @return A {@link Mqtt5PublishAcknowledgementControlHandle} that can be used to manually send the acknowledgement.
66- * @throws IllegalStateException if called outside the onMessageReceived callback, called more than once,
67- * or called on a QoS 0 message.
57+ * @return A {@link Mqtt5PublishAcknowledgementControlHandle} that can be used to manually send
58+ * the acknowledgement, or {@code null} if called outside the callback, called more than
59+ * once, or called on a QoS 0 message.
6860 */
6961 public synchronized Mqtt5PublishAcknowledgementControlHandle acquirePublishAcknowledgementControl () {
7062 if (controlId == 0 || threadID != Thread .currentThread ().getId ()) {
71- throw new IllegalStateException (
72- "acquirePublishAcknowledgementControl() must be called within the onMessageReceived callback and may only be called once." );
63+ return null ;
7364 }
7465 long acquiredControlId = controlId ;
7566 /* Zero out so it can't be double-called */
7667 controlId = 0 ;
77- controlAcquired = true ;
7868 return new Mqtt5PublishAcknowledgementControlHandle (acquiredControlId );
7969 }
8070
81- /**
82- * Returns whether the user called {@link #acquirePublishAcknowledgementControl()} during the
83- * {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived} callback.
84- *
85- * <p>This is called by native/JNI code after the callback returns to determine whether to
86- * automatically invoke the publish acknowledgement (PUBACK). If this returns {@code false},
87- * native code will auto-invoke the PUBACK; if {@code true}, the user is responsible for
88- * calling {@link Mqtt5Client#invokePublishAcknowledgement(Mqtt5PublishAcknowledgementControlHandle)}.</p>
89- *
90- * @return {@code true} if the user acquired manual control of the PUBACK, {@code false} otherwise.
91- */
92- private synchronized boolean wasControlAcquired () {
93- return controlAcquired ;
94- }
95-
96- /**
97- * Called by native/JNI code after the {@link Mqtt5ClientOptions.PublishEvents#onMessageReceived}
98- * callback returns to prevent post-callback use of
99- * {@link #acquirePublishAcknowledgementControl()}.
100- *
101- * <p>Zeroes out {@code controlId} so that any saved reference to this {@code PublishReturn}
102- * cannot call {@code acquirePublishAcknowledgementControl()} after the callback has returned.</p>
103- */
104- private synchronized void invalidateAfterCallback () {
105- controlId = 0 ;
106- }
107-
10871 /**
10972 * This is only called in JNI to make a new PublishReturn with a PUBLISH packet.
11073 * The controlId is eagerly acquired by native code prior to
@@ -119,7 +82,6 @@ private synchronized void invalidateAfterCallback() {
11982 private PublishReturn (PublishPacket newPublishPacket , long controlId ) {
12083 this .publishPacket = newPublishPacket ;
12184 this .controlId = controlId ;
122- this .controlAcquired = false ;
12385 this .threadID = Thread .currentThread ().getId ();
12486 }
12587}
0 commit comments