Search in sources :

Example 11 with TopicName

use of com.google.pubsub.v1.TopicName 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);
}
Also used : Publisher(com.google.cloud.pubsub.spi.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue)

Example 12 with TopicName

use of com.google.pubsub.v1.TopicName 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;
}
Also used : Subscription(com.google.pubsub.v1.Subscription)

Example 13 with TopicName

use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.

the class ITTopicAdminClientSnippets method topicAddedIsSameAsRetrieved.

@Test
public void topicAddedIsSameAsRetrieved() throws Exception {
    String topicName = topics[0];
    Topic topicAdded = topicAdminClientSnippets.createTopic(topicName);
    assertNotNull(topicAdded);
    Topic topicRetrieved = topicAdminClientSnippets.getTopic(topicName);
    assertEquals(topicAdded, topicRetrieved);
}
Also used : Topic(com.google.pubsub.v1.Topic) Test(org.junit.Test)

Example 14 with TopicName

use of com.google.pubsub.v1.TopicName in project google-cloud-java by GoogleCloudPlatform.

the class ITTopicAdminClientSnippets method deletedTopicIsNotRetrievableAndThrowsException.

@Test(expected = ApiException.class)
public void deletedTopicIsNotRetrievableAndThrowsException() throws Exception {
    String topicName = topics[0];
    Topic topicAdded = topicAdminClientSnippets.createTopic(topicName);
    assertNotNull(topicAdded);
    TopicName formattedName = topicAdminClientSnippets.deleteTopic(topicName);
    assertNotNull(formattedName);
    topicAdminClientSnippets.getTopic(topicName);
}
Also used : Topic(com.google.pubsub.v1.Topic) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 15 with TopicName

use of com.google.pubsub.v1.TopicName 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();
        }
    }
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

TopicName (com.google.pubsub.v1.TopicName)28 Test (org.junit.Test)20 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)7 ByteString (com.google.protobuf.ByteString)7 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)7 ApiException (com.google.api.gax.grpc.ApiException)6 PubsubMessage (com.google.pubsub.v1.PubsubMessage)6 Subscription (com.google.pubsub.v1.Subscription)6 Topic (com.google.pubsub.v1.Topic)6 StatusRuntimeException (io.grpc.StatusRuntimeException)6 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)4 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)4 PushConfig (com.google.pubsub.v1.PushConfig)3 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)2 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)2 ListTopicSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse)2 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)2 Policy (com.google.iam.v1.Policy)2 DeleteTopicRequest (com.google.pubsub.v1.DeleteTopicRequest)2