@Override public <T> Future<T> submit(Runnable task, T result) { submitted.add(1); return super.submit(task, result); }
/** * Returns the number of occurrences per second. * * @return throughput in occurrences per second */ public synchronized double throughput() { return total / duration(); }
private void periodicallyPrintStats() { long now = System.currentTimeMillis(); long prev = lastPrinted.get(); if (now - prev > STATS_INTERVAL) { if (lastPrinted.compareAndSet(prev, now)) { log.debug("queue size: {} jobs, submitted: {} jobs/s, taken: {} jobs/s", getQueue().size(), submitted.throughput(), taken.throughput()); submitted.reset(); taken.reset(); } } }
@Override public <T> Future<T> submit(Callable<T> task) { Counter taskCounter = new Counter(); taskCounter.reset(); return executor.submit(() -> { T t = null; long queueWaitTime = (long) taskCounter.duration(); Class className; if (task instanceof CallableExtended) { className = ((CallableExtended) task).getRunnable().getClass(); } else { className = task.getClass(); } if (queueMetrics != null) { queueMetrics.update(queueWaitTime, TimeUnit.SECONDS); } taskCounter.reset(); try { t = task.call(); } catch (Exception e) { getLogger(className).error("Uncaught exception on " + className, e); } long taskwaittime = (long) taskCounter.duration(); if (delayMetrics != null) { delayMetrics.update(taskwaittime, TimeUnit.SECONDS); } return t; }); }
/** * Closes the message buffer. */ public void close() { synchronized (this) { if (closed) { return; } closed = true; } bytesIn.freeze(); bytesOut.freeze(); messagesIn.freeze(); messagesOut.freeze(); loop.removeStream(this); if (key != null) { try { key.cancel(); key.channel().close(); } catch (IOException e) { log.warn("Unable to close stream", e); } } }
@Override public <T> Future<T> submit(Callable<T> task) { submitted.add(1); return super.submit(task); }
@Override public Future<?> submit(Runnable task) { submitted.add(1); return super.submit(task); }
@Override public void execute(Runnable command) { submitted.add(1); super.execute(command); }
/** * Reads, without blocking, a list of messages from the stream. * The list will be empty if there were not messages pending. * * @return list of messages or null if backing channel has been closed * @throws IOException if messages could not be read */ public List<M> read() throws IOException { try { int read = channel.read(inbound); if (read != -1) { // Read the messages one-by-one and add them to the list. List<M> messages = new ArrayList<>(); M message; inbound.flip(); while ((message = read(inbound)) != null) { messages.add(message); messagesIn.add(1); bytesIn.add(message.length()); } inbound.compact(); // Mark the stream with current time to indicate liveness. lastActiveTime = currentTimeMillis(); return messages; } return null; } catch (Exception e) { throw new IOException("Unable to read messages", e); } }
@Override protected void beforeExecute(Thread t, Runnable r) { super.beforeExecute(t, r); taken.add(1); periodicallyPrintStats(); updateLoad(); }