/** * 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 testValues() throws Exception { AWSIotMqttQos[] values = new AWSIotMqttQos[] { AWSIotMqttQos.QOS0, AWSIotMqttQos.QOS1 }; Arrays.asList(AWSIotMqttQos.values()).containsAll(Arrays.asList(values)); }
/** * 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); } } } }
if (mqttMessageQueue.isEmpty()) { try { mqttClient.publish(topic, data, qos.asInt(), false, publishMessageUserData, null); } catch (final MqttException e) { notifyPublishResult(callback, AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Fail,
mqttLWT.getQos().asInt(), false);
mqttLWT.getQos().asInt(), false);
mqttLWT.getQos().asInt(), false);
assertEquals("test payload 0", new String(mockClient.mostRecentPublishPayload)); assertEquals("test/topic", mockClient.mostRecentPublishTopic); assertEquals(AWSIotMqttQos.QOS1.asInt(), mockClient.mostRecentPublishQoS); assertEquals(9, testClient.getMqttMessageQueue().size());
@Test public void testConnectWithLwt() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); AWSIotMqttLastWillAndTestament lwt = new AWSIotMqttLastWillAndTestament("test/lwt", "bye", AWSIotMqttQos.QOS0); testClient.setMqttLastWillAndTestament(lwt); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper .getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); mockClient.mockConnectSuccess(); assertEquals(1, mockClient.connectCalls); assertTrue(mockClient.mostRecentOptions.isCleanSession()); assertEquals(300, mockClient.mostRecentOptions.getKeepAliveInterval()); assertEquals("test/lwt", mockClient.mostRecentOptions.getWillDestination()); assertEquals("bye", new String(mockClient.mostRecentOptions.getWillMessage().getPayload())); assertEquals(AWSIotMqttQos.QOS0.asInt(), mockClient.mostRecentOptions.getWillMessage().getQos()); assertFalse(mockClient.mostRecentOptions.getWillMessage().isRetained()); assertEquals(2, csb.statuses.size()); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connecting, csb.statuses.get(0)); assertEquals(AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected, csb.statuses.get(1)); assertEquals(MqttManagerConnectionState.Connected, testClient.getConnectionState()); }
/** * 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); } }
assertEquals("test payload 0", new String(mockClient.mostRecentPublishPayload)); assertEquals("test/topic", mockClient.mostRecentPublishTopic); assertEquals(AWSIotMqttQos.QOS0.asInt(), mockClient.mostRecentPublishQoS); assertEquals(9, testClient.getMqttMessageQueue().size());
assertEquals("test payload 0", new String(mockClient.mostRecentPublishPayload)); assertEquals("test/topic", mockClient.mostRecentPublishTopic); assertEquals(AWSIotMqttQos.QOS0.asInt(), mockClient.mostRecentPublishQoS); assertEquals(9, testClient.getMqttMessageQueue().size());
/** * 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); } } } }
if (mqttMessageQueue.isEmpty()) { try { mqttClient.publish(topic, data, qos.asInt(), false, publishMessageUserData, null); } catch (final MqttException e) { notifyPublishResult(callback, AWSIotMqttMessageDeliveryCallback.MessageDeliveryStatus.Fail,
assertEquals("test/lwt", mockClient.mostRecentOptions.getWillDestination()); assertEquals("bye", new String(mockClient.mostRecentOptions.getWillMessage().getPayload())); assertEquals(AWSIotMqttQos.QOS0.asInt(), mockClient.mostRecentOptions.getWillMessage().getQos()); assertFalse(mockClient.mostRecentOptions.getWillMessage().isRetained());
mqttLWT.getQos().asInt(), false);