2017-06-05 10 views
3

を発見していない私はのpubsubのテストを書いてみる:NOT_FOUND:リソース

@Test 
public void sendTopic() throws Exception { 

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

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

と:

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

と:

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

コードを実行すると、このエラーが表示されます。

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

何が欠けていますか?

答えて

0

私はTOPIC_IDがトピックの名前であると仮定しています。実際にはサブスクリプションを参照する必要があります。あなたは簡単にGCPコンソールからサブスクリプションを作成し、SubscriptionName.create(project、yoursubscriptionname)の名前を参照することができます

0

私はあなたのプロジェクト内で次の名前でトピックを作成することを忘れていると思います。要求"。 次のコードを使用して作成できます。

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