use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubSnippets method testPublisherSubscriberHelper.
private void testPublisherSubscriberHelper(TopicName topicName, SubscriptionName subscriptionName) throws Exception {
String messageToPublish = "my-message";
Publisher publisher = null;
try {
publisher = Publisher.defaultBuilder(topicName).build();
PublisherSnippets snippets = new PublisherSnippets(publisher);
final SettableApiFuture<Void> done = SettableApiFuture.create();
ApiFutures.addCallback(snippets.publish(messageToPublish), new ApiFutureCallback<String>() {
public void onSuccess(String messageId) {
done.set(null);
}
public void onFailure(Throwable t) {
done.setException(t);
}
});
done.get();
} finally {
if (publisher != null) {
publisher.shutdown();
}
}
final BlockingQueue<PubsubMessage> queue = new ArrayBlockingQueue<>(1);
final SettableApiFuture<Void> done = SettableApiFuture.create();
final SettableApiFuture<PubsubMessage> received = SettableApiFuture.create();
SubscriberSnippets snippets = new SubscriberSnippets(subscriptionName, new MessageReceiverSnippets(queue).messageReceiver(), done, MoreExecutors.directExecutor());
new Thread(new Runnable() {
@Override
public void run() {
try {
received.set(queue.poll(10, TimeUnit.MINUTES));
} catch (InterruptedException e) {
received.set(null);
}
// signal the subscriber to clean up
done.set(null);
}
}).start();
// blocks until done is set
snippets.startAndWait();
PubsubMessage message = received.get();
assertNotNull(message);
assertEquals(message.getData().toStringUtf8(), messageToPublish);
}
use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.
the class ITSubscriptionAdminClientSnippets method createSubscription.
private Subscription createSubscription(String topicName, String subscriptionName) throws Exception {
createTopic(topicName);
Subscription subscription = subscriptionAdminClientSnippets.createSubscription(topicName, subscriptionName);
assertNotNull(subscription);
Subscription retrievedSubscription = subscriptionAdminClientSnippets.getSubscription(subscriptionName);
assertNotNull(retrievedSubscription);
assertEquals(subscription.getName(), retrievedSubscription.getName());
return subscription;
}
use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.
the class CreateSubscriptionAndConsumeMessages method main.
public static void main(String... args) throws Exception {
TopicName topic = TopicName.create("my-project-id", "my-topic-id");
SubscriptionName subscription = SubscriptionName.create("my-project-id", "my-topic-id");
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
}
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
System.out.println("Received message: " + message.getData().toStringUtf8());
consumer.ack();
}
};
Subscriber subscriber = null;
try {
subscriber = Subscriber.defaultBuilder(subscription, receiver).build();
subscriber.addListener(new Subscriber.Listener() {
@Override
public void failed(Subscriber.State from, Throwable failure) {
// Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
System.err.println(failure);
}
}, MoreExecutors.directExecutor());
subscriber.startAsync().awaitRunning();
Thread.sleep(60000);
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method getSubscriptionPolicy.
/** Example of getting a subscription policy. */
public Policy getSubscriptionPolicy(String subscriptionId) throws Exception {
// [START pubsub_get_subscription_policy]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
if (policy == null) {
// subscription was not found
}
return policy;
}
// [END pubsub_get_subscription_policy]
}
use of com.google.pubsub.v1.SubscriptionName 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]
}
Aggregations