Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found

I try to write a test of pubsub:

@Test
public void sendTopic() throws Exception {

    CustomSubscriber customSubscriber = new CustomSubscriber();
    customSubscriber.startAndWait();

    CustomPublisher customPublisher = new CustomPublisher();
    customPublisher.publish("123");
}

and:

public CustomSubscriber() {
    this.subscriptionName = SubscriptionName.create(SdkServiceConfig.s.GCP_PROJECT_ID, SdkServiceConfig.s.TOPIC_ID );
    this.receiveMsgAction = (message, consumer) -> {
        // handle incoming message, then ack/nack the received message
        System.out.println("Id : " + message.getMessageId());
        System.out.println("Data : " + message.getData().toStringUtf8());
        consumer.ack();
    };
    this.afterStopAction = new ApiFutureEmpty();
}

// [TARGET startAsync()]
public void startAndWait() throws Exception {
    Subscriber subscriber = createSubscriberWithCustomCredentials();
    subscriber.startAsync();

    // Wait for a stop signal.
    afterStopAction.get();
    subscriber.stopAsync().awaitTerminated();
}

and:

public ApiFuture<String> publish(String message) throws Exception {
    ByteString data = ByteString.copyFromUtf8(message);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
    ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);

    ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {
        public void onSuccess(String messageId) {
            System.out.println("published with message id: " + messageId);
        }

        public void onFailure(Throwable t) {
            System.out.println("failed to publish: " + t);
        }
    });
    return messageIdFuture;
}

/**
 * Example of creating a {@code Publisher}.
 */
// [TARGET newBuilder(TopicName)]
// [VARIABLE "my_project"]
// [VARIABLE "my_topic"]
public void createPublisher(String projectId, String topicId) throws Exception {
    TopicName topic = TopicName.create(projectId, topicId);
    try {
        publisher = createPublisherWithCustomCredentials(topic);

    } finally {
        // When finished with the publisher, make sure to shutdown to free up resources.
        publisher.shutdown();
    }
}

When i run the code i get this error:

Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: Resource not found (resource=add-partner-request).

What am i missing?

like image 572
Elad Benda Avatar asked Jun 05 '17 13:06

Elad Benda


1 Answers

Whatever entity is named "add-partner-request" was not successfully created or does not belong to the project. If "add-partner-request" is the topic, then you actually need to create the topic; the line TopicName.create(projectId, topicId) is not sufficient for creating the topic itself. Typically, one would create the topic in the Cloud Pub/Sub portion of the Cloud console or via a gcloud command, e.g.,

gcloud pubsub topics create add-partner-request

Ensure that the project you are logged into in the console is the one used in the code. You should also set the project explicitly when creating the topic via the --project flag or verify that the default project is the correct one:

gcloud config list --format='text(core.project)'

For tests, it is typical to create and delete in code. For example, to create a topic:

Topic topic = null;
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
TopicAdminClient topicAdminClient = TopicAdminClient.create();
try {
  topic = topicAdminClient.createTopic(topicName);
} catch (APIException e) {
  System.out.println("Issue creating topic!");
}

If "add-partner-request" is the subscription name, then the same things apply. The gcloud command would change a bit:

gcloud pubsub subscriptions create add-partner-request --topic=<topic name>

The command to create the subscription in Java would be as follows:

Subscription subscription = null;
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create();
try {
  subscription = subscriptionAdminClient.createSubscription(
          subscriptionName, topicName, PushConfig.newBuilder().build(), 600);
} catch (APIException e) {
  System.out.println("Issue creating topic!");
}
like image 195
Kamal Aboul-Hosn Avatar answered Sep 24 '22 15:09

Kamal Aboul-Hosn