com.netflix.niws.client.http.RestClient Java Examples

The following examples show how to use com.netflix.niws.client.http.RestClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NamedConnectionPoolTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectionPoolCleaner() throws Exception {
    // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
    ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, "100");
    ConfigurationManager.getConfigInstance().setProperty("ConnectionPoolCleanerTest.ribbon." + CommonClientConfigKey.ConnectionCleanerRepeatInterval, "500");
    RestClient client = (RestClient) ClientFactory.getNamedClient("ConnectionPoolCleanerTest");
    NFHttpClient httpclient = NFHttpClientFactory.getNamedNFHttpClient("ConnectionPoolCleanerTest");
    assertNotNull(httpclient);
    com.netflix.client.http.HttpResponse response = null;
    try {
        response = client.execute(HttpRequest.newBuilder().uri(server.getServerPath("/")).build());
    } finally {
        if (response != null) {
            response.close();
        }
    }
    MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) httpclient.getConnectionManager();
    Thread.sleep(2000);
    assertEquals(0, connectionPoolManager.getConnectionsInPool());
    client.shutdown();
}
 
Example #2
Source File: AddRSSCommand.java    From recipes-rss with Apache License 2.0 6 votes vote down vote up
@Override
protected String run() {
	try {
		/*
		 * The named client param must match the prefix for the ribbon
		 * configuration specified in the edge.properties file
		 */
		RestClient client = (RestClient) ClientFactory.getNamedClient(RSSConstants.MIDDLETIER_REST_CLIENT);

		HttpClientRequest request = HttpClientRequest
				.newBuilder()
				.setVerb(Verb.POST)
				.setUri(new URI("/"
						+ RSSConstants.MIDDLETIER_WEB_RESOURCE_ROOT_PATH
						+ RSSConstants.RSS_ENTRY_POINT
                           + "?url=" + url))
				.build();
		HttpClientResponse response = client.executeWithLoadBalancer(request);

		return IOUtils.toString(response.getRawEntity(), Charsets.UTF_8);
	} catch (Exception exc) {
		throw new RuntimeException("Exception occurred when adding a RSS feed", exc);
	}
}
 
Example #3
Source File: GetRSSCommand.java    From recipes-rss with Apache License 2.0 6 votes vote down vote up
@Override
protected String run() {
	try {
		// The named client param must match the prefix for the ribbon
		// configuration specified in the edge.properties file
		RestClient client = (RestClient) ClientFactory.getNamedClient(RSSConstants.MIDDLETIER_REST_CLIENT);

		HttpClientRequest request = HttpClientRequest
				.newBuilder()
				.setVerb(Verb.GET)
				.setUri(new URI("/"
						+ RSSConstants.MIDDLETIER_WEB_RESOURCE_ROOT_PATH
						+ RSSConstants.RSS_ENTRY_POINT)
                   )
				.build();
		HttpClientResponse response = client.executeWithLoadBalancer(request);

		return IOUtils.toString(response.getRawEntity(), Charsets.UTF_8);
	} catch (Exception exc) {
		throw new RuntimeException("Exception", exc);
	}
}
 
Example #4
Source File: DeleteRSSCommand.java    From recipes-rss with Apache License 2.0 6 votes vote down vote up
@Override
protected String run() {
	try {
		// The named client param must match the prefix for the ribbon
		// configuration specified in the edge.properties file
		RestClient client = (RestClient) ClientFactory.getNamedClient(RSSConstants.MIDDLETIER_REST_CLIENT);

		HttpClientRequest request = HttpClientRequest
				.newBuilder()
				.setVerb(Verb.DELETE)
				.setUri(new URI("/"
                           + RSSConstants.MIDDLETIER_WEB_RESOURCE_ROOT_PATH
                           + RSSConstants.RSS_ENTRY_POINT
                           + "?url=" + url)
                   )
				.build();
		HttpClientResponse response = client.executeWithLoadBalancer(request);

		return IOUtils.toString(response.getRawEntity(), Charsets.UTF_8);
	} catch (Exception exc) {
		throw new RuntimeException("Exception", exc);
	}
}
 
Example #5
Source File: ManyShortLivedRequestsSurvivorTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void survive() throws IOException, ClientException, URISyntaxException, InterruptedException {

    String clientName = "RibbonClientTest-loadBalancingDefaultPolicyRoundRobin";
    String serverListKey = clientName + ".ribbon.listOfServers";
    int nbHitsPerServer = 60;
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();

    for (int i = 0; i < nbHitsPerServer; i++) {
        server1.enqueue(new MockResponse().setResponseCode(200).setBody("server1 success <" + i + ">!"));
        server2.enqueue(new MockResponse().setResponseCode(200).setBody("server2 success <" + i + ">!"));
    }

    server1.play();
    server2.play();

    getConfigInstance().setProperty(serverListKey, hostAndPort(server1.getUrl("")) + "," + hostAndPort(server2.getUrl("")));

    RestClient client = (RestClient) ClientFactory.getNamedClient(clientName);
    HttpRequest request;
    for (int i = 0; i < nbHitsPerServer * 2; i++) {
        request = HttpRequest.newBuilder().uri(new URI("/")).build();
        HttpResponse response = client.executeWithLoadBalancer(request);
        response.close();
    }
}
 
Example #6
Source File: ElasticSearchSink.java    From suro with Apache License 2.0 5 votes vote down vote up
public ElasticSearchSink(
    @JsonProperty("clientName") String clientName,
    @JsonProperty("queue4Sink") MessageQueue4Sink queue4Sink,
    @JsonProperty("batchSize") int batchSize,
    @JsonProperty("batchTimeout") int batchTimeout,
    @JsonProperty("addressList") List<String> addressList,
    @JsonProperty("indexInfo") @JacksonInject IndexInfoBuilder indexInfo,
    @JsonProperty("jobQueueSize") int jobQueueSize,
    @JsonProperty("corePoolSize") int corePoolSize,
    @JsonProperty("maxPoolSize") int maxPoolSize,
    @JsonProperty("jobTimeout") long jobTimeout,
    @JsonProperty("sleepOverClientException") int sleepOverClientException,
    @JsonProperty("ribbon.etc") Properties ribbonEtc,
    @JsonProperty("reEnqueueOnException") boolean reEnqueueOnException,
    @JacksonInject ObjectMapper jsonMapper,
    @JacksonInject RestClient client) {
    super(jobQueueSize, corePoolSize, maxPoolSize, jobTimeout, clientName);

    this.indexInfo =
        indexInfo == null ? new DefaultIndexInfoBuilder(null, null, null, null, null, jsonMapper) : indexInfo;

    initialize(
        clientName,
        queue4Sink == null ? new MemoryQueue4Sink(10000) : queue4Sink,
        batchSize,
        batchTimeout,
        true);

    this.jsonMapper = jsonMapper;
    this.addressList = addressList;
    this.ribbonEtc = ribbonEtc == null ? new Properties() : ribbonEtc;
    this.clientName = clientName;
    this.client = client;
    this.timer = Servo.getTimer(clientName + "_latency");
    this.sleepOverClientException = sleepOverClientException;
    this.reEnqueueOnException = reEnqueueOnException;
}
 
Example #7
Source File: ElasticSearchSink.java    From suro with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void createClient() {
    if (ribbonEtc.containsKey("eureka")) {
        ribbonEtc.setProperty(
            clientName + ".ribbon.AppName", clientName);
        ribbonEtc.setProperty(
            clientName + ".ribbon.NIWSServerListClassName",
            "com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList");
        String[] host_port = addressList.get(0).split(":");
        ribbonEtc.setProperty(
            clientName + ".ribbon.DeploymentContextBasedVipAddresses",
            host_port[0]);
        ribbonEtc.setProperty(
            clientName + ".ribbon.Port",
            host_port[1]);
    } else {
        ribbonEtc.setProperty(clientName + ".ribbon.listOfServers", Joiner.on(",").join(addressList));
    }
    ribbonEtc.setProperty(
        clientName + ".ribbon.EnablePrimeConnections",
        "true");
    String retryPropertyName = clientName + ".ribbon." + CommonClientConfigKey.OkToRetryOnAllOperations;
    if (ribbonEtc.getProperty(retryPropertyName) == null) {
        // default set this to enable retry on POST operation upon read timeout
        ribbonEtc.setProperty(retryPropertyName, "true");
    }
    String maxRetryProperty = clientName + ".ribbon." + CommonClientConfigKey.MaxAutoRetriesNextServer;
    if (ribbonEtc.getProperty(maxRetryProperty) == null) {
        // by default retry two different servers upon exception
        ribbonEtc.setProperty(maxRetryProperty, "2");
    }
    ConfigurationManager.loadProperties(ribbonEtc);
    client = (RestClient) ClientFactory.getNamedClient(clientName);

}
 
Example #8
Source File: ClientFactoryTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void init() {
	ConfigurationManager.getConfigInstance().setProperty("junit.ribbon.listOfServers", "www.example1.come:80,www.example2.come:80,www.example3.come:80");
	client = (RestClient) ClientFactory.getNamedClient("junit");
}
 
Example #9
Source File: ElasticSearchSink.java    From suro with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
RestClient getClient() {
    return client;
}
 
Example #10
Source File: TestElasticSearchSink.java    From suro with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreate() throws IOException {
    String desc = "    {\n" +
        "        \"type\": \"elasticsearch\",\n" +
        "        \"queue4Sink\":{\"type\": \"memory\", \"capacity\": 0 },\n" +
        "        \"batchSize\": 100,\n" +
        "        \"batchTimeout\": 1000,\n" +
        "        \"clientName\": \"es_test\",\n" +
        "        \"cluster.name\": \"es_test\",\n" +
        "        \"addressList\": [\"http://host1:8080\", \"http://host2:8080\"],\n" +
        "        \"indexInfo\":{\n" +
        "            \"type\": \"default\",\n" +
        "            \"indexTypeMap\":{\"routingkey1\":\"index1:type1\", \"routingkey2\":\"index2:type2\"},\n" +
        "            \"idFields\":{\"index\":[\"f1\", \"f2\"]},\n" +
        "            \"timestamp\": {\"field\":\"ts\"},\n" +
        "            \"indexSuffixFormatter\":{\"type\": \"date\", \"properties\":{\"dateFormat\":\"YYYYMMdd\"}}\n" +
        "        }\n" +
        "    }";
    final ObjectMapper jsonMapper = new DefaultObjectMapper();
    jsonMapper.registerSubtypes(new NamedType(ElasticSearchSink.class, "elasticsearch"));
    jsonMapper.setInjectableValues(new InjectableValues() {
        @Override
        public Object findInjectableValue(
            Object valueId,
            DeserializationContext ctxt,
            BeanProperty forProperty,
            Object beanInstance
        ) {
            if (valueId.equals(ObjectMapper.class.getCanonicalName())) {
                return jsonMapper;
            } else {
                return null;
            }
        }
    });

    Sink sink = jsonMapper.readValue(desc, new TypeReference<Sink>(){});
    assertTrue(sink instanceof ElasticSearchSink);
    ElasticSearchSink esSink = (ElasticSearchSink) sink;
    esSink.createClient();
    RestClient client = esSink.getClient();
    IClientConfig config = ((BaseLoadBalancer) client.getLoadBalancer()).getClientConfig();
    assertTrue(config.get(CommonClientConfigKey.OkToRetryOnAllOperations));
    assertEquals(2, config.get(CommonClientConfigKey.MaxAutoRetriesNextServer).intValue());
    assertEquals(0, esSink.getSleepOverClientException());
    assertFalse(esSink.getReenqueueOnException());
}