Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner design by injecting listener into constructor. #49

Merged
merged 1 commit into from
Dec 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class HystrixMetricsPoller {
private final int delay;
private final AtomicBoolean running = new AtomicBoolean(false);
private volatile ScheduledFuture<?> scheduledTask = null;
private final MetricsAsJsonPollerListener listener;

/**
* Allocate resources to begin polling.
Expand All @@ -66,17 +67,20 @@ public class HystrixMetricsPoller {
* <p>
* Use <code>pause</code> to temporarily stop polling that can be restarted again with <code>start</code>.
*
* @param MetricsAsJsonPollerListener
* for callbacks
* @param delay
*/
public HystrixMetricsPoller(int delay) {
public HystrixMetricsPoller(MetricsAsJsonPollerListener listener, int delay) {
this.listener = listener;
executor = new ScheduledThreadPoolExecutor(1, new MetricsPollerThreadFactory());
this.delay = delay;
}

/**
* Start polling.
*/
public synchronized void start(MetricsAsJsonPollerListener listener) {
public synchronized void start() {
// use compareAndSet to make sure it starts only once and when not running
if (running.compareAndSet(false, true)) {
logger.info("Starting HystrixMetricsPoller");
Expand Down Expand Up @@ -304,9 +308,18 @@ public static class UnitTest {

@Test
public void testStartStopStart() {
HystrixMetricsPoller poller = new HystrixMetricsPoller(100);
final AtomicInteger metricsCount = new AtomicInteger();

HystrixMetricsPoller poller = new HystrixMetricsPoller(new MetricsAsJsonPollerListener() {

@Override
public void handleJsonMetric(String json) {
System.out.println("Received: " + json);
metricsCount.incrementAndGet();
}
}, 100);
try {
final AtomicInteger metricsCount = new AtomicInteger();

HystrixCommand<Boolean> test = new HystrixCommand<Boolean>(HystrixCommandGroupKey.Factory.asKey("HystrixMetricsPollerTest")) {

@Override
Expand All @@ -317,14 +330,7 @@ protected Boolean run() {
};
test.execute();

poller.start(new MetricsAsJsonPollerListener() {

@Override
public void handleJsonMetric(String json) {
System.out.println("Received: " + json);
metricsCount.incrementAndGet();
}
});
poller.start();

try {
Thread.sleep(500);
Expand All @@ -349,14 +355,7 @@ public void handleJsonMetric(String json) {
// they should be the same since we were paused
assertTrue(v2 == v1);

poller.start(new MetricsAsJsonPollerListener() {

@Override
public void handleJsonMetric(String json) {
System.out.println("Received: " + json);
metricsCount.incrementAndGet();
}
});
poller.start();

try {
Thread.sleep(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ private void handleRequest(HttpServletRequest request, HttpServletResponse respo
response.setHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
response.setHeader("Pragma", "no-cache");

poller = new HystrixMetricsPoller(delay);
MetricJsonListener jsonListener = new MetricJsonListener();
poller = new HystrixMetricsPoller(jsonListener, delay);
// start polling and it will write directly to the output stream
poller.start(jsonListener);
poller.start();
logger.info("Starting poller");

// we will use a "single-writer" approach where the Servlet thread does all the writing
Expand Down