Пример #1
0
  /** Entry point for a streaming Fizz Buzz! */
  public static void main(String[] args) throws Exception {
    Topology topology = new Topology();

    // Declare an infinite stream of Long values
    TStream<Long> counting = BeaconStreams.longBeacon(topology);

    // Throttle the rate to allow the output to be seen easier
    counting = counting.throttle(100, TimeUnit.MILLISECONDS);

    // Print the tuples to standard output
    playFizzBuzz(counting).print();

    // At this point the streaming topology (streaming) is
    // declared, but no data is flowing. The topology
    // must be submitted to a StreamsContext to be executed.

    // Since this is an streaming graph with an endless
    // data source it will run for ever
    Future<?> runningTopology = StreamsContextFactory.getEmbedded().submit(topology);

    // Run for one minute before canceling.
    Thread.sleep(TimeUnit.MINUTES.toMillis(1));

    runningTopology.cancel(true);
  }
Пример #2
0
  /**
   * 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);
    }
  }