Я пытаюсь опубликовать в теме публикации / публикации Google, используя следующую информацию:
ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id");
Publisher publisher = null;
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.newBuilder(topicName).build();
List<String> messages = Arrays.asList("first message", "second message");
for (final 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> future = publisher.publish(pubsubMessage);
// Add an asynchronous callback to handle success / failure
ApiFutures.addCallback(
future,
new ApiFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
if (throwable instanceof ApiException) {
ApiException apiException = ((ApiException) throwable);
// details on the API exception
System.out.println(apiException.getStatusCode().getCode());
System.out.println(apiException.isRetryable());
}
System.out.println("Error publishing message : " + message);
}
@Override
public void onSuccess(String messageId) {
// Once published, returns server-assigned message ids (unique within the topic)
System.out.println(messageId);
}
},
MoreExecutors.directExecutor());
}
} finally {
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
publisher.awaitTermination(1, TimeUnit.MINUTES);
}
}
Я изменил значения по умолчанию, которые вы видите здесь, на данные учетной записи, которую я нажимаю.
Переменная среды указывает на файл JSON, содержащий учетные данные для публикации / подписки:
GOOGLE_APPLICATION_CREDENTIALS
был задан с помощью:
export GOOGLE_APPLICATION_CREDENTIALS=path/to/file.json
и проверен с помощью echo $GOOGLE_APPLICATION_CREDENTIALS
- после перезагрузки.
Но я все еще сталкиваюсь:
The Application Default Credentials are not available. They are available
if running in Google Compute Engine. Otherwise, the environment variable
GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining
the credentials. See https://developers.google.com/accounts/docs/application-
default-credentials for more information.
Я полагаю, это связано со средой по умолчанию, в которой выполняется приложение, или, скорее, с тем, что объект GCP считает контекстом - runningOnComputeEngine
:
com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
INFO: Failed to detect whether we are running on Google Compute Engine.
также отображается диалоговое окно:
Unable to launch App Engine Server
Cannot determine server execution context
и в проекте отсутствуют настройки Google Cloud Platform (Eclipse 2019-3):
Это не приложение App Engine.
Как установить среду, на которую указывают объекты GCP -> Non App Engine.
Для справки: