use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project cdap by caskdata.
the class NettyRouterTestBase method testRouterAsync.
@Test
public void testRouterAsync() throws Exception {
int numElements = 123;
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
final CountDownLatch latch = new CountDownLatch(numElements);
final AtomicInteger numSuccessfulRequests = new AtomicInteger(0);
for (int i = 0; i < numElements; ++i) {
final int elem = i;
final Request request = new RequestBuilder("GET").setUrl(resolveURI(DEFAULT_SERVICE, String.format("%s/%s-%d", "/v1/echo", "async", i))).build();
asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
latch.countDown();
Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
String responseBody = response.getResponseBody();
LOG.trace("Got response {}", responseBody);
Assert.assertEquals("async-" + elem, responseBody);
numSuccessfulRequests.incrementAndGet();
return null;
}
@Override
public void onThrowable(Throwable t) {
LOG.error("Got exception while posting {}", elem, t);
latch.countDown();
}
});
// Sleep so as not to overrun the server.
TimeUnit.MILLISECONDS.sleep(1);
}
latch.await();
asyncHttpClient.close();
Assert.assertEquals(numElements, numSuccessfulRequests.get());
// we use sticky endpoint strategy so the sum of requests from the two gateways should be NUM_ELEMENTS
Assert.assertTrue(numElements == (defaultServer1.getNumRequests() + defaultServer2.getNumRequests()));
}
use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project cdap by caskdata.
the class NettyRouterTestBase method testUpload.
@Test
public void testUpload() throws Exception {
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
byte[] requestBody = generatePostData();
final Request request = new RequestBuilder("POST").setUrl(resolveURI(DEFAULT_SERVICE, "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
return null;
}
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
// TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
content.writeTo(byteArrayOutputStream);
return super.onBodyPartReceived(content);
}
});
future.get();
Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project Singularity by HubSpot.
the class SingularityExecutorModule method providesHttpClient.
@Provides
@Singleton
@Named(LOCAL_DOWNLOAD_HTTP_CLIENT)
public AsyncHttpClient providesHttpClient(SingularityExecutorConfiguration configuration) {
AsyncHttpClientConfig.Builder configBldr = new AsyncHttpClientConfig.Builder();
configBldr.setRequestTimeoutInMs((int) configuration.getLocalDownloadServiceTimeoutMillis());
configBldr.setIdleConnectionTimeoutInMs((int) configuration.getLocalDownloadServiceTimeoutMillis());
configBldr.addRequestFilter(new ThrottleRequestFilter(configuration.getLocalDownloadServiceMaxConnections()));
return new AsyncHttpClient(configBldr.build());
}
use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project OpenTripPlanner by opentripplanner.
the class WebsocketGtfsRealtimeUpdater method run.
@Override
public void run() throws InterruptedException {
while (true) {
AsyncHttpClient client = new AsyncHttpClient();
WebSocketListener listener = new Listener();
WebSocketUpgradeHandler handler = new WebSocketUpgradeHandler.Builder().addWebSocketListener(listener).build();
WebSocket socket = null;
boolean connectionSuccessful = true;
// Try to create a websocket connection
try {
socket = client.prepareGet(url).execute(handler).get();
LOG.info("Successfully connected to {}.", url);
} catch (ExecutionException e) {
LOG.error("Could not connect to {}: {}", url, e.getCause().getMessage());
connectionSuccessful = false;
} catch (Exception e) {
LOG.error("Unknown exception when trying to connect to {}:", url, e);
connectionSuccessful = false;
}
// If connection was unsuccessful, wait some time before trying again
if (!connectionSuccessful) {
Thread.sleep(reconnectPeriodSec * 1000);
}
// Keep checking whether connection is still open
while (true) {
if (socket == null || !socket.isOpen()) {
// The connection is closed somehow, try to reconnect
if (connectionSuccessful) {
LOG.warn("Connection to {} was lost. Trying to reconnect...", url);
}
break;
}
Thread.sleep(CHECK_CONNECTION_PERIOD_SEC * 1000);
}
client.close();
}
}
use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project rxrabbit by meltwater.
the class RxRabbitMultiNodeTest method setup.
@Before
public void setup() throws Exception {
dockerContainers.resetAll(false);
dockerContainers.rabbit(RABBIT_1).assertUp();
dockerContainers.rabbit(RABBIT_2).assertUp();
String rabbitTcpPort = dockerContainers.rabbit(RABBIT_1).tcpPort();
rabbitAdminPort = dockerContainers.rabbit(RABBIT_1).adminPort();
String rabbit2TcpPort = dockerContainers.rabbit(RABBIT_2).tcpPort();
log.infoWithParams("****** Rabbit brokers are up and running *****");
BrokerAddresses addresses = new BrokerAddresses("amqp://localhost:" + rabbitTcpPort + "," + "amqp://localhost:" + rabbit2TcpPort);
channelFactory = new DefaultChannelFactory(addresses, connectionSettings);
consumerFactory = new DefaultConsumerFactory(channelFactory, consumeSettings);
DefaultPublisherFactory publisherFactory = new DefaultPublisherFactory(channelFactory, publishSettings);
httpClient = new AsyncHttpClient();
messagesSeen.clear();
createQueues(channelFactory, inputQueue, new Exchange(inputExchange));
publisher = publisherFactory.createPublisher();
}
Aggregations