use of com.google.cloud.pubsub.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.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.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.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method createIotTopic.
/**
* Creates a topic and grants the IoT service account access.
*/
public static Topic createIotTopic(String projectId, String topicId) throws Exception {
// Create a new topic
final TopicName topicName = TopicName.create(projectId, topicId);
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
final Topic topic = topicAdminClient.createTopic(topicName);
com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
// add role -> members binding
Binding binding = Binding.newBuilder().addMembers("serviceAccount:cloud-iot@system.gserviceaccount.com").setRole(Role.owner().toString()).build();
// create updated policy
com.google.iam.v1.Policy updatedPolicy = com.google.iam.v1.Policy.newBuilder(policy).addBindings(binding).build();
topicAdminClient.setIamPolicy(topicName.toString(), updatedPolicy);
System.out.println("Setup topic / policy for: " + topic.getName());
return topic;
}
}
use of com.google.cloud.pubsub.v1.TopicAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class ManagerIT method testCreateGetRegistry.
@Test
public void testCreateGetRegistry() throws Exception {
topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
DeviceRegistryExample.getRegistry(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
String got = bout.toString();
Assert.assertFalse(got.contains("eventNotificationConfigs"));
DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
topicAdminClient.deleteTopic(topic.getNameAsTopicName());
}
}
Aggregations