use of org.apache.storm.mqtt.common.MqttPublisher in project storm by apache.
the class MqttBolt method prepare.
@Override
public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
this.collector = collector;
this.topologyName = (String) conf.get(Config.TOPOLOGY_NAME);
this.publisher = new MqttPublisher(this.options, this.keyStoreLoader, this.retain);
try {
this.publisher.connectMqtt(this.topologyName + "-" + context.getThisComponentId() + "-" + context.getThisTaskId());
} catch (Exception e) {
LOG.error("Unable to connect to MQTT Broker.", e);
throw new RuntimeException("Unable to connect to MQTT Broker.", e);
}
}
use of org.apache.storm.mqtt.common.MqttPublisher in project storm by apache.
the class MqttPublishFunction method prepare.
@Override
public void prepare(Map conf, TridentOperationContext context) {
this.topologyName = (String) conf.get(Config.TOPOLOGY_NAME);
this.publisher = new MqttPublisher(this.options, this.keyStoreLoader, this.retain);
try {
this.publisher.connectMqtt(this.topologyName + "-" + context.getPartitionIndex());
} catch (Exception e) {
LOG.error("Unable to connect to MQTT Broker.", e);
throw new RuntimeException("Unable to connect to MQTT Broker.", e);
}
}
use of org.apache.storm.mqtt.common.MqttPublisher in project storm by apache.
the class StormMqttIntegrationTest method testMqttTopology.
@Test
public void testMqttTopology() throws Exception {
MQTT client = new MQTT();
client.setTracer(new MqttLogger());
URI uri = URI.create("tcp://localhost:1883");
client.setHost(uri);
client.setClientId("MQTTSubscriber");
client.setCleanSession(false);
BlockingConnection connection = client.blockingConnection();
connection.connect();
Topic[] topics = { new Topic("/integration-result", QoS.AT_LEAST_ONCE) };
byte[] qoses = connection.subscribe(topics);
try (LocalCluster cluster = new LocalCluster();
LocalTopology topo = cluster.submitTopology("test", new Config(), buildMqttTopology())) {
LOG.info("topology started");
while (!spoutActivated) {
Thread.sleep(500);
}
// publish a retained message to the broker
MqttOptions options = new MqttOptions();
options.setCleanConnection(false);
MqttPublisher publisher = new MqttPublisher(options, true);
publisher.connectMqtt("MqttPublisher");
publisher.publish(new MqttMessage(TEST_TOPIC, "test".getBytes()));
LOG.info("published message");
Message message = connection.receive();
LOG.info("Message recieved on topic: {}", message.getTopic());
LOG.info("Payload: {}", new String(message.getPayload()));
message.ack();
Assert.assertArrayEquals(message.getPayload(), RESULT_PAYLOAD.getBytes());
Assert.assertEquals(message.getTopic(), RESULT_TOPIC);
}
}
Aggregations