/** * Subscribes to an MQTT topic. * * @param topic The topic to which to subscribe. * @param qos Quality of Service Level of the subscription. * @param callback Callback to be called when new message is received on * this topic for this subscription. */ public void subscribeToTopic(String topic, AWSIotMqttQos qos, AWSIotMqttNewMessageCallback callback) { if (topic == null || topic.isEmpty()) { throw new IllegalArgumentException("topic is null or empty"); } if (qos == null) { throw new IllegalArgumentException("QoS cannot be null."); } if (null != mqttClient) { try { mqttClient.subscribe(topic, qos.asInt()); } catch (final MqttException e) { throw new AmazonClientException("Client error when subscribing.", e); } final AWSIotMqttTopic topicModel = new AWSIotMqttTopic(topic, qos, callback); topicListeners.put(topic, topicModel); } }
@Test public void testSetCallback() throws Exception { topicModel.setCallback(callback2); assertEquals(callback2, topicModel.getCallback()); topicModel.setCallback(callback1); } }
/** * Resubscribe to previously subscribed topics on reconnecting. */ void resubscribeToTopics() { LOGGER.info("Auto-resubscribe is enabled. Resubscribing to previous topics."); for (final AWSIotMqttTopic topic : topicListeners.values()) { if (mqttClient != null) { try { mqttClient.subscribe(topic.getTopic(), topic.getQos().asInt()); } catch (final MqttException e) { LOGGER.error("Error while resubscribing to previously subscribed toipcs.", e); } } } }
@Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { LOGGER.info("message arrived on topic: " + topic); final byte[] data = mqttMessage.getPayload(); for (final String topicKey : topicListeners.keySet()) { if (isTopicMatch(topicKey, topic)) { final AWSIotMqttTopic topicModel = topicListeners.get(topicKey); if (topicModel != null) { if (topicModel.getCallback() != null) { topicModel.getCallback().onMessageArrived(topic, data); } } } } }
@Test public void testGetQos() throws Exception { assertEquals(AWSIotMqttQos.QOS0, topicModel.getQos()); }
@Test public void testGetTopic() throws Exception { assertEquals("test-topic", topicModel.getTopic()); }
@Test public void testGetCallback() throws Exception { assertEquals(callback1, topicModel.getCallback()); }
/** * Resubscribe to previously subscribed topics on reconnecting. */ void resubscribeToTopics() { LOGGER.info("Auto-resubscribe is enabled. Resubscribing to previous topics."); for (final AWSIotMqttTopic topic : topicListeners.values()) { if (mqttClient != null) { try { mqttClient.subscribe(topic.getTopic(), topic.getQos().asInt()); } catch (final MqttException e) { LOGGER.error("Error while resubscribing to previously subscribed toipcs.", e); } } } }
@Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { LOGGER.info("message arrived on topic: " + topic); final byte[] data = mqttMessage.getPayload(); for (final String topicKey : topicListeners.keySet()) { if (isTopicMatch(topicKey, topic)) { final AWSIotMqttTopic topicModel = topicListeners.get(topicKey); if (topicModel != null) { if (topicModel.getCallback() != null) { topicModel.getCallback().onMessageArrived(topic, data); } } } } }
@Before public void setUp() throws Exception { topicModel = new AWSIotMqttTopic("test-topic", AWSIotMqttQos.QOS0, callback1); }
/** * Subscribes to an MQTT topic. * * @param topic The topic to which to subscribe. * @param qos Quality of Service Level of the subscription. * @param callback Callback to be called when new message is received on * this topic for this subscription. */ public void subscribeToTopic(String topic, AWSIotMqttQos qos, AWSIotMqttNewMessageCallback callback) { if (topic == null || topic.isEmpty()) { throw new IllegalArgumentException("topic is null or empty"); } if (qos == null) { throw new IllegalArgumentException("QoS cannot be null."); } if (null != mqttClient) { try { mqttClient.subscribe(topic, qos.asInt()); } catch (final MqttException e) { throw new AmazonClientException("Client error when subscribing.", e); } final AWSIotMqttTopic topicModel = new AWSIotMqttTopic(topic, qos, callback); topicListeners.put(topic, topicModel); } }