Refine search
@Override public ListenableFuture<Response<R>> adapt(final Call<R> call) { return new AbstractFuture<Response<R>>() { { call.enqueue(new Callback<R>() { @Override public void onResponse(Call<R> call, Response<R> response) { set(response); } @Override public void onFailure(Call<R> call, Throwable t) { setException(t); } }); } @Override protected void interruptTask() { call.cancel(); } }; } }
private static void printContributors(GitHub gitHub, String owner, String repo) throws IOException { System.out.println(String.format("== Contributors for %s/%s ==", owner, repo)); Call<List<Contributor>> contributors = gitHub.contributors(owner, repo); for (Contributor contributor : contributors.execute().body()) { System.out.println(contributor.login + " (" + contributor.contributions + ")"); } System.out.println(); } }
@Override protected void interruptTask() { call.cancel(); } };
final Call<RemoteCallResponseType> call = fn.apply(r); try { final Response<RemoteCallResponseType> response = call.execute(); if (response.isSuccessful()) { return Optional.of(transformer.apply(response.body())); } else { LOG.warn("Unable to call {} on node <{}>, result: {}", call.request().url(), node, response.message()); return Optional.<FinalResponseType>empty(); LOG.warn("Unable to call {} on node <{}>", call.request().url(), node, e); return Optional.<FinalResponseType>empty();
@Override public void onResponse(Call<Page> call, Response<Page> response) { if (!response.isSuccessful()) { System.out.println(call.request().url() + ": failed: " + response.code()); return; } // Print this page's URL and title. Page page = response.body(); HttpUrl base = response.raw().request().url(); System.out.println(base + ": " + page.title); // Enqueue its links for visiting. for (String link : page.links) { HttpUrl linkUrl = base.resolve(link); if (linkUrl != null && fetchedUrls.add(linkUrl)) { crawlPage(linkUrl); } } }
@GET @Path("{jobId}") @Timed @ApiOperation(value = "Get job with the given ID") @Produces(MediaType.APPLICATION_JSON) public SystemJobSummary getJob(@ApiParam(name = "jobId", required = true) @PathParam("jobId") String jobId) throws IOException { for (Map.Entry<String, Node> entry : nodeService.allActive().entrySet()) { final RemoteSystemJobResource remoteSystemJobResource = remoteInterfaceProvider.get(entry.getValue(), this.authenticationToken, RemoteSystemJobResource.class); try { final Response<SystemJobSummary> response = remoteSystemJobResource.get(jobId).execute(); if (response.isSuccessful()) { // Return early because there can be only one job with the same ID in the cluster. return response.body(); } } catch (IOException e) { LOG.warn("Unable to fetch system jobs from node {}:", entry.getKey(), e); } } throw new NotFoundException("System job with id " + jobId + " not found!"); }
public static void main(String... args) throws IOException { HostSelectionInterceptor hostSelectionInterceptor = new HostSelectionInterceptor(); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(hostSelectionInterceptor) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://www.github.com/") .callFactory(okHttpClient) .build(); Pop pop = retrofit.create(Pop.class); Response<ResponseBody> response1 = pop.robots().execute(); System.out.println("Response from: " + response1.raw().request().url()); System.out.println(response1.body().string()); hostSelectionInterceptor.setHost("www.pepsi.com"); Response<ResponseBody> response2 = pop.robots().execute(); System.out.println("Response from: " + response2.raw().request().url()); System.out.println(response2.body().string()); } }
@Test public void passThroughCallsToDecoratedObject() throws IOException { final Call<String> call = mock(StringCall.class); final Call<String> decorated = new DecoratedCall<>(call); decorated.cancel(); Mockito.verify(call).cancel(); decorated.enqueue(null); Mockito.verify(call).enqueue(any()); decorated.isExecuted(); Mockito.verify(call).isExecuted(); decorated.isCanceled(); Mockito.verify(call).isCanceled(); decorated.clone(); Mockito.verify(call).clone(); decorated.request(); Mockito.verify(call).request(); decorated.execute(); Mockito.verify(call).execute(); }
@Override public Response<T> execute() throws IOException { return delegate.execute(); }
@POST @Timed @ApiOperation(value = "Resume message processing on node") @Path("resume") @NoAuditEvent("proxy resource, audit event will be emitted on target node") public void resume(@ApiParam(name = "nodeId", value = "The id of the node where processing will be resumed.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException { final Response response = this.getRemoteSystemProcessingResource(nodeId).resume().execute(); if (!response.isSuccessful()) { LOG.warn("Unable to resume message processing on node {}: {}", nodeId, response.message()); throw new WebApplicationException(response.message(), BAD_GATEWAY); } } }
@POST @Timed @ApiOperation(value = "Shutdown node gracefully.", notes = "Attempts to process all buffered and cached messages before exiting, " + "shuts down inputs first to make sure that no new messages are accepted.") @AuditEvent(type = AuditEventTypes.NODE_SHUTDOWN_INITIATE) public void shutdown(@ApiParam(name = "nodeId", value = "The id of the node to shutdown.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException { final Node targetNode = nodeService.byNodeId(nodeId); RemoteSystemShutdownResource remoteSystemShutdownResource = remoteInterfaceProvider.get(targetNode, this.authenticationToken, RemoteSystemShutdownResource.class); final Response response = remoteSystemShutdownResource.shutdown().execute(); if (response.code() != ACCEPTED.getStatusCode()) { LOG.warn("Unable send shut down signal to node {}: {}", nodeId, response.message()); throw new WebApplicationException(response.message(), BAD_GATEWAY); } } }
@Test public void pojo_returnCallCancelBeforeEnqueue() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final Call<Pojo> pojoCall = service.pojoReturnCall(); pojoCall.cancel(); pojoCall.enqueue(new Callback<Pojo>() { @Override public void onResponse(Call<Pojo> call, Response<Pojo> response) { } @Override public void onFailure(Call<Pojo> call, Throwable t) { countDownLatch.countDown(); } }); assertThat(countDownLatch.await(3, TimeUnit.SECONDS)).isTrue(); }
@Override protected void subscribeActual(Observer<? super Response<T>> observer) { // Since Call is a one-shot type, clone it for each new observer. Call<T> call = originalCall.clone(); CallCallback<T> callback = new CallCallback<>(call, observer); observer.onSubscribe(callback); if (!callback.isDisposed()) { call.enqueue(callback); } }
@Test public void decorateUnsuccessfulCall() throws Exception { stubFor(get(urlPathEqualTo("/greeting")) .willReturn(aResponse() .withStatus(500) .withHeader("Content-Type", "text/plain"))); final Response<String> response = service.greeting().execute(); assertThat(response.code()) .describedAs("Response code") .isEqualTo(500); final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1); }
@Override public void call(Subscriber<? super Response<T>> subscriber) { // Since Call is a one-shot type, clone it for each new subscriber. Call<T> call = originalCall.clone(); CallArbiter<T> arbiter = new CallArbiter<>(call, subscriber); subscriber.add(arbiter); subscriber.setProducer(arbiter); Response<T> response; try { response = call.execute(); } catch (Throwable t) { Exceptions.throwIfFatal(t); arbiter.emitError(t); return; } arbiter.emitResponse(response); } }
@Override public void onCancel(DialogInterface dialog) { if (!call.isCanceled()) { call.cancel(); } }
@Override public Request request() { return delegate.request(); } }
@Override public Call<T> clone() { return call.clone(); }
@GET @Timed @Path("/names") @ApiOperation(value = "Get all metrics keys/names from node") @RequiresPermissions(RestPermissions.METRICS_ALLKEYS) public MetricNamesResponse metricNames(@ApiParam(name = "nodeId", value = "The id of the node whose metrics we want.", required = true) @PathParam("nodeId") String nodeId) throws IOException, NodeNotFoundException { final Response<MetricNamesResponse> result = getResourceForNode(nodeId).metricNames().execute(); if (result.isSuccessful()) { return result.body(); } else { throw new WebApplicationException(result.message(), BAD_GATEWAY); } }