-
Notifications
You must be signed in to change notification settings - Fork 549
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
Add subscription status callback to subscribeToTopic #1086
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -1185,6 +1185,23 @@ public void resetReconnect() { | |
*/ | ||
public void subscribeToTopic(String topic, AWSIotMqttQos qos, | ||
AWSIotMqttNewMessageCallback callback) { | ||
subscribeToTopic(topic, qos, null, callback); | ||
} | ||
|
||
/** | ||
* Subscribes to an MQTT topic | ||
* | ||
* @param topic The topic to which to subscribe. | ||
* @param qos Quality of Service Level of the subscription. | ||
* @param subscriptionStatusCallback Callback that will be notified when the subscribe has completed. | ||
* Any exception encountered during the subscribe operation is reported on the callback | ||
* if avalialble, else AmazonClientException is thrown by this method. | ||
* @param callback Callback to be called when new message is received on this | ||
* topic for this subscription. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment in the Javadoc which says when the callback is invoked and when the AmazonClientException is thrown? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
*/ | ||
public void subscribeToTopic(final String topic, final AWSIotMqttQos qos, | ||
final AWSIotMqttSubscriptionStatusCallback subscriptionStatusCallback, | ||
final AWSIotMqttNewMessageCallback callback) { | ||
|
||
if (topic == null || topic.isEmpty()) { | ||
throw new IllegalArgumentException("topic is null or empty"); | ||
|
@@ -1196,9 +1213,27 @@ public void subscribeToTopic(String topic, AWSIotMqttQos qos, | |
|
||
if (null != mqttClient) { | ||
try { | ||
mqttClient.subscribe(topic, qos.asInt()); | ||
if (subscriptionStatusCallback != null) { | ||
mqttClient.subscribe(topic, qos.asInt(), null, new IMqttActionListener() { | ||
@Override | ||
public void onSuccess(IMqttToken asyncActionToken) { | ||
subscriptionStatusCallback.onSuccess(); | ||
} | ||
|
||
@Override | ||
public void onFailure(IMqttToken asyncActionToken, Throwable exception) { | ||
subscriptionStatusCallback.onFailure(exception); | ||
} | ||
}); | ||
} else { | ||
mqttClient.subscribe(topic, qos.asInt()); | ||
} | ||
} catch (final MqttException e) { | ||
throw new AmazonClientException("Client error when subscribing.", e); | ||
if(subscriptionStatusCallback != null) { | ||
subscriptionStatusCallback.onFailure(e); | ||
} else { | ||
throw new AmazonClientException("Client error when subscribing.", e); | ||
} | ||
} | ||
final AWSIotMqttTopic topicModel = new AWSIotMqttTopic(topic, qos, callback); | ||
topicListeners.put(topic, topicModel); | ||
|
33 changes: 33 additions & 0 deletions
33
...rc/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttSubscriptionStatusCallback.java
This file contains 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,33 @@ | ||
/** | ||
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* 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://aws.amazon.com/apache2.0 | ||
* | ||
* This file 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 com.amazonaws.mobileconnectors.iot; | ||
|
||
/** | ||
* Enables an application to be notified of status of call to subscribe to a topic. | ||
*/ | ||
public interface AWSIotMqttSubscriptionStatusCallback { | ||
/** | ||
* This method is invoked when a susbcription has completed successfully. | ||
*/ | ||
void onSuccess(); | ||
desokroshan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* This method is invoked when subscription fails. | ||
* If a client is disconnected while an subscription is in progress | ||
* onFailure will be called. | ||
*/ | ||
void onFailure(Throwable exception); | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you keep this test and add a new test for subscribeToTopic with callback for subscription status, that way we don't lose the coverage on the original method.