@Override public void accept(Consumer<T> eventSubmitter) { this.eventSubmitter = eventSubmitter; try { connector.client(); // induce connecting. } catch (Exception e) { logger.error("{} setup failed topicFilter:{}", id(), topicFilter, e); } }
/** * Subscribe to the MQTT topic(s) and create a {@code TStream<String>}. * <p> * A convenience function. * Each message's payload is expected/required to be a UTF-8 encoded string. * Only the converted payload is present the generated tuple. * * @param <T> Tuple type * @param topicFilter the topic(s) to subscribe to. * @param qos the maximum Quality of Service to use. * @return {@code TStream<String>} * @see #publish(TStream, String, int, boolean) */ public <T> TStream<String> subscribe(String topicFilter, int qos) { addSubscribe(); return topology().events(new MqttSubscriber<String>(connector, topicFilter, qos, (topic, payload) -> new String(payload, StandardCharsets.UTF_8))); }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { // N.B. MQTT will close the connection if this unwinds try { logger.trace("{} received topic:{} qos:{} isRetained:{} {} bytes", id(), topic, message.getQos(), message.isRetained(), message.getPayload().length); notIdle(); subscriber.messageArrived(topic, message); } catch (Exception e) { logger.error("{} messageArrived handling failed", id(), e); } }
if (sub != null) { setIdleReconnectInterval(config.getSubscriberIdleReconnectInterval()); sub.connected(client);
/** * Subscribe to the MQTT topic(s) and create a stream of tuples of type {@code T}. * @param <T> Tuple type * @param topicFilter the topic(s) to subscribe to. * @param qos the maximum Quality of Service to use. * @param message2Tuple function to convert {@code (topic, payload)} to * a tuple of type {@code T} * @return {@code TStream<T>} */ public <T> TStream<T> subscribe(String topicFilter, int qos, BiFunction<String, byte[], T> message2Tuple) { addSubscribe(); return topology().events(new MqttSubscriber<T>(connector, topicFilter, qos, message2Tuple)); }
/** * To be called on initial connection and reconnects * @param client the MqttClient * @throws MqttException */ void connected(MqttClient client) throws MqttException { logger.info("{} subscribe({}, {})", id(), topicFilter, qos); client.subscribe(topicFilter, qos); }