001/*
002# Licensed Materials - Property of IBM
003# Copyright IBM Corp. 2015  
004 */
005package topic;
006
007import java.util.concurrent.TimeUnit;
008
009import com.ibm.streamsx.topology.Topology;
010import com.ibm.streamsx.topology.context.StreamsContextFactory;
011import com.ibm.streamsx.topology.streams.BeaconStreams;
012
013/**
014 * Publishes a beacon stream on a topic.
015 * A published stream can be subscribed to by
016 * other applications. This allows multiple
017 * applications to consume tuples from an
018 * application. For example an application
019 * that ingests & prepares data can be consumed
020 * by multiple analytic applications.
021 * <BR>
022 * Multiple applications (or multiple streams within applications)
023 * can publish to the same topic.
024 * <BR>
025 * Applications dynamically subscribe
026 * to a published topic, so that new applications
027 * that subscribe to a topic can be submitted at
028 * any time.
029 * 
030 * @see SubscribeBeacon Sample application that
031 * subscribes to the topic published by this application
032 * @see <a href="../../../spldoc/html/tk$com.ibm.streamsx.topology/ns$com.ibm.streamsx.topology.topic$1.html">Integration with SPL applications</a>
033 */
034public class PublishBeacon {
035
036    /**
037     * Submit this application which publishes a stream.
038     * @param args Command line arguments, accepts a single optional topic name.
039     * @throws Exception Error running the application
040     */
041    public static void main(String[] args) throws Exception {
042        
043        // Select the topic name for the command line
044        // using '/beacon' if not supplied.
045        String topic = "/beacon";
046        String type = "DISTRIBUTED";
047
048        if (args.length >= 1)
049            topic = "/" + args[0];
050        if (args.length == 2)
051            type = args[1];
052
053        Topology topology = new Topology("PublishBeacon");
054
055        /*
056         * Publish a throttled beacon stream to a topic
057         */
058        BeaconStreams.beacon(topology).throttle(100, TimeUnit.MILLISECONDS)
059                .publish(topic);
060
061        /*
062         * Publish-Subscribe only works with distributed.
063         */
064        StreamsContextFactory.getStreamsContext(type)
065                .submit(topology);
066    }
067}