Search in sources :

Example 1 with HystrixStream

use of com.netflix.hystrix.contrib.metrics.HystrixStream in project Hystrix by Netflix.

the class StreamingOutputProviderTest method testInfiniteOnNextStream.

@Test
public void testInfiniteOnNextStream() throws Exception {
    final PipedInputStream is = new PipedInputStream();
    final PipedOutputStream os = new PipedOutputStream(is);
    final AtomicInteger writes = new AtomicInteger(0);
    final HystrixStream stream = new HystrixStream(streamOfOnNexts, 100, new AtomicInteger(1));
    Thread streamingThread = startStreamingThread(stream, os);
    verifyStream(is, writes);
    // Let the provider stream for some time.
    Thread.sleep(1000);
    // Stop streaming
    streamingThread.interrupt();
    os.close();
    is.close();
    System.out.println("Total lines:" + writes.get());
    // Observable is configured to emit events in every 100 ms. So expect at least 9 in a second.
    assertTrue(writes.get() >= 9);
    // Provider is expected to decrement connection count when streaming process is terminated.
    assertTrue(stream.getConcurrentConnections().get() == 0);
}
Also used : HystrixStream(com.netflix.hystrix.contrib.metrics.HystrixStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Test(org.junit.Test)

Example 2 with HystrixStream

use of com.netflix.hystrix.contrib.metrics.HystrixStream in project Hystrix by Netflix.

the class StreamingOutputProviderTest method testStreamOnce.

private void testStreamOnce(Observable<String> observable) throws Exception {
    final PipedInputStream is = new PipedInputStream();
    final PipedOutputStream os = new PipedOutputStream(is);
    final AtomicInteger writes = new AtomicInteger(0);
    final HystrixStream stream = new HystrixStream(observable, 100, new AtomicInteger(1));
    startStreamingThread(stream, os);
    verifyStream(is, writes);
    Thread.sleep(1000);
    os.close();
    is.close();
    System.out.println("Total lines:" + writes.get());
    assertTrue(writes.get() == 1);
    assertTrue(stream.getConcurrentConnections().get() == 0);
}
Also used : HystrixStream(com.netflix.hystrix.contrib.metrics.HystrixStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 3 with HystrixStream

use of com.netflix.hystrix.contrib.metrics.HystrixStream in project Hystrix by Netflix.

the class AbstractHystrixStreamController method handleRequest.

/**
	 * Maintain an open connection with the client. On initial connection send latest data of each requested event type and subsequently send all changes for each requested event type.
	 * 
	 * @return JAX-RS Response - Serialization will be handled by {@link HystrixStreamingOutputProvider}
	 */
protected Response handleRequest() {
    ResponseBuilder builder = null;
    /* ensure we aren't allowing more connections than we want */
    int numberConnections = getCurrentConnections().get();
    // may change at runtime, so look this up for each request
    int maxNumberConnectionsAllowed = getMaxNumberConcurrentConnectionsAllowed();
    if (numberConnections >= maxNumberConnectionsAllowed) {
        builder = Response.status(Status.SERVICE_UNAVAILABLE).entity("MaxConcurrentConnections reached: " + maxNumberConnectionsAllowed);
    } else {
        /* initialize response */
        builder = Response.status(Status.OK);
        builder.header(HttpHeaders.CONTENT_TYPE, "text/event-stream;charset=UTF-8");
        builder.header(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
        builder.header("Pragma", "no-cache");
        getCurrentConnections().incrementAndGet();
        builder.entity(new HystrixStream(sampleStream, pausePollerThreadDelayInMs, getCurrentConnections()));
    }
    return builder.build();
}
Also used : HystrixStream(com.netflix.hystrix.contrib.metrics.HystrixStream) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

HystrixStream (com.netflix.hystrix.contrib.metrics.HystrixStream)3 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Test (org.junit.Test)1