Search in sources :

Example 1 with StreamImpl

use of com.ibm.streamsx.topology.internal.core.StreamImpl in project streamsx.topology by IBMStreams.

the class Topology method subscribe.

/**
     * Declare a stream that is a subscription to {@code topic}.
     * A topic is published using {@link TStream#publish(String)}.
     * Subscribers are matched to published streams when the {@code topic}
     * is an exact match and the type of the stream ({@code T},
     * {@code tupleTypeClass}) is an exact match.
     * <BR>
     * Publish-subscribe is a many to many relationship,
     * multiple streams from multiple applications may
     * be published on the same topic and type. Multiple
     * subscribers may subscribe to a topic and type.
     * <BR>
     * A subscription will match all publishers using the
     * same topic and tuple type. Tuples on the published
     * streams will appear on the returned stream, as
     * a single stream.
     * <BR>
     * The subscription is dynamic, the returned stream
     * will subscribe to a matching stream published by
     * a newly submitted application (a job), and stops a
     * subscription when an running job is cancelled.
     * <P>
     * Publish-subscribe only works when the topology is
     * submitted to a {@link com.ibm.streamsx.topology.context.StreamsContext.Type#DISTRIBUTED}
     * context. This allows different applications (or
     * even within the same application) to communicate
     * using published streams.
     * </P>
     * <P>
     * If {@code tupleTypeClass} is {@code JSONObject.class} then the
     * subscription is the generic IBM Streams schema for JSON
     * ({@link JSONSchemas#JSON}). Streams of type {@code JSONObject}
     * are always published and subscribed using the generic schema
     * to allow interchange between applications implemented in
     * different languages.
     * </P>
     * @param topic Topic to subscribe to.
     * @param tupleTypeClass Type to subscribe to.
     * @return Stream the will contain tuples from matching publishers.
     * 
     * @see TStream#publish(String)
     * @see SPLStreams#subscribe(TopologyElement, String, com.ibm.streams.operator.StreamSchema)
     */
public <T> TStream<T> subscribe(String topic, Class<T> tupleTypeClass) {
    checkTopicFilter(topic);
    if (JSONObject.class.equals(tupleTypeClass)) {
        @SuppressWarnings("unchecked") TStream<T> json = (TStream<T>) SPLStreams.subscribe(this, topic, JSONSchemas.JSON).toJSON();
        return json;
    }
    StreamSchema mappingSchema = Schemas.getSPLMappingSchema(tupleTypeClass);
    SPLStream splImport;
    // Subscribed as an SPL Stream.
    if (Schemas.usesDirectSchema(tupleTypeClass)) {
        splImport = SPLStreams.subscribe(this, topic, mappingSchema);
    } else {
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("class", tupleTypeClass.getName());
        params.put("streamType", mappingSchema);
        splImport = SPL.invokeSource(this, "com.ibm.streamsx.topology.topic::SubscribeJava", params, mappingSchema);
    }
    return new StreamImpl<T>(this, splImport.output(), tupleTypeClass);
}
Also used : HashMap(java.util.HashMap) StreamImpl(com.ibm.streamsx.topology.internal.core.StreamImpl) JSONObject(com.ibm.json.java.JSONObject) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Aggregations

JSONObject (com.ibm.json.java.JSONObject)1 StreamSchema (com.ibm.streams.operator.StreamSchema)1 StreamImpl (com.ibm.streamsx.topology.internal.core.StreamImpl)1 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)1 HashMap (java.util.HashMap)1