blueflood-elasticsearch/src/main/java/com/rackspacecloud/blueflood/io/ElasticsearchRestHelper.java
public void index(final String urlFormat, final String bulkString) throws IOException {
        String tempUrl = String.format(urlFormat, getNextBaseUrl());
        HttpEntity entity = new NStringEntity(bulkString, ContentType.APPLICATION_JSON);
        int statusCode = 0;

        /*
        Here I am using Queue to keep a round-robin selection of next base url. If current base URL fails for
        whatever reason (with response == null), then in catch block, I am enqueueing the next base URL so that
        in next iteration call picks up the new URL. If URL works, queue will be empty and loop will break out.
        */
        Queue<String> callQ = new LinkedList<>();
        callQ.add(tempUrl);
        int callCount = 0;
        while(!callQ.isEmpty() && callCount < MAX_CALL_COUNT) {
            callCount++;
            String url = callQ.remove();
            logger.debug("Using url [{}]", url);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeaders(getHeaders());
            httpPost.setEntity(entity);

            CloseableHttpResponse response = null;

            try {
                logger.debug("ElasticsearchRestHelper.index Thread name in use: [{}]", Thread.currentThread().getName());
                response = closeableHttpClient.execute(httpPost);

                statusCode = response.getStatusLine().getStatusCode();
                String str = EntityUtils.toString(response.getEntity());
                EntityUtils.consume(response.getEntity());

                if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
                    logger.error("index method failed with status code: {} and error: {}", statusCode, str);
                }
            }
            catch (Exception e) {
                if(response == null){
                    logger.error("index method failed with message: {}", e.getMessage());
                    url = String.format(urlFormat, getNextBaseUrl());
                    callQ.add(url);
                }
                else {
                    logger.error("index method failed with status code: {} and exception message: {}",
                            statusCode, e.getMessage());
                }
            } finally {
                if(response != null) {
                    response.close();
                }
            }
        }

        if(statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED)
            throw new IOException("Elasticsearch indexing failed with status code: [" + statusCode + "]");
    }
public void index(final String urlFormat, final String bulkString) throws IOException {
        String tempUrl = String.format(urlFormat, getNextBaseUrl());
        HttpEntity entity = new NStringEntity(bulkString, ContentType.APPLICATION_JSON);
        int statusCode = 0;

        /*
        Here I am using Queue to keep a round-robin selection of next base url. If current base URL fails for
        whatever reason (with response == null), then in catch block, I am enqueueing the next base URL so that
        in next iteration call picks up the new URL. If URL works, queue will be empty and loop will break out.
        */
        Queue<String> callQ = new LinkedList<>();
        callQ.add(tempUrl);
        int callCount = 0;
        while(!callQ.isEmpty() && callCount < MAX_CALL_COUNT) {
            callCount++;
            String url = callQ.remove();
            logger.debug("Using url [{}]", url);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeaders(getHeaders());
            httpPost.setEntity(entity);

            CloseableHttpResponse response = null;

            try {
                logger.debug("ElasticsearchRestHelper.index Thread name in use: [{}]", Thread.currentThread().getName());
                response = closeableHttpClient.execute(httpPost);

                statusCode = response.getStatusLine().getStatusCode();
                String str = EntityUtils.toString(response.getEntity());
                EntityUtils.consume(response.getEntity());

                if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
                    logger.error("index method failed with status code: {} and error: {}", statusCode, str);
                }
            }
            catch (Exception e) {
                if(response == null){
                    logger.error("index method failed with message: {}", e.getMessage());
                    url = String.format(urlFormat, getNextBaseUrl());
                    callQ.add(url);
                }
                else {
                    logger.error("index method failed with status code: {} and exception message: {}",
                            statusCode, e.getMessage());
                }
            } finally {
                if(response != null) {
                    response.close();
                }
            }
        }

        if(statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED)
            throw new IOException("Elasticsearch indexing failed with status code: [" + statusCode + "]");
    }
public void index ( final String urlFormat , final String bulkString ) throws IOException { String tempUrl = String . format ( urlFormat , getNextBaseUrl ( ) ) ; HttpEntity entity = new NStringEntity ( bulkString , ContentType . APPLICATION_JSON ) ; int statusCode = 0 ; /*
        Here I am using Queue to keep a round-robin selection of next base url. If current base URL fails for
        whatever reason (with response == null), then in catch block, I am enqueueing the next base URL so that
        in next iteration call picks up the new URL. If URL works, queue will be empty and loop will break out.
        */ Queue < String > callQ = new LinkedList <> ( ) ; callQ . add ( tempUrl ) ; int callCount = 0 ; while ( ! callQ . isEmpty ( ) && callCount < MAX_CALL_COUNT ) { callCount ++ ; String url = callQ . remove ( ) ; logger . debug ( "Using url [{}]" , url ) ; HttpPost httpPost = new HttpPost ( url ) ; httpPost . setHeaders ( getHeaders ( ) ) ; httpPost . setEntity ( entity ) ; CloseableHttpResponse response = null ; try { logger . debug ( "ElasticsearchRestHelper.index Thread name in use: [{}]" , Thread . currentThread ( ) . getName ( ) ) ; response = closeableHttpClient . execute ( httpPost ) ; statusCode = response . getStatusLine ( ) . getStatusCode ( ) ; String str = EntityUtils . toString ( response . getEntity ( ) ) ; EntityUtils . consume ( response . getEntity ( ) ) ; if ( statusCode != HttpStatus . SC_OK && statusCode != HttpStatus . SC_CREATED ) { logger . error ( "index method failed with status code: {} and error: {}" , statusCode , str ) ; } } catch ( Exception e ) { if ( response == null ) { logger . error ( "index method failed with message: {}" , e . getMessage ( ) ) ; url = String . format ( urlFormat , getNextBaseUrl ( ) ) ; callQ . add ( url ) ; } else { logger . error ( "index method failed with status code: {} and exception message: {}" , statusCode , e . getMessage ( ) ) ; } } finally { if ( response != null ) { response . close ( ) ; } } } if ( statusCode != HttpStatus . SC_OK && statusCode != HttpStatus . SC_CREATED ) throw new IOException ( "Elasticsearch indexing failed with status code: [" + statusCode + "]" ) ; }
If index() fails for whatever reason, it always throws IOException because indexing failed for Elasticsearch.
If index () fails for whatever reason it always throws IOException because indexing failed for Elasticsearch .