Search in sources :

Example 1 with MqttPublisher

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);
    }
}
Also used : MqttPublisher(org.apache.storm.mqtt.common.MqttPublisher)

Example 2 with MqttPublisher

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);
    }
}
Also used : MqttPublisher(org.apache.storm.mqtt.common.MqttPublisher) FailedException(org.apache.storm.topology.FailedException)

Example 3 with MqttPublisher

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);
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) LocalCluster(org.apache.storm.LocalCluster) MqttOptions(org.apache.storm.mqtt.common.MqttOptions) Message(org.fusesource.mqtt.client.Message) Config(org.apache.storm.Config) MqttPublisher(org.apache.storm.mqtt.common.MqttPublisher) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) URI(java.net.URI) LocalTopology(org.apache.storm.LocalCluster.LocalTopology) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test) IntegrationTest(org.apache.storm.testing.IntegrationTest)

Aggregations

MqttPublisher (org.apache.storm.mqtt.common.MqttPublisher)3 URI (java.net.URI)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalTopology (org.apache.storm.LocalCluster.LocalTopology)1 MqttOptions (org.apache.storm.mqtt.common.MqttOptions)1 IntegrationTest (org.apache.storm.testing.IntegrationTest)1 FailedException (org.apache.storm.topology.FailedException)1 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)1 MQTT (org.fusesource.mqtt.client.MQTT)1 Message (org.fusesource.mqtt.client.Message)1 Topic (org.fusesource.mqtt.client.Topic)1 Test (org.junit.Test)1