public class MqttStreams
extends java.lang.Object
MqttStreams
is a connector to a MQTT messaging broker
for publishing and subscribing to topics.
For more information about MQTT see http://mqtt.org
The connector exposes all MQTT capabilities:
Sample use:
Topology t = ...;
String url = "tcp://localhost:1883";
MqttStreams mqtt = new MqttStreams(t, url);
TStream<String> s = top.constants("one", "two", "three");
mqtt.publish(s, "myTopic", 0);
TStream<String> rcvd = mqtt.subscribe("someTopic", 0);
rcvd.print();
publish()
can be called zero or more times.
subscribe()
can be called at most once.
See MqttConfig
for all configuration items.
Messages are delivered with the specified Quality of Service. TODO adjust the QoS-1 and 2 descriptions based on the fact that we only supply a MemoryPersistence class under the covers.
subscribe()
, the QoS is the maximum QoS used for a message.
If a message was published with a QoS less then the subscribe's QoS,
the message will be received with the published QoS,
otherwise it will be received with the subscribe's QoS.Constructor and Description |
---|
MqttStreams(Topology topology,
java.lang.String url,
java.lang.String clientId)
Create a connector to the specified server.
|
MqttStreams(Topology topology,
Supplier<MqttConfig> config)
Create a connector with the specified configuration.
|
Modifier and Type | Method and Description |
---|---|
TSink<java.lang.String> |
publish(TStream<java.lang.String> stream,
java.lang.String topic,
int qos,
boolean retain)
Publish a
TStream<String> stream's tuples as MQTT messages. |
<T> TSink<T> |
publish(TStream<T> stream,
Function<T,java.lang.String> topic,
Function<T,byte[]> payload,
Function<T,java.lang.Integer> qos,
Function<T,java.lang.Boolean> retain)
Publish a stream's tuples as MQTT messages.
|
<T> TStream<java.lang.String> |
subscribe(java.lang.String topicFilter,
int qos)
Subscribe to the MQTT topic(s) and create a
TStream<String> . |
<T> TStream<T> |
subscribe(java.lang.String topicFilter,
int qos,
BiFunction<java.lang.String,byte[],T> message2Tuple)
Subscribe to the MQTT topic(s) and create a stream of tuples of type
T . |
Topology |
topology()
Get the
Topology the connector is associated with. |
public MqttStreams(Topology topology, java.lang.String url, java.lang.String clientId)
A convenience function. Connecting to the server occurs after the topology is submitted for execution.
topology
- the connector's associated Topology
.url
- URL of MQTT server.clientId
- the connector's MQTT clientId. auto-generated if null.public MqttStreams(Topology topology, Supplier<MqttConfig> config)
Connecting to the server occurs after the topology is submitted for execution.
topology
- the Topology to add toconfig
- MqttConfig
supplier.public <T> TSink<T> publish(TStream<T> stream, Function<T,java.lang.String> topic, Function<T,byte[]> payload, Function<T,java.lang.Integer> qos, Function<T,java.lang.Boolean> retain)
Each tuple is published as an MQTT message with the supplied functions providing the message topic, payload and QoS. The topic and QoS can be generated based upon the tuple.
T
- Tuple typestream
- Stream to be published.topic
- function to supply the message's topic.payload
- function to supply the message's payload.qos
- function to supply the message's delivery Quality of Service.retain
- function to supply the message's retain valuepublic TSink<java.lang.String> publish(TStream<java.lang.String> stream, java.lang.String topic, int qos, boolean retain)
TStream<String>
stream's tuples as MQTT messages.
A convenience function. The payload of each message is the String tuple's value serialized as UTF-8.
stream
- Stream to be published.topic
- the fixed topic.qos
- the fixed delivery Quality of Service.retain
- the fixed retain value.public <T> TStream<T> subscribe(java.lang.String topicFilter, int qos, BiFunction<java.lang.String,byte[],T> message2Tuple)
T
.T
- Tuple typetopicFilter
- the topic(s) to subscribe to.qos
- the maximum Quality of Service to use.message2Tuple
- function to convert (topic, payload)
to
a tuple of type T
TStream<T>
public <T> TStream<java.lang.String> subscribe(java.lang.String topicFilter, int qos)
TStream<String>
.
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.
T
- Tuple typetopicFilter
- the topic(s) to subscribe to.qos
- the maximum Quality of Service to use.TStream<String>
publish(TStream, String, int, boolean)
Copyright © 2016 The Apache Software Foundation. All Rights Reserved - bbe71fa-20161201-1641