下面列出了怎么用javax.ws.rs.client.InvocationCallback的API类实例代码及写法,或者点击链接到github查看源代码。
private void asyncAPICall() {
Client client = ClientBuilder.newClient();
Future<Response> futureRsp = client.target("http://localhost:8080/ims-micro-users/resources/users")
.request("application/json")
.async()
.get(new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
//Use the response
}
@Override
public void failed(Throwable throwable) {
//Error case
}
});
// System.out.println("response " + res.get().readEntity(User.class).getName());
}
private static InvocationCallback<Response> responseInvocationCallback() {
return new InvocationCallback<Response>() {
@Override
public void completed(Response res) {
System.out.println("Status:" + res.getStatusInfo());
System.out.println("Entity:" + res.getEntity());
System.out.println("Request success!");
}
@Override
public void failed(Throwable e) {
System.out.println("Request failed!");
}
};
}
@Override
public CompletableFuture<SchemaInfo> getSchemaInfoAsync(String topic, long version) {
TopicName tn = TopicName.get(topic);
WebTarget path = schemaPath(tn).path(Long.toString(version));
final CompletableFuture<SchemaInfo> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<GetSchemaResponse>() {
@Override
public void completed(GetSchemaResponse response) {
future.complete(convertGetSchemaResponseToSchemaInfo(tn, response));
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<DispatchRate> getReplicatorDispatchRateAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "replicatorDispatchRate");
final CompletableFuture<DispatchRate> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<DispatchRate>() {
@Override
public void completed(DispatchRate dispatchRate) {
future.complete(dispatchRate);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<Integer> getSubscriptionExpirationTimeAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "subscriptionExpirationTime");
final CompletableFuture<Integer> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<Integer>() {
@Override
public void completed(Integer expirationTime) {
future.complete(expirationTime);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadataAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "partitions");
final CompletableFuture<PartitionedTopicMetadata> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<PartitionedTopicMetadata>() {
@Override
public void completed(PartitionedTopicMetadata response) {
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<List<SchemaInfo>> getAllSchemasAsync(String topic) {
WebTarget path = schemasPath(TopicName.get(topic));
TopicName topicName = TopicName.get(topic);
final CompletableFuture<List<SchemaInfo>> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<GetAllVersionsSchemaResponse>() {
@Override
public void completed(GetAllVersionsSchemaResponse response) {
future.complete(
response.getGetSchemaResponses().stream()
.map(getSchemaResponse ->
convertGetSchemaResponseToSchemaInfo(topicName, getSchemaResponse))
.collect(Collectors.toList()));
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<List<String>> getSubscriptionsAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "subscriptions");
final CompletableFuture<List<String>> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<List<String>>() {
@Override
public void completed(List<String> response) {
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<TopicStats> getStatsAsync(String topic, boolean getPreciseBacklog) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "stats").queryParam("getPreciseBacklog", getPreciseBacklog);
final CompletableFuture<TopicStats> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<TopicStats>() {
@Override
public void completed(TopicStats response) {
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<PersistentTopicInternalStats> getInternalStatsAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "internalStats");
final CompletableFuture<PersistentTopicInternalStats> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<PersistentTopicInternalStats>() {
@Override
public void completed(PersistentTopicInternalStats response) {
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<SchemaInfoWithVersion> getSchemaInfoWithVersionAsync(String topic) {
TopicName tn = TopicName.get(topic);
final CompletableFuture<SchemaInfoWithVersion> future = new CompletableFuture<>();
asyncGetRequest(schemaPath(tn),
new InvocationCallback<GetSchemaResponse>() {
@Override
public void completed(GetSchemaResponse response) {
future.complete(convertGetSchemaResponseToSchemaInfoWithVersion(tn, response));
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<PartitionedTopicStats> getPartitionedStatsAsync(String topic,
boolean perPartition, boolean getPreciseBacklog) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "partitioned-stats");
path = path.queryParam("perPartition", perPartition).queryParam("getPreciseBacklog", getPreciseBacklog);
final CompletableFuture<PartitionedTopicStats> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<PartitionedTopicStats>() {
@Override
public void completed(PartitionedTopicStats response) {
if (!perPartition) {
response.partitions.clear();
}
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<PartitionedTopicInternalStats> getPartitionedInternalStatsAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "partitioned-internalStats");
final CompletableFuture<PartitionedTopicInternalStats> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<PartitionedTopicInternalStats>() {
@Override
public void completed(PartitionedTopicInternalStats response) {
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<Integer> getMaxUnackedMessagesPerConsumerAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "maxUnackedMessagesPerConsumer");
final CompletableFuture<Integer> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Integer>() {
@Override
public void completed(Integer max) {
future.complete(max);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
private CompletableFuture<Message<byte[]>> getRemoteMessageById(String topic, long ledgerId, long entryId) {
TopicName topicName = validateTopic(topic);
WebTarget path = topicPath(topicName, "ledger", Long.toString(ledgerId), "entry", Long.toString(entryId));
final CompletableFuture<Message<byte[]>> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
try {
future.complete(getMessagesFromHttpResponse(topicName.toString(), response).get(0));
} catch (Exception e) {
future.completeExceptionally(getApiException(e));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<LongRunningProcessStatus> compactionStatusAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "compaction");
final CompletableFuture<LongRunningProcessStatus> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<LongRunningProcessStatus>() {
@Override
public void completed(LongRunningProcessStatus longRunningProcessStatus) {
future.complete(longRunningProcessStatus);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<Void> triggerOffloadAsync(String topic, MessageId messageId) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "offload");
final CompletableFuture<Void> future = new CompletableFuture<>();
try {
request(path).async().put(Entity.entity(messageId, MediaType.APPLICATION_JSON)
, new InvocationCallback<MessageIdImpl>() {
@Override
public void completed(MessageIdImpl response) {
future.complete(null);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
} catch (PulsarAdminException cae) {
future.completeExceptionally(cae);
}
return future;
}
@Override
public CompletableFuture<List<ConnectorDefinition>> getBuiltInSinksAsync() {
WebTarget path = sink.path("builtinsinks");
final CompletableFuture<List<ConnectorDefinition>> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (!response.getStatusInfo().equals(Response.Status.OK)) {
future.completeExceptionally(getApiException(response));
} else {
future.complete(response.readEntity(
new GenericType<List<ConnectorDefinition>>() {}));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<MessageId> getLastMessageIdAsync(String topic) {
TopicName tn = validateTopic(topic);
WebTarget path = topicPath(tn, "lastMessageId");
final CompletableFuture<MessageId> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<BatchMessageIdImpl>() {
@Override
public void completed(BatchMessageIdImpl response) {
if (response.getBatchIndex() == -1) {
future.complete(new MessageIdImpl(response.getLedgerId(),
response.getEntryId(), response.getPartitionIndex()));
}
future.complete(response);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<List<String>> getTenantsAsync() {
final CompletableFuture<List<String>> future = new CompletableFuture<>();
asyncGetRequest(adminTenants,
new InvocationCallback<List<String>>() {
@Override
public void completed(List<String> tenants) {
future.complete(tenants);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<TenantInfo> getTenantInfoAsync(String tenant) {
WebTarget path = adminTenants.path(tenant);
final CompletableFuture<TenantInfo> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<TenantInfo>() {
@Override
public void completed(TenantInfo tenantInfo) {
future.complete(tenantInfo);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<String> lookupTopicAsync(String topic) {
TopicName topicName = TopicName.get(topic);
String prefix = topicName.isV2() ? "/topic" : "/destination";
WebTarget path = v2lookup.path(prefix).path(topicName.getLookupName());
final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<LookupData>() {
@Override
public void completed(LookupData lookupData) {
if (useTls) {
future.complete(lookupData.getBrokerUrlTls());
} else {
future.complete(lookupData.getBrokerUrl());
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<String> getBundleRangeAsync(String topic) {
TopicName topicName = TopicName.get(topic);
String prefix = topicName.isV2() ? "/topic" : "/destination";
WebTarget path = v2lookup.path(prefix).path(topicName.getLookupName()).path("bundle");
final CompletableFuture<String> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<String>() {
@Override
public void completed(String bundleRange) {
future.complete(bundleRange);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<FunctionConfig> getFunctionAsync(String tenant, String namespace, String function) {
WebTarget path = functions.path(tenant).path(namespace).path(function);
final CompletableFuture<FunctionConfig> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (!response.getStatusInfo().equals(Response.Status.OK)) {
future.completeExceptionally(getApiException(response));
} else {
future.complete(response.readEntity(FunctionConfig.class));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<FunctionStatus> getFunctionStatusAsync(String tenant, String namespace, String function) {
WebTarget path = functions.path(tenant).path(namespace).path(function).path("status");
final CompletableFuture<FunctionStatus> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (!response.getStatusInfo().equals(Response.Status.OK)) {
future.completeExceptionally(getApiException(response));
} else {
future.complete(response.readEntity(FunctionStatus.class));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<FunctionStatus.FunctionInstanceStatus.FunctionInstanceStatusData> getFunctionStatusAsync(
String tenant, String namespace, String function, int id) {
WebTarget path =
functions.path(tenant).path(namespace).path(function).path(Integer.toString(id)).path("status");
final CompletableFuture<FunctionStatus.FunctionInstanceStatus.FunctionInstanceStatusData> future =
new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (!response.getStatusInfo().equals(Response.Status.OK)) {
future.completeExceptionally(getApiException(response));
} else {
future.complete(response.readEntity(
FunctionStatus.FunctionInstanceStatus.FunctionInstanceStatusData.class));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<FunctionStats.FunctionInstanceStats.FunctionInstanceStatsData> getFunctionStatsAsync(
String tenant, String namespace, String function, int id) {
WebTarget path = functions.path(tenant).path(namespace).path(function).path(Integer.toString(id)).path("stats");
final CompletableFuture<FunctionStats.FunctionInstanceStats.FunctionInstanceStatsData> future =
new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (!response.getStatusInfo().equals(Response.Status.OK)) {
future.completeExceptionally(getApiException(response));
} else {
future.complete(response.readEntity(
FunctionStats.FunctionInstanceStats.FunctionInstanceStatsData.class));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<DelayedDeliveryPolicies> getDelayedDeliveryAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "delayedDelivery");
final CompletableFuture<DelayedDeliveryPolicies> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<DelayedDeliveryPolicies>() {
@Override
public void completed(DelayedDeliveryPolicies delayedDeliveryPolicies) {
future.complete(delayedDeliveryPolicies);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<Integer> getMaxProducersPerTopicAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "maxProducersPerTopic");
final CompletableFuture<Integer> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Integer>() {
@Override
public void completed(Integer max) {
future.complete(max);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
@Override
public CompletableFuture<FunctionState> getFunctionStateAsync(
String tenant, String namespace, String function, String key) {
WebTarget path = functions.path(tenant).path(namespace).path(function).path("state").path(key);
final CompletableFuture<FunctionState> future = new CompletableFuture<>();
asyncGetRequest(path,
new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (!response.getStatusInfo().equals(Response.Status.OK)) {
future.completeExceptionally(getApiException(response));
} else {
future.complete(response.readEntity(FunctionState.class));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}