Package flex.samples.feed

Source Code of flex.samples.feed.Feed$FeedThread

package flex.samples.feed;

import java.util.*;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class Feed {
  private static FeedThread thread;

  public Feed() {
  }

  public void start() {
    if (thread == null) {
      thread = new FeedThread();
      thread.start();
    }
  }

  public void stop() {
    thread.running = false;
    thread = null;
  }

  public static class FeedThread extends Thread {

    public boolean running = true;

    public void run() {
      MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
      String clientID = UUIDUtils.createUUID();

      Random random = new Random();
      double initialValue = 35;
      double currentValue = 35;
      double maxChange = initialValue * 0.005;

      while (running) {
        double change = maxChange - random.nextDouble() * maxChange * 2;
        double newValue = currentValue + change;

        if (currentValue < initialValue + initialValue * 0.15
            && currentValue > initialValue - initialValue * 0.15) {
          currentValue = newValue;
        } else {
          currentValue -= change;
        }

        AsyncMessage msg = new AsyncMessage();
        msg.setDestination("feed");
        msg.setClientId(clientID);
        msg.setMessageId(UUIDUtils.createUUID());
        msg.setTimestamp(System.currentTimeMillis());
        msg.setBody(new Double(currentValue));
        msgBroker.routeMessageToService(msg, null);

        System.out.println("" + currentValue);

        try {
          Thread.sleep(300);
        } catch (InterruptedException e) {
        }

      }
    }
  }

}
TOP

Related Classes of flex.samples.feed.Feed$FeedThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.