Search in sources :

Example 11 with TSink

use of com.ibm.streamsx.topology.TSink in project streamsx.topology by IBMStreams.

the class MqttStreams method publish.

/**
     * Publish {@code stream} tuples to one or more MQTT topics.
     * <p>
     * If {@code topic} is null, each tuple is published to the topic
     * specified by its {@link Message#getTopic()}.
     * Otherwise, all tuples are published to {@code topic}.
     * <p>
     * The messages added to MQTT include a topic and message.
     * The {@link Message#getKey()} field is ignored.
     * <p>
     * The message is handled with the quality of service
     * indicated by configuration property {@code defaultQOS}.
     * 
     * @param stream the stream to publish
     * @param topic topic to publish to.  May be a submission parameter. May be null.
     * @return the sink element
     * @see Value
     * @see Topology#createSubmissionParameter(String, Class)
     */
public TSink publish(TStream<? extends Message> stream, Supplier<String> topic) {
    stream = stream.lowLatency();
    @SuppressWarnings("unchecked") SPLStream splStream = SPLStreams.convertStream((TStream<Message>) stream, cvtMsgFunc(topic), MqttSchemas.MQTT);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("reconnectionBound", -1);
    params.put("qos", 0);
    params.putAll(Util.configToSplParams(config));
    params.remove("messageQueueSize");
    if (topic == null)
        params.put("topicAttributeName", "topic");
    else
        params.put("topic", topic);
    params.put("dataAttributeName", "message");
    if (++opCnt > 1) {
        // each op requires its own clientID
        String clientId = (String) params.get("clientID");
        if (clientId != null && clientId.length() > 0)
            params.put("clientID", opCnt + "-" + clientId);
    }
    // Use SPL.invoke to avoid adding a compile time dependency
    // to com.ibm.streamsx.messaging since JavaPrimitive.invoke*()
    // lack "kind" based variants.
    String kind = "com.ibm.streamsx.messaging.mqtt::MQTTSink";
    String className = "com.ibm.streamsx.messaging.kafka.MqttSinkOperator";
    TSink sink = SPL.invokeSink(kind, splStream, params);
    SPL.tagOpAsJavaPrimitive(sink.operator(), kind, className);
    return sink;
}
Also used : TSink(com.ibm.streamsx.topology.TSink) SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) HashMap(java.util.HashMap) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 12 with TSink

use of com.ibm.streamsx.topology.TSink 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 13 with TSink

use of com.ibm.streamsx.topology.TSink 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 14 with TSink

use of com.ibm.streamsx.topology.TSink in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testSubtypeExplicitTopicProducer.

@Test
public void testSubtypeExplicitTopicProducer() throws Exception {
    checkAssumes();
    setupDebug();
    Topology top = new Topology("testSubtypeExplicitTopicProducer");
    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<MyMsgSubtype> msgs = new ArrayList<>();
    for (Message m : createMsgs(mgen, null)) {
        msgs.add(new MyMsgSubtype(m.getMessage()));
    }
    List<String> expectedAsString = mapList(msgs, subtypeMsgToJSONStringFunc(topicVal));
    // Test producer that takes a TStream<MyMsgSubtype>
    MqttStreams producer = new MqttStreams(top, createConfig(pubClientId));
    MqttStreams consumer = new MqttStreams(top, createConfig(subClientId));
    TStream<MyMsgSubtype> msgsToPublish = top.constants(msgs).asType(MyMsgSubtype.class).modify(new InitialDelay<MyMsgSubtype>(PUB_DELAY_MSEC));
    TSink sink = producer.publish(msgsToPublish, topic);
    TStream<Message> rcvdMsgs = consumer.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(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) ArrayList(java.util.ArrayList) 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 15 with TSink

use of com.ibm.streamsx.topology.TSink in project streamsx.topology by IBMStreams.

the class MqttStreamsTest method testMultiTopicProducer.

@Test
public void testMultiTopicProducer() throws Exception {
    checkAssumes();
    setupDebug();
    Topology top = new Topology("testMultiTopicProducer");
    MsgGenerator mgen = new MsgGenerator(top.getName());
    String subClientId = newSubClientId(top.getName());
    String pubClientId = newPubClientId(top.getName());
    String[] topics = getMqttTopics();
    String topic1 = topics[0];
    String topic2 = topics[1];
    List<Message> topic1Msgs = createMsgs(mgen, topic1);
    List<Message> topic2Msgs = createMsgs(mgen, topic2);
    List<Message> msgs = new ArrayList<>(topic1Msgs);
    msgs.addAll(topic2Msgs);
    List<String> expectedAsString = mapList(msgs, msgToJSONStringFunc());
    // Test producer that publishes to multiple topics (implies implicit topic)
    MqttStreams producer = new MqttStreams(top, createConfig(pubClientId));
    MqttStreams consumer = new MqttStreams(top, createConfig(subClientId));
    TStream<Message> msgsToPublish = top.constants(msgs).modify(new InitialDelay<Message>(PUB_DELAY_MSEC));
    TSink sink = producer.publish(msgsToPublish);
    TStream<Message> rcvdTopic1Msgs = consumer.subscribe(new Value<String>(topic1));
    TStream<Message> rcvdTopic2Msgs = consumer.subscribe(new Value<String>(topic2));
    // for validation...
    TStream<Message> rcvdMsgs = rcvdTopic1Msgs.union(rcvdTopic2Msgs);
    rcvdMsgs.print();
    // just our msgs
    rcvdMsgs = selectMsgs(rcvdMsgs, mgen.pattern());
    TStream<String> rcvdAsString = rcvdMsgs.transform(msgToJSONStringFunc());
    if (testBuildOnly(top))
        return;
    completeAndValidateUnordered(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) ArrayList(java.util.ArrayList) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Aggregations

TSink (com.ibm.streamsx.topology.TSink)16 Message (com.ibm.streamsx.topology.tuple.Message)13 Topology (com.ibm.streamsx.topology.Topology)12 TestTopology (com.ibm.streamsx.topology.test.TestTopology)12 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)12 Test (org.junit.Test)12 MqttStreams (com.ibm.streamsx.topology.messaging.mqtt.MqttStreams)11 Value (com.ibm.streamsx.topology.logic.Value)6 JSONObject (com.ibm.json.java.JSONObject)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)3 File (java.io.File)2 Observation (com.ibm.streamsx.health.ingest.types.model.Observation)1 SPLWindow (com.ibm.streamsx.topology.spl.SPLWindow)1