Search in sources :

Example 6 with MqttStreams

use of com.ibm.streamsx.topology.messaging.mqtt.MqttStreams in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testReusableApp.

@Test
public void testReusableApp() throws Exception {
    checkAssumes();
    setupDebug();
    Topology top = new Topology("testReusableApp");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String subClientId = newSubClientId(top.getName());
    String pubClientId = newPubClientId(top.getName());
    String topic = getMqttTopics()[0];
    List<Message> msgs = createMsgs(mgen, null);
    // Test an app structured more as a "reusable asset" - i.e.,
    // where the mqtt connection info (URI, authInfo) and
    // topic are defined at submission time.
    // define/create the app's submission parameters
    ParameterHelper params = new ParameterHelper(top);
    params.definitions().put("mqtt.serverURI", String.class);
    params.definitions().put("mqtt.userID", System.getProperty("user.name"));
    params.definitions().put("mqtt.password", String.class);
    params.definitions().put("mqtt.pub.topic", String.class);
    params.definitions().put("mqtt.sub.topic", String.class);
    params.createAll();
    // add the actual param values for our call to submit()
    Map<String, Object> submitParams = new HashMap<>();
    submitParams.put("mqtt.serverURI", "tcp://localhost:1883");
    // submitParams.put("mqtt.userID", System.getProperty("user.name"));
    submitParams.put("mqtt.password", "myMosquittoPw");
    submitParams.put("mqtt.pub.topic", topic);
    submitParams.put("mqtt.sub.topic", topic);
    getConfig().put(ContextProperties.SUBMISSION_PARAMS, submitParams);
    // Produce and consume the msgs
    Map<String, Object> pconfig = createConfig(pubClientId);
    addMqttParams(pconfig, false, params);
    Map<String, Object> cconfig = createConfig(subClientId);
    addMqttParams(cconfig, true, params);
    MqttStreams producer = new MqttStreams(top, pconfig);
    MqttStreams consumer = new MqttStreams(top, cconfig);
    TStream<Message> msgsToPublish = top.constants(msgs).modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    TSink sink = producer.publish(msgsToPublish, params.getString("mqtt.pub.topic"));
    TStream<Message> rcvdMsgs = consumer.subscribe(params.getString("mqtt.sub.topic"));
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    msgs = modifyList(msgs, setTopic(topic));
    List<String> expectedAsString = mapList(msgs, msgToJSONStringFunc());
    if (testBuildOnly(top))
        return;
    completeAndValidate(subClientId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
    assertTrue(sink != null);
}
Also used : TSink(com.ibm.streamsx.topology.TSink) MqttStreams(com.ibm.streamsx.topology.messaging.mqtt.MqttStreams) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) HashMap(java.util.HashMap) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) JSONObject(com.ibm.json.java.JSONObject) Test(org.junit.Test)

Example 7 with MqttStreams

use of com.ibm.streamsx.topology.messaging.mqtt.MqttStreams in project streamsx.topology by IBMStreams.

the class MqttSample method publishSubscribe.

/**
     * Publish some messages to a topic; subscribe to the topic and report
     * received messages.
     * @param contextType string value of a {@code StreamsContext.Type}
     * @throws Exception
     */
public void publishSubscribe(String contextType) throws Exception {
    Map<String, Object> contextConfig = new HashMap<>();
    initContextConfig(contextConfig);
    Topology top = new Topology("mqttSample");
    // A compile time MQTT topic value.
    Supplier<String> topic = new Value<String>(TOPIC);
    // Create the MQTT connector
    MqttStreams mqtt = new MqttStreams(top, createMqttConfig());
    // Create a stream of messages and for the sample, give the
    // consumer a change to become ready
    TStream<Message> msgs = makeStreamToPublish(top).modify(initialDelayFunc(PUB_DELAY_MSEC));
    // Publish the message stream to the topic
    mqtt.publish(msgs, topic);
    // Subscribe to the topic and report received messages
    TStream<Message> rcvdMsgs = mqtt.subscribe(topic);
    rcvdMsgs.print();
    // Submit the topology, to send and receive the messages.
    Future<?> future = StreamsContextFactory.getStreamsContext(contextType).submit(top, contextConfig);
    if (contextType.contains("DISTRIBUTED")) {
        System.out.println("\nSee the job's PE console logs for the topology output.\n" + "Use Streams Studio or streamtool.  e.g.,\n" + "    # identify the job's \"Print\" PE\n" + "    streamtool lspes --jobs " + future.get() + "\n" + "    # print the PE's console log\n" + "    streamtool viewlog --print --console --pe <the-peid>" + "\n");
        System.out.println("Cancel the job using Streams Studio or streamtool. e.g.,\n" + "    streamtool canceljob " + future.get() + "\n");
    } else if (contextType.contains("STANDALONE")) {
        Thread.sleep(15000);
        future.cancel(true);
    }
}
Also used : MqttStreams(com.ibm.streamsx.topology.messaging.mqtt.MqttStreams) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) HashMap(java.util.HashMap) Value(com.ibm.streamsx.topology.logic.Value) Topology(com.ibm.streamsx.topology.Topology)

Example 8 with MqttStreams

use of com.ibm.streamsx.topology.messaging.mqtt.MqttStreams in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testExplicitTopicProducerSingleConnector.

@Test
public void testExplicitTopicProducerSingleConnector() throws Exception {
    checkAssumes();
    setupDebug();
    Topology top = new Topology("testExplicitTopicProducerSingleConnector");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String clientId = newPubClientId(top.getName());
    String topicVal = getMqttTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    List<Message> msgs = createMsgs(mgen, null);
    List<String> expectedAsString = mapList(modifyList(msgs, setTopic(topicVal)), msgToJSONStringFunc());
    // Test producer that takes an explicit topic and implicit config qos
    MqttStreams mqtt = new MqttStreams(top, createConfig(clientId));
    TStream<Message> msgsToPublish = top.constants(msgs).modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    TSink sink = mqtt.publish(msgsToPublish, topic);
    TStream<Message> rcvdMsgs = mqtt.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    if (testBuildOnly(top))
        return;
    completeAndValidate(clientId, top, rcvdAsString, SEC_TIMEOUT, expectedAsString.toArray(new String[0]));
    assertTrue(sink != null);
}
Also used : TSink(com.ibm.streamsx.topology.TSink) MqttStreams(com.ibm.streamsx.topology.messaging.mqtt.MqttStreams) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) Test(org.junit.Test)

Example 9 with MqttStreams

use of com.ibm.streamsx.topology.messaging.mqtt.MqttStreams in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testConfigParamsSubmissionParam.

@Test
public void testConfigParamsSubmissionParam() throws Exception {
    checkAssumes();
    setupDebug();
    Topology top = new Topology("testConfigParamsSubmissionParam");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String subClientId = newSubClientId(top.getName());
    String pubClientId = newPubClientId(top.getName());
    String topicVal = getMqttTopics()[0];
    Supplier<String> topic = new Value<String>(topicVal);
    List<Message> msgs = createMsgs(mgen, null);
    // Test more config properties to be sure we don't blow up
    Supplier<String> userID = top.createSubmissionParameter("userID", String.class);
    Supplier<String> password = top.createSubmissionParameter("password", String.class);
    Supplier<String> trustStore = top.createSubmissionParameter("trustStore", String.class);
    Supplier<String> trustStorePassword = top.createSubmissionParameter("trustStorePassword", String.class);
    Supplier<String> keyStore = top.createSubmissionParameter("keyStore", String.class);
    Supplier<String> keyStorePassword = top.createSubmissionParameter("keyStorePassword", String.class);
    Map<String, Object> producerConfig = createConfig(pubClientId);
    producerConfig.put("userID", userID);
    producerConfig.put("password", password);
    producerConfig.put("trustStore", trustStore);
    producerConfig.put("trustStorePassword", trustStorePassword);
    producerConfig.put("keyStore", keyStore);
    producerConfig.put("keyStorePassword", keyStorePassword);
    Map<String, Object> consumerConfig = createConfig(subClientId);
    consumerConfig.put("userID", userID);
    consumerConfig.put("password", password);
    consumerConfig.put("trustStore", trustStore);
    consumerConfig.put("trustStorePassword", trustStorePassword);
    consumerConfig.put("keyStore", keyStore);
    consumerConfig.put("keyStorePassword", keyStorePassword);
    MqttStreams producer = new MqttStreams(top, producerConfig);
    MqttStreams consumer = new MqttStreams(top, consumerConfig);
    TStream<Message> msgsToPublish = top.constants(msgs).modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    TSink sink = producer.publish(msgsToPublish, topic);
    TStream<Message> rcvdMsgs = consumer.subscribe(topic);
    // for validation...
    rcvdMsgs.print();
    // bundle construction fails for unrecognized or incorrectly typed SPL op params
    File actBundle = (File) StreamsContextFactory.getStreamsContext(StreamsContext.Type.BUNDLE).submit(top, getConfig()).get(15, TimeUnit.SECONDS);
    System.out.println("bundle " + actBundle.getAbsolutePath());
    assertTrue(actBundle != null);
    actBundle.delete();
    assertTrue(sink != null);
}
Also used : TSink(com.ibm.streamsx.topology.TSink) MqttStreams(com.ibm.streamsx.topology.messaging.mqtt.MqttStreams) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Value(com.ibm.streamsx.topology.logic.Value) JSONObject(com.ibm.json.java.JSONObject) File(java.io.File) Test(org.junit.Test)

Example 10 with MqttStreams

use of com.ibm.streamsx.topology.messaging.mqtt.MqttStreams in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testSubscribeNullTopic.

@Test(expected = IllegalArgumentException.class)
public void testSubscribeNullTopic() throws Exception {
    Topology top = new Topology("testSubscribeNullTopic");
    String subClientId = newSubClientId(top.getName());
    MqttStreams mqtt = new MqttStreams(top, createConfig(subClientId));
    // throws IAE
    mqtt.subscribe(null);
}
Also used : MqttStreams(com.ibm.streamsx.topology.messaging.mqtt.MqttStreams) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Aggregations

Topology (com.ibm.streamsx.topology.Topology)13 MqttStreams (com.ibm.streamsx.topology.messaging.mqtt.MqttStreams)13 TestTopology (com.ibm.streamsx.topology.test.TestTopology)12 Message (com.ibm.streamsx.topology.tuple.Message)12 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)12 Test (org.junit.Test)12 TSink (com.ibm.streamsx.topology.TSink)11 Value (com.ibm.streamsx.topology.logic.Value)7 ArrayList (java.util.ArrayList)4 JSONObject (com.ibm.json.java.JSONObject)3 File (java.io.File)2 HashMap (java.util.HashMap)2