我按照https://cloud.google.com/tasks/docs/creating-appengine-tasks中的描述创建任务
它适用于 99% 的情况,但有时我会遇到以下异常:
java.lang.RuntimeException: ManagedChannel allocation site
at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init> (ManagedChannelOrphanWrapper.java:103)
at io.grpc.internal.ManagedChannelOrphanWrapper.<init> (ManagedChannelOrphanWrapper.java:53)
at io.grpc.internal.ManagedChannelOrphanWrapper.<init> (ManagedChannelOrphanWrapper.java:44)
at io.grpc.internal.AbstractManagedChannelImplBuilder.build (AbstractManagedChannelImplBuilder.java:411)
at by.company.application.QueueService.createTask (QueueService.java:76)
QueueService 内部方法代码:
private void createTask(String queueName, String payload, long timeShiftMillis) {
String queuePath = QueueName.of(gcpProjectId, gcpLocation, queueName).toString();
try (CloudTasksClient client = CloudTasksClient.create()) {
Task.Builder taskBuilder = Task
.newBuilder()
.setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder()
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setRelativeUri("/" + queueName)
.setHttpMethod(HttpMethod.POST)
.build());
taskBuilder.setScheduleTime(Timestamp
.newBuilder()
.setSeconds(Instant.now(Clock.systemUTC()).plusMillis(timeShiftMillis).getEpochSecond()));
Task build = taskBuilder.build();
if (!gcpTaskEnabled) {
return;
}
client.createTask(queuePath, build);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Can't create task", e);
}
}