2017-06-05 4 views
3

J'essaie d'écrire un test de PubSub:Causée par: io.grpc.StatusRuntimeException: NOT_FOUND: Ressource introuvable

@Test 
public void sendTopic() throws Exception { 

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

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

et:

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

et:

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

Lorsque je cours le code j'obtiens cette erreur:

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

Que manque-t-il?

Répondre

0

Je suppose que TOPIC_ID est le nom de votre sujet; vous avez réellement besoin de référencer un abonnement. Vous pouvez facilement créer un abonnement à partir de la console GCP, puis référencer ce nom dans SubscriptionName.create (project, yoursubscriptionname)

0

Je pense que vous oubliez de créer un sujet dans votre projet avec le nom suivant: "add-partner- demande". Vous pouvez le créer en utilisant le code suivant:

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