use of com.google.cloud.pubsub.spi.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class Cleanup method deleteTestTopics.
private static void deleteTestTopics(String projectId, String[] testTopics) throws Exception {
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
for (String topicId : testTopics) {
try {
topicAdminClient.deleteTopic(TopicName.create(projectId, topicId));
System.out.println("Topic deleted : " + topicId);
} catch (Exception e) {
//do nothing catch clause
}
}
}
}
use of com.google.cloud.pubsub.spi.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method testSubscriptionPermissions.
/** Example of testing whether the caller has the provided permissions on a subscription. */
public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionId) throws Exception {
// [START pubsub_test_subscription_permissions]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.subscriptions.get");
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(subscriptionName.toString(), permissions);
return testedPermissions;
}
// [END pubsub_test_subscription_permissions]
}
use of com.google.cloud.pubsub.spi.v1.TopicAdminClient in project toolkit by googleapis.
the class Pubsub method gapic.
private static void gapic(final Settings settings) throws Exception {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
// In real clients, InstantiatingChannelProvider is responsible for adding interceptors.
// We can't use it here because InstantiatingChannelProvider doesn't have sslContext method.
// Instead, we imitate HeaderInterceptor.
ClientInterceptor headerInterceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(call) {
@Override
public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
headers.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
super.start(responseListener, headers);
}
};
}
};
ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).intercept(headerInterceptor).build();
final Semaphore semaphore = new Semaphore(settings.numWorkers());
final TopicAdminClient client = TopicAdminClient.create(TopicAdminSettings.defaultBuilder().setChannelProvider(FixedChannelProvider.create(channel)).setExecutorProvider(FixedExecutorProvider.create(executor)).build());
final AtomicLong resetTime = new AtomicLong();
final AtomicLong numCalls = new AtomicLong();
final AtomicLong numErrs = new AtomicLong();
long endTime = System.nanoTime() + settings.warmDurNano() + settings.targetDurNano();
Thread resetter = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(settings.warmDurNano() / MILLION);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
numCalls.set(0);
numErrs.set(0);
resetTime.set(System.nanoTime());
}
});
resetter.start();
while (System.nanoTime() < endTime) {
semaphore.acquire(1);
ApiFutures.addCallback(client.getTopicCallable().futureCall(GetTopicRequest.newBuilder().setTopicWithTopicName(TOPIC_NAME_RESOURCE).build()), new ApiFutureCallback<Topic>() {
@Override
public void onSuccess(Topic topic) {
if (!topic.getName().equals(TOPIC_NAME_STRING)) {
numErrs.incrementAndGet();
}
both();
}
@Override
public void onFailure(Throwable t) {
numErrs.incrementAndGet();
both();
}
void both() {
numCalls.incrementAndGet();
semaphore.release(1);
}
});
}
long nCalls = numCalls.get();
long nErrs = numErrs.get();
long runDurNano = System.nanoTime() - resetTime.get();
System.out.println("errors: " + nErrs);
System.out.println("calls: " + nCalls);
System.out.println("time per call (ns): " + (runDurNano / nCalls));
System.out.println("QPS: " + (nCalls * BILLION / runDurNano));
client.close();
channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
use of com.google.cloud.pubsub.spi.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method getTopic.
/** Example of getting a topic. */
public Topic getTopic(String topicId) throws Exception {
// [START pubsub_get_topic]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
Topic topic = topicAdminClient.getTopic(topicName);
return topic;
}
// [END pubsub_get_topic]
}
use of com.google.cloud.pubsub.spi.v1.TopicAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method listTopics.
/** Example of listing topics. */
public ListTopicsPagedResponse listTopics() throws Exception {
// [START pubsub_list_topics]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
ListTopicsRequest listTopicsRequest = ListTopicsRequest.newBuilder().setProjectWithProjectName(ProjectName.create(projectId)).build();
ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAll();
for (Topic topic : topics) {
// do something with the topic
}
return response;
}
// [END pubsub_list_topics]
}
Aggregations