Search in sources :

Example 1 with MqttOptions

use of org.apache.storm.mqtt.common.MqttOptions in project storm by apache.

the class StormMqttIntegrationTest method buildMqttTopology.

public StormTopology buildMqttTopology() {
    TopologyBuilder builder = new TopologyBuilder();
    MqttOptions options = new MqttOptions();
    options.setTopics(Arrays.asList(TEST_TOPIC));
    options.setCleanConnection(false);
    TestSpout spout = new TestSpout(new StringMessageMapper(), options);
    MqttBolt bolt = new MqttBolt(options, new MqttTupleMapper() {

        @Override
        public MqttMessage toMessage(ITuple tuple) {
            LOG.info("Received: {}", tuple);
            return new MqttMessage(RESULT_TOPIC, RESULT_PAYLOAD.getBytes());
        }
    });
    builder.setSpout("mqtt-spout", spout);
    builder.setBolt("mqtt-bolt", bolt).shuffleGrouping("mqtt-spout");
    return builder.createTopology();
}
Also used : MqttOptions(org.apache.storm.mqtt.common.MqttOptions) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) MqttBolt(org.apache.storm.mqtt.bolt.MqttBolt) ITuple(org.apache.storm.tuple.ITuple) StringMessageMapper(org.apache.storm.mqtt.mappers.StringMessageMapper)

Example 2 with MqttOptions

use of org.apache.storm.mqtt.common.MqttOptions 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

MqttOptions (org.apache.storm.mqtt.common.MqttOptions)2 URI (java.net.URI)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalTopology (org.apache.storm.LocalCluster.LocalTopology)1 MqttBolt (org.apache.storm.mqtt.bolt.MqttBolt)1 MqttPublisher (org.apache.storm.mqtt.common.MqttPublisher)1 StringMessageMapper (org.apache.storm.mqtt.mappers.StringMessageMapper)1 IntegrationTest (org.apache.storm.testing.IntegrationTest)1 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)1 ITuple (org.apache.storm.tuple.ITuple)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