/** * Jet repeatedly calls this method whenever it wants more data from our * source. The source limits the rate of HTTP requests by first checking * that enough time has elapsed since the previous request. Then it * makes the request and emits each line of the request as a pair {@code * (timestamp, usedMemory)}. */ void fillBuffer(TimestampedSourceBuffer<TimestampedItem<Long>> buf) throws IOException { if (!readyToPoll()) { return; } try (Stream<String> lines = new BufferedReader(new InputStreamReader( httpc.execute(new HttpGet("http://localhost:8008")) .getEntity().getContent())) .lines() ) { lines.forEach(line -> { int splitPoint = line.indexOf(' '); long timestamp = Long.valueOf(line.substring(0, splitPoint)); long value = Long.valueOf(line.substring(splitPoint + 1)); buf.add(new TimestampedItem<>(timestamp, value), timestamp); }); } }
private void fillBuffer(TimestampedSourceBuffer<Aircraft> buffer) throws IOException { long now = System.currentTimeMillis(); if (now < (lastPoll + pollIntervalMillis)) { return; } lastPoll = now; JsonArray aircraftList = pollForAircraft(); aircraftList.values().stream() .map(FlightDataSource::parseAircraft) .filter(a -> !isNullOrEmpty(a.getReg())) // there should be a reg number // only add new positions to buffer .filter(a -> a.getPosTime() > aircraftLastSeenAt.getOrDefault(a.getId(), 0L)) .forEach(a -> { // update cache aircraftLastSeenAt.put(a.getId(), a.getPosTime()); buffer.add(a, a.getPosTime()); }); logger.info("Polled " + aircraftList.size() + " aircraft, " + buffer.size() + " new positions."); }
void addToBuffer(TimestampedSourceBuffer<String> sourceBuffer) { // drain everything at once for optimal performance and also naturally limit batch size queue.drainTo(buffer); for (String tweet : buffer) { JSONObject object = new JSONObject(tweet); if (object.has("text") && object.has("timestamp_ms")) { String text = object.getString("text"); long timestamp = object.getLong("timestamp_ms"); sourceBuffer.add(text, timestamp); } } buffer.clear(); }
public void addToBufferFn(TimestampedSourceBuffer<BufferedImage> buffer) { long now = System.currentTimeMillis(); if (now < (lastPoll + pollIntervalMillis)) { return; } lastPoll = now; BufferedImage image = webcam.getImage(); gui.setImageRepaint(image); buffer.add(image); }
/** * Adds an item to the buffer, assigning {@code System.currentTimeMillis()} * to it as the timestamp. */ @Override default void add(@Nonnull T item) { add(item, System.currentTimeMillis()); } }