Search in sources :

Example 11 with Topic

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

the class TopicAdminClientTest method listTopicSubscriptionsExceptionTest.

@Test
@SuppressWarnings("all")
public void listTopicSubscriptionsExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockPublisher.addException(exception);
    try {
        TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
        client.listTopicSubscriptions(topic);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) TopicName(com.google.pubsub.v1.TopicName) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Example 12 with Topic

use of com.google.pubsub.v1.Topic 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 13 with Topic

use of com.google.pubsub.v1.Topic 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 14 with Topic

use of com.google.pubsub.v1.Topic 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)

Example 15 with Topic

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

the class CreateTopicAndPublishMessages method publishMessages.

public static void publishMessages() throws Exception {
    // [START pubsub_publish]
    TopicName topicName = TopicName.create("my-project-id", "my-topic-id");
    Publisher publisher = null;
    List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
    try {
        // Create a publisher instance with default settings bound to the topic
        publisher = Publisher.defaultBuilder(topicName).build();
        List<String> messages = Arrays.asList("first message", "second message");
        // schedule publishing one message at a time : messages get automatically batched
        for (String message : messages) {
            ByteString data = ByteString.copyFromUtf8(message);
            PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
            // Once published, returns a server-assigned message id (unique within the topic)
            ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
            messageIdFutures.add(messageIdFuture);
        }
    } finally {
        // wait on any pending publish requests.
        List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
        for (String messageId : messageIds) {
            System.out.println("published with message ID: " + messageId);
        }
        if (publisher != null) {
            // When finished with the publisher, shutdown to free up resources.
            publisher.shutdown();
        }
    }
// [END pubsub_publish]
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

TopicName (com.google.pubsub.v1.TopicName)26 Test (org.junit.Test)22 Topic (com.google.pubsub.v1.Topic)13 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)8 ByteString (com.google.protobuf.ByteString)7 PubsubMessage (com.google.pubsub.v1.PubsubMessage)7 ApiException (com.google.api.gax.grpc.ApiException)6 StatusRuntimeException (io.grpc.StatusRuntimeException)6 Subscription (com.google.pubsub.v1.Subscription)5 ArrayList (java.util.ArrayList)5 ListTopicsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse)3 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)3 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)3 DeleteTopicRequest (com.google.pubsub.v1.DeleteTopicRequest)3 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)3 PublishRequest (com.google.pubsub.v1.PublishRequest)3 PublishResponse (com.google.pubsub.v1.PublishResponse)3 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)2