Java 类com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler 实例源码

项目:kazoo-client    文件:KazooConnection.java   
private <U, T extends ResponseEnvelope<U>> U execute(final HttpRequest httpRequest, final Class<T> responseType) throws IOException {
   httpRequest.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()));
   if (authToken != null) {
      HttpHeaders headers = httpRequest.getHeaders();
      headers.set("X-Auth-Token", authToken);
   }

   HttpResponse httpResponse = httpRequest.execute();
   T response = httpResponse.parseAs(responseType);

   // Update authToken, if necessary
   if (response.getAuthToken() != null) {
      authToken = response.getAuthToken();
   }

   return response.getData();
}
项目:bigdata-interop    文件:RetryHttpInitializer.java   
public CredentialOrBackoffResponseHandler() {
  HttpBackOffUnsuccessfulResponseHandler errorCodeHandler =
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff());
  errorCodeHandler.setBackOffRequired(
      new HttpBackOffUnsuccessfulResponseHandler.BackOffRequired() {
        @Override
        public boolean isRequired(HttpResponse response) {
          return BASE_HTTP_BACKOFF_REQUIRED.isRequired(response)
              || response.getStatusCode() == STATUS_CODE_TOO_MANY_REQUESTS;
        }
      });
  if (sleeperOverride != null) {
    errorCodeHandler.setSleeper(sleeperOverride);
  }
  this.delegateHandler = errorCodeHandler;
}
项目:secor    文件:GsUploadManager.java   
private static HttpRequestInitializer setHttpBackoffTimeout(final HttpRequestInitializer requestInitializer,
                                                            final int connectTimeoutMs, final int readTimeoutMs) {
    return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            requestInitializer.initialize(httpRequest);

            // Configure exponential backoff on error
            // https://developers.google.com/api-client-library/java/google-http-java-client/backoff
            ExponentialBackOff backoff = new ExponentialBackOff();
            HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(backoff)
                    .setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ALWAYS);
            httpRequest.setUnsuccessfulResponseHandler(backoffHandler);

            httpRequest.setConnectTimeout(connectTimeoutMs);
            httpRequest.setReadTimeout(readTimeoutMs);
        }
    };
}
项目:elasticsearch_my    文件:GoogleCloudStorageService.java   
DefaultHttpRequestInitializer(GoogleCredential credential, TimeValue connectTimeout, TimeValue readTimeout) {
    this.credential = credential;
    this.connectTimeout = connectTimeout;
    this.readTimeout = readTimeout;
    this.handler = new HttpBackOffUnsuccessfulResponseHandler(newBackOff());
    this.ioHandler = new HttpBackOffIOExceptionHandler(newBackOff());
}
项目:sync    文件:Networker.java   
public static boolean sendGCMSync(String userId, String senderId) {
    if(Constants.GCM_API_KEY == null) {
        log.info("GCM not set up, see readme for how to configure");
        return false;
    }
    try {
        GCMMessage message = new GCMMessage(userId);
        log.info("message:" + message);
        log.info("gson:" + gson.toJson(message));
        GenericData body = new GenericData();
        GenericData data = new GenericData();
        body.put("to", "/topics/" + userId);
        if(senderId != null) {
            data.put("senderId",senderId);
            body.put("data", data);
        }

        //why does this only take a generic map?
        HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(GCM_HOST),
                new JsonHttpContent(gsonFactory, body))
                .setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()))
                .setHeaders(new HttpHeaders().setAuthorization("key=" + Constants.GCM_API_KEY));
        request.getContent().writeTo(System.out);
        HttpResponse response = request.execute();
        log.info("response" + response);
        return true;
    } catch(IOException e) {
        log.info("could not send http message:");
        e.printStackTrace();
        return false;
    }
}
项目:halyard    文件:GoogleCredentials.java   
public static HttpRequestInitializer retryRequestInitializer() {
  return request -> {
    request.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(2));
    request.setReadTimeout((int) TimeUnit.MINUTES.toMillis(2));
    request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()));
  };
}
项目:halyard    文件:GoogleCredentials.java   
public static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
  return request -> {
    requestInitializer.initialize(request);
    request.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(2));
    request.setReadTimeout((int) TimeUnit.MINUTES.toMillis(2));
    request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()));
  };
}
项目:mesfavoris    文件:GDriveBackOffHttpRequestInitializer.java   
@Override
public void initialize(HttpRequest request) throws IOException {
    request.setInterceptor(credential);

    // Exponential Back-off for 5xx response and 403 rate limit exceeded
    // error
    HttpBackOffUnsuccessfulResponseHandler backOffHandler = getHttpBackOffUnsuccessfulResponseHandler();

    request.setUnsuccessfulResponseHandler(
            new CompositeHttpUnsuccessfulResponseHandler(credential, backOffHandler));

    // Back-off for socket connection error
    request.setIOExceptionHandler(getHttpBackOffIOExceptionHandler());
}
项目:mesfavoris    文件:GDriveBackOffHttpRequestInitializer.java   
private HttpBackOffUnsuccessfulResponseHandler getHttpBackOffUnsuccessfulResponseHandler() {
    HttpBackOffUnsuccessfulResponseHandler backOffHandler = new HttpBackOffUnsuccessfulResponseHandler(
            new ExponentialBackOff.Builder().setInitialIntervalMillis(200).setMaxElapsedTimeMillis(60000).build());
    backOffHandler.setBackOffRequired(
            (HttpResponse response) -> response.getStatusCode() / 100 == 5 || isRateLimitExceeded(response));
    return backOffHandler;
}
项目:mail-importer    文件:GmailService.java   
Gmail getServiceWithRetries() {
  HttpRequestInitializer httpRequestInitializer =
      request -> {
        credential.initialize(request);
        new UnsuccessfulResponseHandlerChainer().chain(
            request.getUnsuccessfulResponseHandler(),
            new HttpBackOffUnsuccessfulResponseHandler(
                backOffProvider.get()));
      };

  return new Gmail.Builder(httpTransport, jsonFactory, httpRequestInitializer)
      .setApplicationName(GmailServiceModule.APP_NAME)
      .build();
}
项目:bigdata-interop    文件:CredentialFactory.java   
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
  httpRequest.setIOExceptionHandler(
      new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
  httpRequest.setUnsuccessfulResponseHandler(
      new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()));
}
项目:elasticsearch_my    文件:RetryHttpInitializerWrapper.java   
@Override
public void initialize(HttpRequest httpRequest) {
    final HttpUnsuccessfulResponseHandler backoffHandler =
            new HttpBackOffUnsuccessfulResponseHandler(
                    new ExponentialBackOff.Builder()
                            .setMaxElapsedTimeMillis(((int) maxWait.getMillis()))
                            .build())
                    .setSleeper(sleeper);

    httpRequest.setInterceptor(wrappedCredential);
    httpRequest.setUnsuccessfulResponseHandler(
            new HttpUnsuccessfulResponseHandler() {
                int retry = 0;

                @Override
                public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException {
                    if (wrappedCredential.handleResponse(
                            request, response, supportsRetry)) {
                        // If credential decides it can handle it,
                        // the return code or message indicated
                        // something specific to authentication,
                        // and no backoff is desired.
                        return true;
                    } else if (backoffHandler.handleResponse(
                            request, response, supportsRetry)) {
                        // Otherwise, we defer to the judgement of
                        // our internal backoff handler.
                        logger.debug("Retrying [{}] times : [{}]", retry, request.getUrl());
                        return true;
                    } else {
                        return false;
                    }
                }
            });
    httpRequest.setIOExceptionHandler(
            new HttpBackOffIOExceptionHandler(
                    new ExponentialBackOff.Builder()
                            .setMaxElapsedTimeMillis(((int) maxWait.getMillis()))
                            .build())
                    .setSleeper(sleeper)
    );
}
项目:gyingpan    文件:UploadDir.java   
public static void main(String[] args) throws IOException {
    if (args.length != 3) {
        System.out.println("UploadDirs accountname folderid folder");
        return;
    }
    String folderid = args[1];
    String account = args[0];
    String folder = args[2];
    java.io.File jdrivedir = new java.io.File(
            new java.io.File(new java.io.File(
                    System.getProperty("user.home")), ".gyingpan"), account);
    jdrivedir.mkdirs();
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, jsonFactory, GDrive.CLIENT_ID,
            GDrive.CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
            .setAccessType("offline")
            .setApprovalPrompt("auto")
            .setDataStoreFactory(
                    new FileDataStoreFactory(new java.io.File(jdrivedir,
                            "driveauth"))).build();
    Credential credential = flow.loadCredential(args[0]);
    Drive service = new Drive.Builder(httpTransport, jsonFactory,
            credential).setHttpRequestInitializer(
            new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest request)
                        throws IOException {
                    credential.initialize(request);
                    request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(
                            new ExponentialBackOff()));
                    request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(
                            new ExponentialBackOff()));
                }
            }).build();
    HashMap<String, File> gfilelist = getGDriveFileList(service, folderid);
    uploadDir(service, gfilelist, folderid,
            new java.io.File(folder).toPath());
}
项目:java-docs-samples    文件:HttpExample.java   
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
public static void getConfig(String urlPath, String token, String projectId,
    String cloudRegion, String registryId, String deviceId, String version)
    throws UnsupportedEncodingException, IOException, JSONException, ProtocolException {
  // Build the resource path of the device that is going to be authenticated.
  String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryId, deviceId);
  urlPath = urlPath + devicePath + "/config?local_version=" + version;

  HttpRequestFactory requestFactory =
      HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) {
          request.setParser(new JsonObjectParser(JSON_FACTORY));
        }
      });

  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath));
  HttpHeaders heads = new HttpHeaders();

  heads.setAuthorization(String.format("Bearer %s", token));
  heads.setContentType("application/json; charset=UTF-8");
  heads.setCacheControl("no-cache");

  req.setHeaders(heads);
  ExponentialBackOff backoff = new ExponentialBackOff.Builder()
      .setInitialIntervalMillis(500)
      .setMaxElapsedTimeMillis(900000)
      .setMaxIntervalMillis(6000)
      .setMultiplier(1.5)
      .setRandomizationFactor(0.5)
      .build();
  req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
  HttpResponse res = req.execute();
  System.out.println(res.getStatusCode());
  System.out.println(res.getStatusMessage());
  InputStream in = res.getContent();

  System.out.println(CharStreams.toString(new InputStreamReader(in, Charsets.UTF_8)));
}
项目:java-docs-samples    文件:HttpExample.java   
/** Publish an event or state message using Cloud IoT Core via the HTTP API. */
public static void publishMessage(String payload, String urlPath, String messageType,
    String token, String projectId, String cloudRegion, String registryId, String deviceId)
    throws UnsupportedEncodingException, IOException, JSONException, ProtocolException {
  // Build the resource path of the device that is going to be authenticated.
  String devicePath =
      String.format(
          "projects/%s/locations/%s/registries/%s/devices/%s",
          projectId, cloudRegion, registryId, deviceId);
  String urlSuffix = messageType.equals("event") ? "publishEvent" : "setState";

  // Data sent through the wire has to be base64 encoded.
  Base64.Encoder encoder = Base64.getEncoder();

  String encPayload = encoder.encodeToString(payload.getBytes("UTF-8"));

  urlPath = urlPath + devicePath + ":" + urlSuffix;


  final HttpRequestFactory requestFactory =
      HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest request) {
          request.setParser(new JsonObjectParser(JSON_FACTORY));
        }
      });

  HttpHeaders heads = new HttpHeaders();
  heads.setAuthorization(String.format("Bearer %s", token));
  heads.setContentType("application/json; charset=UTF-8");
  heads.setCacheControl("no-cache");

  // Add post data. The data sent depends on whether we're updating state or publishing events.
  JSONObject data = new JSONObject();
  if (messageType.equals("event")) {
    data.put("binary_data", encPayload);
  } else {
    JSONObject state = new JSONObject();
    state.put("binary_data", encPayload);
    data.put("state", state);
  }

  ByteArrayContent content = new ByteArrayContent(
      "application/json", data.toString().getBytes("UTF-8"));

  final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath));
  req.setHeaders(heads);
  req.setContent(content);
  req.setRequestMethod("POST");
  ExponentialBackOff backoff = new ExponentialBackOff.Builder()
      .setInitialIntervalMillis(500)
      .setMaxElapsedTimeMillis(900000)
      .setMaxIntervalMillis(6000)
      .setMultiplier(1.5)
      .setRandomizationFactor(0.5)
      .build();
  req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));

  HttpResponse res = req.execute();
  System.out.println(res.getStatusCode());
  System.out.println(res.getStatusMessage());
}