use of com.ibm.streamsx.topology.spl.SPLStream 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);
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.
the class PublishSubscribeSPLTest method _testSPLPublishFilteredSubscribe.
private void _testSPLPublishFilteredSubscribe(String topic, boolean allowFilters) throws Exception {
final Topology t = new Topology();
SPL.addToolkit(t, new File(getTestRoot(), "spl/testtk"));
SPLStream source = SPLStreamsTest.testTupleStream(t);
source = addStartupDelay(source);
source.publish(topic, allowFilters);
// Filter on the vi attribute, passing the value 321.
Map<String, Object> params = new HashMap<>();
params.put("topic", topic);
params.put("value", 43675232L);
SPLStream sub = SPL.invokeSource(t, "testspl::Int64SubscribeFilter", params, source.getSchema());
TStream<String> subscribe = sub.transform(new GetTupleId());
completeAndValidate(subscribe, 20, "SPL:1");
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.
the class SimpleEmbeddedTest method testSimple.
@Test
public void testSimple() throws Exception {
Topology topology = new Topology("testSimple");
TStream<String> hw = topology.strings("Hello", "World!", "Test!!");
SPLStream hws = SPLStreams.stringToSPLStream(hw);
Tester tester = topology.getTester();
StreamCounter<Tuple> counter = tester.splHandler(hws, new StreamCounter<Tuple>());
StreamCollector<LinkedList<Tuple>, Tuple> collector = tester.splHandler(hws, StreamCollector.newLinkedListCollector());
StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED_TESTER).submit(topology).get();
assertEquals(3, counter.getTupleCount());
assertEquals("Hello", collector.getTuples().get(0).getString(0));
assertEquals("World!", collector.getTuples().get(1).getString(0));
assertEquals("Test!!", collector.getTuples().get(2).getString(0));
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.
the class PublishSubscribeSPLTest method testSPLPublishNoFilterWithSubscribe.
/**
* Test that with publish allow filter set to false
* a subscriber without a filter gets the full set of data.
*/
@Test
public void testSPLPublishNoFilterWithSubscribe() throws Exception {
final Topology t = new Topology();
SPLStream source = SPLStreamsTest.testTupleStream(t);
source = addStartupDelay(source);
source.publish("testSPLPublishNoFilterSFilteredSubscribe", false);
SPLStream sub = SPLStreams.subscribe(t, "testSPLPublishNoFilterSFilteredSubscribe", source.getSchema());
TStream<String> subscribe = sub.transform(new GetTupleId());
completeAndValidate(subscribe, 20, "SPL:0", "SPL:1", "SPL:2", "SPL:3");
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.
the class PublishSubscribeSPLTest method testSPLPublishAllowFilterWithSubscribe.
/**
* Test that with publish allow filter set to false
* a subscriber without a filter gets the full set of data.
*/
@Test
public void testSPLPublishAllowFilterWithSubscribe() throws Exception {
final Topology t = new Topology();
SPLStream source = SPLStreamsTest.testTupleStream(t);
source = addStartupDelay(source);
source.publish("testSPLPublishAllowFilterWithSubscribe", true);
SPLStream sub = SPLStreams.subscribe(t, "testSPLPublishAllowFilterWithSubscribe", source.getSchema());
TStream<String> subscribe = sub.transform(new GetTupleId());
completeAndValidate(subscribe, 20, "SPL:0", "SPL:1", "SPL:2", "SPL:3");
}
Aggregations