2017-06-05 1 views
3

fand ich versuche, einen Test von PubSub zu schreiben:Verursacht durch: io.grpc.StatusRuntimeException: NOT_FOUND: Ressource nicht

@Test 
public void sendTopic() throws Exception { 

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

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

und:

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(); 
} 

und:

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(); 
    } 
} 

Wenn ich den Code ausführen, erhalte ich diesen Fehler:

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

Was fehlt mir?

Antwort

0

Ich nehme an, TOPIC_ID ist der Name Ihres Themas; Sie müssen tatsächlich auf ein Abonnement verweisen. Sie können ganz einfach ein Abonnement über die GCP-Konsole erstellen und dann auf den Namen in SubscriptionName.create (Projekt, IhrAubutznamenname) verweisen.

0

Ich glaube, dass Sie vergessen haben, in Ihrem Projekt ein Thema mit dem folgenden Namen zu erstellen: add-partner- anfordern". Sie können es mit dem folgenden Code erstellen:

try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { 
    // projectId <= unique project identifier, eg. "my-project-id" 
    TopicName topicName = TopicName.create(projectId, "add-partner-request"); 
    Topic topic = topicAdminClient.createTopic(topicName); 
    return topic; 
} 
Verwandte Themen